private static void ProcessPageLayoutSettings(PublishingWeb publishingWeb, PageLayoutAndSiteTemplateSettingsDefinition definition)
        {
            var web = publishingWeb.Web;

            if (definition.InheritPageLayouts.HasValue && definition.InheritPageLayouts.Value)
            {
                publishingWeb.InheritAvailablePageLayouts();
            }
            else if (definition.UseAnyPageLayout.HasValue && definition.UseAnyPageLayout.Value)
            {
                publishingWeb.AllowAllPageLayouts(definition.ResetAllSubsitesToInheritPageLayouts.HasValue
                    ? definition.ResetAllSubsitesToInheritPageLayouts.Value
                    : false);
            }
            else if (definition.UseDefinedPageLayouts.HasValue && definition.UseDefinedPageLayouts.Value)
            {
                var publishingSite = new PublishingSite(web.Site);
                var pageLayouts    = publishingSite.PageLayouts;

                var selectedPageLayouts = new List <PageLayout>();

                foreach (var selectedLayoutName in definition.DefinedPageLayouts)
                {
                    var targetLayout = pageLayouts.FirstOrDefault(t => t.Name.ToUpper() == selectedLayoutName.ToUpper());

                    if (targetLayout != null)
                    {
                        selectedPageLayouts.Add(targetLayout);
                    }
                }

                if (selectedPageLayouts.Any())
                {
                    publishingWeb.SetAvailablePageLayouts(selectedPageLayouts.ToArray(),
                                                          definition.ResetAllSubsitesToInheritPageLayouts.HasValue
                            ? definition.ResetAllSubsitesToInheritPageLayouts.Value
                            : false);
                }
            }
        }
Esempio n. 2
0
        public static SPWebCollection removePageLayoutFromWeb(SPWeb oWeb, string pageLayoutToRemove)
        {
            PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(oWeb);

            Console.Write("\n Doing " + oWeb.Url);

            if (!pWeb.IsInheritingAvailablePageLayouts)
            {
                Console.Write("\n  Does not inherit. Searching...");

                List <PageLayout> myArray = new List <PageLayout>();


                var availablePageLayouts = pWeb.GetAvailablePageLayouts();
                Console.Write("\n  " + availablePageLayouts.Length.ToString() + " Page Layouts assoiciated with web.");
                if (availablePageLayouts.Length > 0)
                {
                    for (int i = 1; i < availablePageLayouts.Length; i++)
                    {
                        Console.Out.Flush();
                        string strapl = availablePageLayouts[i].Name;

                        if (strapl.Length == 0)
                        {
                            Console.Write("\nPage Layout without filename. Skipping " + availablePageLayouts[i].Title);
                        }
                        else
                        {
                            Console.Write("\n   Comparing " + strapl + " with " + pageLayoutToRemove);

                            if (strapl.CompareTo(pageLayoutToRemove) == 0)
                            {
                                var strrh = availablePageLayouts[i].Title;
                                Console.Write("\n   Removing page" + strapl + ". Press Enter to continue.");
                                Console.ReadLine();
                            }
                            else
                            {
                                Console.Write("\n   Keeping page" + strapl);
                                myArray.Add(availablePageLayouts[i]);
                            }
                        }
                    }

                    if (myArray.Count > 0)
                    {
                        Console.Write("\n  Updating Web");

                        pWeb.SetAvailablePageLayouts(myArray.ToArray(), false);
                        pWeb.Update();
                    }
                    else
                    {
                        Console.Write("\n  No pagelayouts to set. Skipping update. (At least one PageLayout must be left to do update).");
                    }
                }
            }
            else
            {
                Console.Write("\n  Inherits. Skipping.");
            }
            Console.Write("\n");
            return(oWeb.Webs);
        }
        private static void ProcessPageLayoutSettings(PublishingWeb publishingWeb, PageLayoutAndSiteTemplateSettingsDefinition definition)
        {
            var web = publishingWeb.Web;

            if (definition.InheritPageLayouts.HasValue && definition.InheritPageLayouts.Value)
                publishingWeb.InheritAvailablePageLayouts();
            else if (definition.UseAnyPageLayout.HasValue && definition.UseAnyPageLayout.Value)
            {
                publishingWeb.AllowAllPageLayouts(definition.ResetAllSubsitesToInheritPageLayouts.HasValue
                    ? definition.ResetAllSubsitesToInheritPageLayouts.Value
                    : false);
            }
            else if (definition.UseDefinedPageLayouts.HasValue && definition.UseDefinedPageLayouts.Value)
            {
                var publishingSite = new PublishingSite(web.Site);
                var pageLayouts = publishingSite.PageLayouts;

                var selectedPageLayouts = new List<PageLayout>();

                foreach (var selectedLayoutName in definition.DefinedPageLayouts)
                {
                    var targetLayout = pageLayouts.FirstOrDefault(t => t.Name.ToUpper() == selectedLayoutName.ToUpper());

                    if (targetLayout != null)
                        selectedPageLayouts.Add(targetLayout);
                }

                if (selectedPageLayouts.Any())
                {
                    publishingWeb.SetAvailablePageLayouts(selectedPageLayouts.ToArray(),
                        definition.ResetAllSubsitesToInheritPageLayouts.HasValue
                            ? definition.ResetAllSubsitesToInheritPageLayouts.Value
                            : false);
                }
            }
        }
        private void DefaultPageLayoutProcess(SPWebEventProperties properties)
        {
            PageLayout _pageLayout;

            try
            {
                using (SPSite site = new SPSite(properties.SiteId))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        PublishingSite pubSiteCollection = new PublishingSite(site);
                        PublishingWeb  publishingWeb     = PublishingWeb.GetPublishingWeb(properties.Web);

                        //check if pagelayout to be defaulted already exists in AvailablePageLayouts
                        _pageLayout = (from _pl in publishingWeb.GetAvailablePageLayouts()
                                       where _pl.Name == defaultPageLayout
                                       select _pl).FirstOrDefault();

                        //if exists
                        if (_pageLayout != null)
                        {
                            publishingWeb.SetDefaultPageLayout(_pageLayout, true);
                            publishingWeb.Update();
                        }
                        else  //if does not exist
                        {
                            //get all AvailablePageLayouts
                            PageLayout[] _allpageLayout = publishingWeb.GetAvailablePageLayouts();
                            PageLayout[] plarray        = new PageLayout[_allpageLayout.Length + 1];
                            int          ipl            = -1;
                            //transfer existing pagelayouts in AvailablePageLayouts to PageLayout[]
                            foreach (PageLayout _itempl in _allpageLayout)
                            {
                                ipl++;
                                plarray[ipl] = _itempl;
                            }

                            //PageLayout to be defaulted to
                            _pageLayout = pubSiteCollection.PageLayouts["/_catalogs/masterpage/" + defaultPageLayout];
                            ipl++;
                            //add to the PageLayout array
                            plarray[ipl] = _pageLayout;
                            //reset AvailablePageLayouts
                            publishingWeb.SetAvailablePageLayouts(plarray, true);
                            publishingWeb.Update();
                            //set DefaultPageLayout
                            publishingWeb.SetDefaultPageLayout(_pageLayout, true);

                            publishingWeb.Update();
                            web.Update();
                        }

                        //Swap the page layout of the default.aspx page
                        SwapPageLayout(publishingWeb, _pageLayout, web.Site.RootWeb.ContentTypes[_pageLayout.AssociatedContentType.Id]);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.WriteLog("WebProvisioning.cs - DefaultPageLayoutProcess: " + ex.Message + " " + ex.StackTrace);
            }
        }