protected WebNavigationSettings GetWebNavigationSettings(WebModelHost webModelHost, WebNavigationSettingsDefinition navigationModel)
        {
            var web = webModelHost.HostWeb;
            var thisWebNavSettings = new WebNavigationSettings(web);

            return thisWebNavSettings;
        }
        protected WebNavigationSettings GetWebNavigationSettings(WebModelHost webModelHost, WebNavigationSettingsDefinition navigationModel)
        {
            var web = webModelHost.HostWeb;

            var context = web.Context;
            var thisWebNavSettings = new WebNavigationSettings(context, web);

            context.Load(thisWebNavSettings);
            context.ExecuteQueryWithTrace();

            return thisWebNavSettings;
        }
        protected int? GetGlobalNavigationIncludeTypes(WebNavigationSettingsDefinition navigationModel)
        {
            int? currentNavigationIncludeTypes = null;

            if (navigationModel.CurrentNavigationShowPages == false
                && navigationModel.CurrentNavigationShowSubsites == false)
                currentNavigationIncludeTypes = 0;
            else if (navigationModel.CurrentNavigationShowPages == true
                && navigationModel.CurrentNavigationShowSubsites == true)
                currentNavigationIncludeTypes = 3;
            else if (navigationModel.CurrentNavigationShowPages == true)
                currentNavigationIncludeTypes = 2;
            else if (navigationModel.CurrentNavigationShowSubsites == true)
                currentNavigationIncludeTypes = 1;

            return currentNavigationIncludeTypes;
        }
        public void CanDeploy_WebNavigationSettings_As_HideGlobalNavigation()
        {
            var siteModel = SPMeta2Model.NewSiteModel(site =>
            {
                site.AddSiteFeature(BuiltInSiteFeatures.SharePointServerPublishingInfrastructure.Inherit(def =>
                {
                    def.Enable = true;
                }));

            });

            var navigationDef = new WebNavigationSettingsDefinition()
            {
                GlobalNavigationSource = BuiltInStandardNavigationSources.PortalProvider,

                GlobalNavigationShowSubsites = false,
                GlobalNavigationShowPages = false,
            };

            var sunWebNavigationDef = navigationDef.Inherit();

            var webModel = SPMeta2Model.NewWebModel(web =>
            {
                web.AddWebFeature(BuiltInWebFeatures.SharePointServerPublishing.Inherit(def =>
                {
                    def.Enable = true;
                }));

                web.AddWebNavigationSettings(navigationDef);

                web.AddRandomWeb(subWeb =>
                {
                    web.AddWebFeature(BuiltInWebFeatures.SharePointServerPublishing.Inherit(def =>
                    {
                        def.Enable = true;
                    }));

                    subWeb.AddWebNavigationSettings(sunWebNavigationDef);
                });
            });

            TestModel(siteModel, webModel);
        }
        private void DeployNavigationSettings(object modelHost, WebModelHost webModelHost, WebNavigationSettingsDefinition navigationModel)
        {
            var shouldUpdateWeb = false;

            var site = webModelHost.HostSite;
            var web = webModelHost.HostWeb;

            var context = web.Context;
            var allProperties = web.AllProperties;

            context.Load(allProperties);

            // the GetWebNavigationSettings will call ExecuteQueryWithTrace
            var thisWebNavSettings = GetWebNavigationSettings(webModelHost, navigationModel);

            StandardNavigationSource? globalSource = null;
            StandardNavigationSource? currentSource = null;

            if (!string.IsNullOrEmpty(navigationModel.GlobalNavigationSource))
                globalSource = (StandardNavigationSource)Enum.Parse(typeof(StandardNavigationSource), navigationModel.GlobalNavigationSource);

            if (!string.IsNullOrEmpty(navigationModel.CurrentNavigationSource))
                currentSource = (StandardNavigationSource)Enum.Parse(typeof(StandardNavigationSource), navigationModel.CurrentNavigationSource);

            TermStore currentTermStore = null;
            TermSet currentTermSet = null;

            if (currentSource == StandardNavigationSource.TaxonomyProvider)
            {
                currentTermStore = TaxonomyTermStoreModelHandler.FindTermStore(site,
                           navigationModel.CurrentNavigationTermStoreName,
                           navigationModel.CurrentNavigationTermStoreId,
                           navigationModel.CurrentNavigationUseDefaultSiteCollectionTermStore);

                currentTermSet = TaxonomyFieldModelHandler.LookupTermSet(currentTermStore.Context,
                  currentTermStore,
                  site,
                  null,
                  null,
                  null,
                  navigationModel.CurrentNavigationTermSetName,
                  navigationModel.CurrentNavigationTermSetId,
                  navigationModel.CurrentNavigationTermSetLCID);
            }

            TermStore globalTermStore = null;
            TermSet globalTermSet = null;

            if (globalSource == StandardNavigationSource.TaxonomyProvider)
            {
                globalTermStore = TaxonomyTermStoreModelHandler.FindTermStore(site,
                          navigationModel.GlobalNavigationTermStoreName,
                          navigationModel.GlobalNavigationTermStoreId,
                          navigationModel.GlobalNavigationUseDefaultSiteCollectionTermStore);

                globalTermSet = TaxonomyFieldModelHandler.LookupTermSet(site.Context,
                  globalTermStore,
                  site,
                  null,
                  null,
                  null,
                  navigationModel.GlobalNavigationTermSetName,
                  navigationModel.GlobalNavigationTermSetId,
                  navigationModel.GlobalNavigationTermSetLCID);
            }

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

            if (!string.IsNullOrEmpty(navigationModel.GlobalNavigationSource) ||
                !string.IsNullOrEmpty(navigationModel.CurrentNavigationSource))
            {
                if (globalSource.HasValue)
                {
                    TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Setting GlobalNavigation to: [{0}]", navigationModel.GlobalNavigationSource);

                    thisWebNavSettings.GlobalNavigation.Source = globalSource.Value;

                    if (globalTermStore != null)
                    {
                        thisWebNavSettings.GlobalNavigation.TermStoreId = globalTermStore.Id;
                        thisWebNavSettings.GlobalNavigation.TermSetId = globalTermSet.Id;
                    }
                    else
                    {
                        var value = allProperties.FieldValues.ContainsKey(BuiltInWebPropertyId.GlobalNavigationIncludeTypes)
                                        ? allProperties[BuiltInWebPropertyId.GlobalNavigationIncludeTypes]
                                        : string.Empty;

                        int? globalNavigationIncludeTypes = GetGlobalNavigationIncludeTypes(
                                    navigationModel,
                                    ConvertUtils.ToInt(value));

                        if (globalNavigationIncludeTypes != null)
                        {
                            TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Setting GlobalNavigationIncludeTypes to: [{0}]", globalNavigationIncludeTypes);
                            allProperties[BuiltInWebPropertyId.GlobalNavigationIncludeTypes] = globalNavigationIncludeTypes.Value;

                            shouldUpdateWeb = true;
                        }

                        if (navigationModel.GlobalNavigationMaximumNumberOfDynamicItems.HasValue)
                        {
                            TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Setting GlobalDynamicChildLimit to: [{0}]", navigationModel.GlobalNavigationMaximumNumberOfDynamicItems.Value);
                            allProperties[BuiltInWebPropertyId.GlobalDynamicChildLimit] = navigationModel.GlobalNavigationMaximumNumberOfDynamicItems.Value;

                            shouldUpdateWeb = true;
                        }
                    }
                }

                if (currentSource.HasValue)
                {
                    TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall,
                        "Setting CurrentNavigation to: [{0}]", navigationModel.CurrentNavigationSource);

                    thisWebNavSettings.CurrentNavigation.Source = currentSource.Value;

                    if (currentTermStore != null)
                    {
                        thisWebNavSettings.CurrentNavigation.TermStoreId = currentTermStore.Id;
                        thisWebNavSettings.CurrentNavigation.TermSetId = currentTermSet.Id;
                    }
                    else
                    {
                        var value = allProperties.FieldValues.ContainsKey(BuiltInWebPropertyId.CurrentNavigationIncludeTypes)
                                        ? allProperties[BuiltInWebPropertyId.CurrentNavigationIncludeTypes]
                                        : string.Empty;


                        int? currentNavigationIncludeTypes = GetCurrentNavigationIncludeTypes(
                            navigationModel,
                            ConvertUtils.ToInt(value));

                        if (currentNavigationIncludeTypes != null)
                        {
                            TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Setting CurrentNavigationIncludeTypes to: [{0}]", currentNavigationIncludeTypes);
                            allProperties[BuiltInWebPropertyId.CurrentNavigationIncludeTypes] = currentNavigationIncludeTypes.Value;

                            shouldUpdateWeb = true;
                        }

                        if (navigationModel.CurrentNavigationMaximumNumberOfDynamicItems.HasValue)
                        {
                            TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Setting CurrentDynamicChildLimit to: [{0}]", navigationModel.CurrentNavigationMaximumNumberOfDynamicItems.Value);
                            allProperties[BuiltInWebPropertyId.CurrentDynamicChildLimit] = navigationModel.CurrentNavigationMaximumNumberOfDynamicItems.Value;

                            shouldUpdateWeb = true;
                        }
                    }
                }

            }

            if (navigationModel.DisplayShowHideRibbonAction.HasValue)
            {
                allProperties["__DisplayShowHideRibbonActionId"] = navigationModel.DisplayShowHideRibbonAction.ToString();
                shouldUpdateWeb = true;
            }

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

            if (!string.IsNullOrEmpty(navigationModel.GlobalNavigationSource) ||
                !string.IsNullOrEmpty(navigationModel.CurrentNavigationSource))
            {
                TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Updating navigation settings");

                web.Update();
                thisWebNavSettings.Update(null);
                shouldUpdateWeb = true;
            }

            if (shouldUpdateWeb)
            {
                TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Updating web");
                web.Update();
                context.ExecuteQueryWithTrace();
            }
        }
        protected int? GetCurrentNavigationIncludeTypes(
            WebNavigationSettingsDefinition navigationModel,
            int? navigationIncludeTypes)
        {
            if (navigationModel.CurrentNavigationShowPages.HasValue
               || navigationModel.CurrentNavigationShowSubsites.HasValue)
            {
                if (!navigationIncludeTypes.HasValue)
                    navigationIncludeTypes = 2;
            }

            if (navigationModel.CurrentNavigationShowPages.HasValue)
            {
                if (navigationModel.CurrentNavigationShowPages.Value)
                {
                    navigationIncludeTypes |= 2;
                }
                else
                {
                    navigationIncludeTypes &= ~2;
                }
            }

            if (navigationModel.CurrentNavigationShowSubsites.HasValue)
            {
                if (navigationModel.CurrentNavigationShowSubsites.Value)
                {
                    navigationIncludeTypes |= 1;
                }
                else
                {
                    navigationIncludeTypes &= ~1;
                }
            }

            return navigationIncludeTypes;
        }
        private void DeployNavigationSettings(object modelHost, WebModelHost webModelHost, WebNavigationSettingsDefinition navigationModel)
        {
            var site = webModelHost.HostWeb.Site;
            var web = webModelHost.HostWeb;
            var thisWebNavSettings = GetWebNavigationSettings(webModelHost, navigationModel);

            var shouldUpdateWeb = false;
            var allProperties = web.AllProperties;

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

            if (!string.IsNullOrEmpty(navigationModel.GlobalNavigationSource) ||
                !string.IsNullOrEmpty(navigationModel.CurrentNavigationSource))
            {
                if (!string.IsNullOrEmpty(navigationModel.GlobalNavigationSource))
                {
                    var globalSource = (StandardNavigationSource)Enum.Parse(typeof(StandardNavigationSource), navigationModel.GlobalNavigationSource);

                    thisWebNavSettings.GlobalNavigation.Source = globalSource;

                    if (globalSource == StandardNavigationSource.TaxonomyProvider)
                    {
                        var globalTermStore = TaxonomyTermStoreModelHandler.FindTermStore(site,
                           navigationModel.GlobalNavigationTermStoreName,
                           navigationModel.GlobalNavigationTermStoreId,
                           navigationModel.GlobalNavigationUseDefaultSiteCollectionTermStore);

                        var globalTermSet = TaxonomyFieldModelHandler.LookupTermSet(globalTermStore,
                          null, null, null, null,
                          navigationModel.GlobalNavigationTermSetName,
                          navigationModel.GlobalNavigationTermSetId,
                          navigationModel.GlobalNavigationTermSetLCID);

                        thisWebNavSettings.GlobalNavigation.TermStoreId = globalTermStore.Id;
                        thisWebNavSettings.GlobalNavigation.TermSetId = globalTermSet.Id;
                    }
                    else
                    {
                        int? globalNavigationIncludeTypes = GetGlobalNavigationIncludeTypes(navigationModel,
                          ConvertUtils.ToInt(allProperties[BuiltInWebPropertyId.GlobalNavigationIncludeTypes]));

                        if (globalNavigationIncludeTypes != null)
                        {
                            allProperties[BuiltInWebPropertyId.GlobalNavigationIncludeTypes] = globalNavigationIncludeTypes.Value;
                            shouldUpdateWeb = true;
                        }

                        if (navigationModel.GlobalNavigationMaximumNumberOfDynamicItems.HasValue)
                        {
                            allProperties[BuiltInWebPropertyId.GlobalDynamicChildLimit] = navigationModel.GlobalNavigationMaximumNumberOfDynamicItems.Value;
                            shouldUpdateWeb = true;
                        }
                    }
                }

                if (!string.IsNullOrEmpty(navigationModel.CurrentNavigationSource))
                {
                    var currentSource = (StandardNavigationSource)Enum.Parse(typeof(StandardNavigationSource), navigationModel.CurrentNavigationSource);

                    thisWebNavSettings.CurrentNavigation.Source = currentSource;

                    if (currentSource == StandardNavigationSource.TaxonomyProvider)
                    {
                        var currentTermStore = TaxonomyTermStoreModelHandler.FindTermStore(site,
                          navigationModel.CurrentNavigationTermStoreName,
                          navigationModel.CurrentNavigationTermStoreId,
                          navigationModel.CurrentNavigationUseDefaultSiteCollectionTermStore);

                        var currentTermSet = TaxonomyFieldModelHandler.LookupTermSet(currentTermStore,
                          null, null, null, null,
                          navigationModel.CurrentNavigationTermSetName,
                          navigationModel.CurrentNavigationTermSetId,
                          navigationModel.CurrentNavigationTermSetLCID);

                        thisWebNavSettings.CurrentNavigation.TermStoreId = currentTermStore.Id;
                        thisWebNavSettings.CurrentNavigation.TermSetId = currentTermSet.Id;
                    }
                    else
                    {
                        int? currentNavigationIncludeTypes = GetCurrentNavigationIncludeTypes(navigationModel,
                           ConvertUtils.ToInt(allProperties[BuiltInWebPropertyId.CurrentNavigationIncludeTypes]));

                        if (currentNavigationIncludeTypes != null)
                        {
                            allProperties[BuiltInWebPropertyId.CurrentNavigationIncludeTypes] = currentNavigationIncludeTypes.Value;
                            shouldUpdateWeb = true;
                        }

                        if (navigationModel.CurrentNavigationMaximumNumberOfDynamicItems.HasValue)
                        {
                            allProperties[BuiltInWebPropertyId.CurrentDynamicChildLimit] = navigationModel.CurrentNavigationMaximumNumberOfDynamicItems.Value;
                            shouldUpdateWeb = true;
                        }
                    }
                }
            }

            if (navigationModel.DisplayShowHideRibbonAction.HasValue)
            {
                allProperties["__DisplayShowHideRibbonActionId"] = navigationModel.DisplayShowHideRibbonAction.ToString();
                shouldUpdateWeb = true;
            }

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

            if (shouldUpdateWeb)
                web.Update();

            if (!string.IsNullOrEmpty(navigationModel.GlobalNavigationSource) ||
                !string.IsNullOrEmpty(navigationModel.CurrentNavigationSource))
            {
                thisWebNavSettings.Update(null);
            }


        }
        protected int? GetCurrentNavigationIncludeTypes(WebNavigationSettingsDefinition navigationModel,
            int? navigationIncludeTypes)
        {
            if (navigationModel.CurrentNavigationShowPages.HasValue
               || navigationModel.CurrentNavigationShowSubsites.HasValue)
            {
                if (!navigationIncludeTypes.HasValue)
                    navigationIncludeTypes = 2;
            }

            if (navigationModel.CurrentNavigationShowPages.HasValue)
            {
                if (navigationModel.CurrentNavigationShowPages.Value)
                {
                    navigationIncludeTypes |= 2;
                }
                else
                {
                    navigationIncludeTypes &= ~2;
                }
            }

            if (navigationModel.CurrentNavigationShowSubsites.HasValue)
            {
                if (navigationModel.CurrentNavigationShowSubsites.Value)
                {
                    navigationIncludeTypes |= 1;
                }
                else
                {
                    navigationIncludeTypes &= ~1;
                }
            }

            //if (navigationModel.CurrentNavigationShowPages == false
            //    && navigationModel.CurrentNavigationShowSubsites == false)__GlobalNavigationIncludeTypes
            //    currentNavigationIncludeTypes = 0;
            //else if (navigationModel.CurrentNavigationShowPages == true
            //    && navigationModel.CurrentNavigationShowSubsites == true)
            //    currentNavigationIncludeTypes = 3;
            //else if (navigationModel.CurrentNavigationShowPages == true)
            //    currentNavigationIncludeTypes = 2;
            //else if (navigationModel.CurrentNavigationShowSubsites == true)
            //    currentNavigationIncludeTypes = 1;

            return navigationIncludeTypes;
        }
 public static ModelNode AddWebNavigationSettings(this ModelNode model, WebNavigationSettingsDefinition definition, Action<ModelNode> action)
 {
     return model.AddDefinitionNode(definition, action);
 }
 public static ModelNode AddWebNavigationSettings(this ModelNode model, WebNavigationSettingsDefinition definition)
 {
     return AddWebNavigationSettings(model, definition, null);
 }