private void DeployDefinition(object modelHost, WebModelHost webModelHost, PageLayoutAndSiteTemplateSettingsDefinition definition)
        {
            var web = webModelHost.HostWeb;
            var publishingWeb = PublishingWeb.GetPublishingWeb(web);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model = null,
                EventType = ModelEventType.OnProvisioning,
                Object = web,
                ObjectType = typeof(SPWeb),
                ObjectDefinition = definition,
                ModelHost = modelHost
            });

            ProcessWebTemplateSettings(publishingWeb, definition);
            ProcessPageLayoutSettings(publishingWeb, definition);
            ProcessNewPageDefaultSettings(publishingWeb, definition);
            ProcessConverBlankSpacesIntoHyphenSetting(publishingWeb, definition);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model = null,
                EventType = ModelEventType.OnProvisioned,
                Object = web,
                ObjectType = typeof(SPWeb),
                ObjectDefinition = definition,
                ModelHost = modelHost
            });

            publishingWeb.Update();
        }
        public void CanDeploy_PageLayoutAndSiteTemplates_InheritEverything()
        {
            var siteFeature = BuiltInSiteFeatures.SharePointServerPublishingInfrastructure.Inherit(f => f.Enable());

            var webFeature = BuiltInWebFeatures.SharePointServerPublishing.Inherit(f => f.Enable());
            var subWebFeature = BuiltInWebFeatures.SharePointServerPublishing.Inherit(f => f.Enable());

            var settings = new PageLayoutAndSiteTemplateSettingsDefinition
            {
                InheritWebTemplates = true,
                InheritPageLayouts = true,
                InheritDefaultPageLayout = true,
                ConverBlankSpacesIntoHyphen = Rnd.Bool()
            };

            var siteModel = SPMeta2Model.NewSiteModel(site => site.AddSiteFeature(siteFeature));
            var webModel = SPMeta2Model.NewWebModel(web =>
            {
                web.AddWebFeature(webFeature);
                web.AddWeb(GetPublishingWeb(), subWeb =>
                {
                    subWeb.AddFeature(subWebFeature);
                    subWeb.AddPageLayoutAndSiteTemplateSettings(settings);
                });
            });

            TestModels(new[] { siteModel, webModel });
        }
        private void ProcessConverBlankSpacesIntoHyphenSetting(PublishingWeb publishingWeb, PageLayoutAndSiteTemplateSettingsDefinition definition)
        {
            var web = publishingWeb.Web;

            var key = "__AllowSpacesInNewPageName";
            var value = definition.ConverBlankSpacesIntoHyphen.HasValue
                ? definition.ConverBlankSpacesIntoHyphen.ToString()
                : false.ToString();

            if (!web.AllProperties.ContainsKey(key))
                web.AllProperties.Add(key, value);
            else
                web.AllProperties[key] = value;
        }
        private void ProcessConverBlankSpacesIntoHyphenSetting(
            WebModelHost webModelHost,
            PublishingWeb publishingWeb,
            PageLayoutAndSiteTemplateSettingsDefinition definition,
            List<ListItem> pageLayouts)
        {
            var web = publishingWeb.Web;

            var key = "__AllowSpacesInNewPageName";
            var value = definition.ConverBlankSpacesIntoHyphen.HasValue
                ? definition.ConverBlankSpacesIntoHyphen.ToString()
                : false.ToString();

            SetPropertyBagValue(web, key, value);
        }
        private void DeployDefinition(object modelHost, WebModelHost webModelHost, PageLayoutAndSiteTemplateSettingsDefinition definition)
        {
            var web = webModelHost.HostWeb;
            var context = web.Context;

            var publishingWeb = PublishingWeb.GetPublishingWeb(context, web);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model = null,
                EventType = ModelEventType.OnProvisioning,
                Object = web,
                ObjectType = typeof(Web),
                ObjectDefinition = definition,
                ModelHost = modelHost
            });

            var pageLayouts = LoadPageLayouts(webModelHost);

            ProcessWebTemplateSettings(webModelHost, publishingWeb, definition, pageLayouts);
            ProcessPageLayoutSettings(webModelHost, publishingWeb, definition, pageLayouts);
            ProcessNewPageDefaultSettings(webModelHost, publishingWeb, definition, pageLayouts);
            ProcessConverBlankSpacesIntoHyphenSetting(webModelHost, publishingWeb, definition, pageLayouts);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model = null,
                EventType = ModelEventType.OnProvisioned,
                Object = web,
                ObjectType = typeof(Web),
                ObjectDefinition = definition,
                ModelHost = modelHost
            });

            web.Update();
            context.ExecuteQueryWithTrace();
        }
        private static void ProcessNewPageDefaultSettings(PublishingWeb publishingWeb, PageLayoutAndSiteTemplateSettingsDefinition definition)
        {
            var web = publishingWeb.Web;

            if (definition.InheritDefaultPageLayout.HasValue && definition.InheritDefaultPageLayout.Value)
                publishingWeb.InheritDefaultPageLayout();
            else if (definition.UseDefinedDefaultPageLayout.HasValue && definition.UseDefinedDefaultPageLayout.Value)
            {
                var publishingSite = new PublishingSite(web.Site);
                var pageLayouts = publishingSite.PageLayouts;

                var selectedPageLayout = pageLayouts.FirstOrDefault(t => t.Name.ToUpper() == definition.DefinedDefaultPageLayout.ToUpper()); ;

                if (selectedPageLayout != null)
                {
                    publishingWeb.SetDefaultPageLayout(
                        selectedPageLayout,
                        definition.ResetAllSubsitesToInheritDefaultPageLayout.HasValue
                            ? definition.ResetAllSubsitesToInheritDefaultPageLayout.Value
                            : false);
                }
            }
        }
        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 static void ProcessWebTemplateSettings(
            WebModelHost webModelHost,
            PublishingWeb publishingWeb,
            PageLayoutAndSiteTemplateSettingsDefinition definition,
            List<ListItem> pageLayouts)
        {
            //var web = publishingWeb.Web;

            //if (definition.InheritWebTemplates.HasValue && definition.InheritWebTemplates.Value)
            //    publishingWeb.InheritAvailableWebTemplates();
            //else if (definition.UseAnyWebTemplate.HasValue && definition.UseAnyWebTemplate.Value)
            //{
            //    publishingWeb.AllowAllWebTemplates(definition.ResetAllSubsitesToInheritWebTemplates.HasValue
            //        ? definition.ResetAllSubsitesToInheritWebTemplates.Value
            //        : false);
            //}
            //else if (definition.UseDefinedWebTemplates.HasValue && definition.UseDefinedWebTemplates.Value)
            //{
            //    var currentLocaleId = (uint)web.CurrencyLocaleID;
            //    var webTemplates = new List<SPWebTemplate>();

            //    webTemplates.AddRange(web.Site.GetWebTemplates(currentLocaleId).OfType<SPWebTemplate>());
            //    webTemplates.AddRange(web.Site.GetCustomWebTemplates(currentLocaleId).OfType<SPWebTemplate>());

            //    var selectedWebTemplates = new Collection<SPWebTemplate>();

            //    foreach (var selectedWebTemplateName in definition.DefinedWebTemplates)
            //    {
            //        var targetWebTemplate =
            //            webTemplates.FirstOrDefault(t => t.Name.ToUpper() == selectedWebTemplateName.ToUpper());

            //        if (targetWebTemplate != null)
            //            selectedWebTemplates.Add(targetWebTemplate);
            //    }

            //    if (selectedWebTemplates.Any())
            //    {
            //        publishingWeb.SetAvailableWebTemplates(selectedWebTemplates, 0,
            //            definition.ResetAllSubsitesToInheritWebTemplates.HasValue
            //                ? definition.ResetAllSubsitesToInheritWebTemplates.Value
            //                : false);
            //    }
            //}
        }
        private static void ProcessPageLayoutSettings(
            WebModelHost webModelHost,
            PublishingWeb publishingWeb,
            PageLayoutAndSiteTemplateSettingsDefinition definition,
            List<ListItem> pageLayouts)
        {
            var rootWeb = webModelHost.HostSite.RootWeb;
            var web = publishingWeb.Web;
            var context = web.Context;

            if (definition.InheritPageLayouts.HasValue && definition.InheritPageLayouts.Value)
            {
                SetPropertyBagValue(web, "__PageLayouts", "__inherit");
            }
            else if (definition.UseAnyPageLayout.HasValue && definition.UseAnyPageLayout.Value)
            {
                SetPropertyBagValue(web, "__PageLayouts", "");
            }
            else if (definition.UseDefinedPageLayouts.HasValue && definition.UseDefinedPageLayouts.Value)
            {
                var selectedPageLayouts = new List<ListItem>();

                foreach (var selectedLayoutName in definition.DefinedPageLayouts)
                {
                    var targetLayout = pageLayouts.FirstOrDefault(t => t["FileLeafRef"].ToString().ToUpper() == selectedLayoutName.ToUpper());

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

                if (selectedPageLayouts.Any())
                {
                    var resultString = CreateLayoutsXmlString(selectedPageLayouts,rootWeb.ServerRelativeUrl);
                    SetPropertyBagValue(web, "__PageLayouts", resultString);
                }
            }
        }
        private static void ProcessNewPageDefaultSettings(
            WebModelHost webModelHost,
            PublishingWeb publishingWeb,
            PageLayoutAndSiteTemplateSettingsDefinition definition,
            List<ListItem> pageLayouts)
        {
            var web = publishingWeb.Web;
            var context = web.Context;

            if (definition.InheritDefaultPageLayout.HasValue && definition.InheritDefaultPageLayout.Value)
            {
                SetPropertyBagValue(web, "__DefaultPageLayout", "__inherit");
            }
            else if (definition.UseDefinedDefaultPageLayout.HasValue && definition.UseDefinedDefaultPageLayout.Value)
            {
                var selectedLayoutName = definition.DefinedDefaultPageLayout;
                var targetLayout = pageLayouts.FirstOrDefault(t => t["FileLeafRef"].ToString().ToUpper() == selectedLayoutName.ToUpper());

                if (targetLayout != null)
                {
                    var resultString = CreateLayoutXmlString(targetLayout, webModelHost.HostSite.RootWeb.ServerRelativeUrl);
                    SetPropertyBagValue(web, "__DefaultPageLayout", resultString);
                }
            }
        }
        private static void ProcessWebTemplateSettings(PublishingWeb publishingWeb, PageLayoutAndSiteTemplateSettingsDefinition definition)
        {
            var web = publishingWeb.Web;

            if (definition.InheritWebTemplates.HasValue && definition.InheritWebTemplates.Value)
                publishingWeb.InheritAvailableWebTemplates();
            else if (definition.UseAnyWebTemplate.HasValue && definition.UseAnyWebTemplate.Value)
            {
                publishingWeb.AllowAllWebTemplates(definition.ResetAllSubsitesToInheritWebTemplates.HasValue
                    ? definition.ResetAllSubsitesToInheritWebTemplates.Value
                    : false);
            }
            else if (definition.UseDefinedWebTemplates.HasValue && definition.UseDefinedWebTemplates.Value)
            {
                var currentLocaleId = (uint)web.CurrencyLocaleID;
                var webTemplates = new List<SPWebTemplate>();

                webTemplates.AddRange(web.Site.GetWebTemplates(currentLocaleId).OfType<SPWebTemplate>());
                webTemplates.AddRange(web.Site.GetCustomWebTemplates(currentLocaleId).OfType<SPWebTemplate>());

                var selectedWebTemplates = new Collection<SPWebTemplate>();

                foreach (var selectedWebTemplateName in definition.DefinedWebTemplates)
                {
                    var targetWebTemplate =
                        webTemplates.FirstOrDefault(t => t.Name.ToUpper() == selectedWebTemplateName.ToUpper());

                    if (targetWebTemplate != null)
                        selectedWebTemplates.Add(targetWebTemplate);
                }

                if (selectedWebTemplates.Any())
                {
                    publishingWeb.SetAvailableWebTemplates(selectedWebTemplates, 0,
                        definition.ResetAllSubsitesToInheritWebTemplates.HasValue
                            ? definition.ResetAllSubsitesToInheritWebTemplates.Value
                            : false);
                }
            }
        }
        public void CanDeploy_PageLayoutAndSiteTemplates_UseDefined()
        {
            var siteFeature = BuiltInSiteFeatures.SharePointServerPublishingInfrastructure.Inherit(f => f.Enable());

            var webFeature = BuiltInWebFeatures.SharePointServerPublishing.Inherit(f => f.Enable());
            var subWebFeature = BuiltInWebFeatures.SharePointServerPublishing.Inherit(f => f.Enable());

            var settings = new PageLayoutAndSiteTemplateSettingsDefinition
            {
                UseDefinedWebTemplates = true,
                DefinedWebTemplates = new Collection<string>
                {
                    BuiltInWebTemplates.Collaboration.BlankSite,
                    BuiltInWebTemplates.Collaboration.Blog,
                    BuiltInWebTemplates.Collaboration.TeamSite
                },

                UseDefinedPageLayouts = true,
                DefinedPageLayouts = new Collection<string>
                {
                    BuiltInPublishingPageLayoutNames.ArticleLeft,
                    BuiltInPublishingPageLayoutNames.ArticleRight,
                    BuiltInPublishingPageLayoutNames.ArticleLinks
                },

                UseDefinedDefaultPageLayout = true,
                DefinedDefaultPageLayout = BuiltInPublishingPageLayoutNames.ArticleRight,

                ConverBlankSpacesIntoHyphen = Rnd.Bool()
            };

            var siteModel = SPMeta2Model.NewSiteModel(site => site.AddSiteFeature(siteFeature));
            var webModel = SPMeta2Model.NewWebModel(web =>
            {
                web.AddWebFeature(webFeature);
                web.AddWeb(GetPublishingWeb(), subWeb =>
                {
                    subWeb.AddFeature(subWebFeature);
                    subWeb.AddPageLayoutAndSiteTemplateSettings(settings);
                });
            });

            TestModels(new[] { siteModel, webModel });
        }
 public static ModelNode AddPageLayoutAndSiteTemplateSettings(this ModelNode model, PageLayoutAndSiteTemplateSettingsDefinition definition, Action<ModelNode> action)
 {
     return model.AddDefinitionNode(definition, action);
 }
 public static ModelNode AddPageLayoutAndSiteTemplateSettings(this ModelNode model, PageLayoutAndSiteTemplateSettingsDefinition definition)
 {
     return AddPageLayoutAndSiteTemplateSettings(model, definition, null);
 }