Ejemplo n.º 1
0
        public static void ProvisionWebStructure(this SPWeb web, WebDefinitionCollection subsites)
        {
            foreach (var site in subsites)
            {
                var existed = web.Webs.Cast <SPWeb>().Where(p => p.Url.Contains(site.Url)).FirstOrDefault();

                if (existed != null && !site.Overwrite)
                {
                    continue;
                }
                if (existed != null && site.Overwrite)
                {
                    DeleteSite(existed);
                }

                var newSite = web.AddSite(site.Url.Trim(), site.Name.Trim(), site.Template.Trim());
                if (newSite == null)
                {
                    continue;
                }
                //Provision Lists
                //Provision Features
                foreach (var feature in site.Features)
                {
                    newSite.Features.Add(feature.Guid);
                    //newSite.Update();
                }

                //BreakRoleInheritance
                if (site.BreakRoleInheritance)
                {
                    try
                    {
                        newSite.AllowUnsafeUpdates = true;

                        if (!newSite.HasUniqueRoleAssignments)
                        {
                            newSite.BreakRoleInheritance(false);
                        }

                        //create group
                        string groupOwners = site.Name.Trim() + " Owners";
                        newSite.CreateNewGroup(groupOwners, "Use this group to grant people full control permissions to the SharePoint site: " + site.Name.Trim(), SPRoleType.Administrator);

                        string groupMembers = site.Name.Trim() + " Members";
                        newSite.CreateNewGroup(groupMembers, "Use this group to grant people contribute permissions to the SharePoint site: " + site.Name.Trim(), SPRoleType.Contributor);

                        string groupVisitors = site.Name.Trim() + " Visitors";
                        newSite.CreateNewGroup(groupVisitors, "Use this group to grant people read permissions to the SharePoint site: " + site.Name.Trim(), SPRoleType.Reader);

                        newSite.Update();

                        Utility.ChangeSPGroupOwnerBySPGroup(newSite, groupOwners, groupOwners);
                        Utility.ChangeSPGroupOwnerBySPGroup(newSite, groupMembers, groupOwners);
                        Utility.ChangeSPGroupOwnerBySPGroup(newSite, groupVisitors, groupOwners);

                        SPUser authenUsers = newSite.EnsureUser("NT AUTHORITY\\authenticated users");
                        if (authenUsers != null)
                        {
                            SPGroup spGrp = newSite.Groups[groupVisitors];
                            if (spGrp != null)
                            {
                                spGrp.AddUser(authenUsers);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                    finally
                    {
                        newSite.AllowUnsafeUpdates = false;
                    }
                }


                //provision Top Navigation Bar
                try
                {
                    newSite.AllowUnsafeUpdates = true;

                    SPNavigationNodeCollection topnav = newSite.Navigation.TopNavigationBar;

                    //clear all
                    for (int i = topnav.Count - 1; i >= 0; i--)
                    {
                        topnav.Delete(topnav[i]);
                    }

                    string           linkTitle = newSite.Title;
                    SPNavigationNode node      = new SPNavigationNode(linkTitle, newSite.ServerRelativeUrl);
                    node = topnav.AddAsLast(node);

                    SPWeb            rootWeb       = web.Site.RootWeb;
                    string           linkRootTitle = rootWeb.Title;
                    SPNavigationNode nodeRoot      = new SPNavigationNode(linkRootTitle, rootWeb.ServerRelativeUrl);
                    nodeRoot = topnav.AddAsFirst(nodeRoot);

                    //AddParentNode(newSite, topnav);
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    newSite.AllowUnsafeUpdates = false;
                }

                if (site.SubSites.Count > 0)
                {
                    newSite.ProvisionWebStructure(site.SubSites);
                }
            }
        }