Example #1
0
        /// <summary>
        /// Reset web navigation to its default configuration. Disabled the term set as navigation term set.
        /// </summary>
        /// <param name="web">The web</param>
        /// <param name="settings">The managed navigation settings. Set null if you want to keep the associated termset unchanged</param>
        public void ResetWebNavigationToDefault(SPWeb web, ManagedNavigationInfo settings)
        {
            var taxonomySession = new TaxonomySession(web.Site);

            if (taxonomySession.TermStores.Count > 0)
            {
                if (settings != null)
                {
                    // Disable the navigation flag on the the term set
                    var termStore = taxonomySession.DefaultSiteCollectionTermStore;
                    var group     = this.taxonomyService.GetTermGroupFromStore(termStore, settings.TermGroup.Name);
                    var termSet   = this.taxonomyService.GetTermSetFromGroup(termStore, group, settings.TermSet.Label);

                    const string PropertyName = "_Sys_Nav_IsNavigationTermSet";
                    string       propertyValue;
                    if (termSet.CustomProperties.TryGetValue(PropertyName, out propertyValue))
                    {
                        termSet.DeleteCustomProperty(PropertyName);
                        termSet.TermStore.CommitAll();
                    }
                }

                var navigationSettings = new WebNavigationSettings(web);
                navigationSettings.ResetToDefaults();
                navigationSettings.Update(taxonomySession);
            }
        }
Example #2
0
        /// <summary>
        /// Sets the web navigation settings.
        /// </summary>
        /// <param name="web">The web.</param>
        /// <param name="settings">The settings.</param>
        public void SetWebNavigationSettings(SPWeb web, ManagedNavigationInfo settings)
        {
            var taxonomySession = new TaxonomySession(web.Site);

            if (taxonomySession.TermStores.Count > 0)
            {
                var termStore = settings.TermSet.ResolveParentTermStore(taxonomySession);
                var group     = settings.TermSet.ResolveParentGroup(taxonomySession, web.Site);
                var termSet   = this.taxonomyService.GetTermSetFromGroup(termStore, group, settings.TermSet.Label);

                // Flag the term set as a navigation term set
                termSet.SetCustomProperty(SystemIsNavigationTermSet, "True");
                termStore.CommitAll();

                var navigationSettings = new WebNavigationSettings(web);

                navigationSettings.GlobalNavigation.Source      = StandardNavigationSource.TaxonomyProvider;
                navigationSettings.GlobalNavigation.TermStoreId = termStore.Id;
                navigationSettings.GlobalNavigation.TermSetId   = termSet.Id;

                navigationSettings.CurrentNavigation.Source      = StandardNavigationSource.TaxonomyProvider;
                navigationSettings.CurrentNavigation.TermStoreId = termStore.Id;
                navigationSettings.CurrentNavigation.TermSetId   = termSet.Id;

                navigationSettings.AddNewPagesToNavigation       = settings.AddNewPagesToNavigation;
                navigationSettings.CreateFriendlyUrlsForNewPages = settings.CreateFriendlyUrlsForNewsPages;
                navigationSettings.Update(taxonomySession);

                if (settings.PreserveTaggingOnTermSet)
                {
                    termSet.IsAvailableForTagging = true;
                    termStore.CommitAll();
                }
            }
        }
Example #3
0
        /// <summary>
        /// Reset web navigation to its default configuration. Disabled the term set as navigation term set.
        /// </summary>
        /// <param name="web">The web</param>
        /// <param name="settings">The managed navigation settings. Set null if you want to keep the associated termset unchanged</param>
        public void ResetWebNavigationToDefault(SPWeb web, ManagedNavigationInfo settings)
        {
            var taxonomySession = new TaxonomySession(web.Site);

            if (taxonomySession.TermStores.Count > 0)
            {
                if (settings != null)
                {
                    // Disable the navigation flag on the the term set
                    var termStore = settings.TermSet.ResolveParentTermStore(taxonomySession);
                    var group     = settings.TermSet.ResolveParentGroup(taxonomySession, web.Site);
                    var termSet   = this.taxonomyService.GetTermSetFromGroup(termStore, group, settings.TermSet.Label);

                    string propertyValue;
                    if (termSet.CustomProperties.TryGetValue(SystemIsNavigationTermSet, out propertyValue))
                    {
                        termSet.DeleteCustomProperty(SystemIsNavigationTermSet);
                        termStore.CommitAll();
                    }
                }

                var navigationSettings = new WebNavigationSettings(web);
                navigationSettings.ResetToDefaults();
                navigationSettings.Update(taxonomySession);
            }
        }
        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 thisWebNavSettings = new WebNavigationSettings(web);

            return(thisWebNavSettings);
        }
Example #6
0
        /// <summary>
        /// Determines whether the current Web has the managed navigation enabled
        /// </summary>
        /// <param name="web">The target web.</param>
        /// <param name="navigationKind">The kind of navigation (Current or Global).</param>
        /// <returns>A boolean result of the test.</returns>
        public static bool IsManagedNavigationEnabled(this Web web, ManagedNavigationKind navigationKind)
        {
            var result             = false;
            var navigationSettings = new WebNavigationSettings(web.Context, web);

            web.Context.Load(navigationSettings, ns => ns.CurrentNavigation, ns => ns.GlobalNavigation);
            web.Context.Load(web.ParentWeb, pw => pw.ServerRelativeUrl);
            web.Context.ExecuteQueryRetry();

            var targetNavigationSettings =
                navigationKind == ManagedNavigationKind.Current ?
                navigationSettings.CurrentNavigation : navigationSettings.GlobalNavigation;

            if (targetNavigationSettings.Source == StandardNavigationSource.InheritFromParentWeb &&
                !web.ParentWeb.ServerObjectIsNull())
            {
                var currentWebUri = new Uri(web.Url);
                var parentWebUri  = new Uri($"{currentWebUri.Scheme}://{currentWebUri.Host}{web.ParentWeb.ServerRelativeUrl}");

                using (var parentContext = web.Context.Clone(parentWebUri))
                {
                    result = IsManagedNavigationEnabled(parentContext.Web, navigationKind);
                }
            }
            else
            {
                result = targetNavigationSettings.Source == StandardNavigationSource.TaxonomyProvider;
            }

            return(result);
        }
        /// <summary>
        /// Determines whether the current Web has the managed navigation enabled
        /// </summary>
        /// <param name="web">The target web.</param>
        /// <param name="navigationKind">The kind of navigation (Current or Global).</param>
        /// <returns>A boolean result of the test.</returns>
        public static Boolean IsManagedNavigationEnabled(this Web web, ManagedNavigationKind navigationKind)
        {
            Boolean result             = false;
            var     navigationSettings = new WebNavigationSettings(web.Context, web);

            web.Context.Load(navigationSettings, ns => ns.CurrentNavigation, ns => ns.GlobalNavigation);
            web.Context.Load(web.ParentWeb, pw => pw.ServerRelativeUrl);
            web.Context.ExecuteQueryRetry();

            StandardNavigationSettings targeNavigationSettings =
                navigationKind == ManagedNavigationKind.Current ?
                navigationSettings.CurrentNavigation : navigationSettings.GlobalNavigation;

            if (targeNavigationSettings.Source == StandardNavigationSource.InheritFromParentWeb &&
                (web.ParentWeb.ServerObjectIsNull.HasValue && !web.ParentWeb.ServerObjectIsNull.Value))
            {
                Uri currentWebUri = new Uri(web.Url);
                Uri parentWebUri  = new Uri(String.Format("{0}://{1}{2}", currentWebUri.Scheme, currentWebUri.Host, web.ParentWeb.ServerRelativeUrl));

                using (ClientContext parentContext = web.Context.Clone(parentWebUri))
                {
                    result = IsManagedNavigationEnabled(parentContext.Web, navigationKind);
                }
            }
            else
            {
                result = targeNavigationSettings.Source == StandardNavigationSource.TaxonomyProvider;
            }

            return(result);
        }
        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;
        }
Example #9
0
        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);
        }
Example #10
0
        private StructuralNavigation GetStructuralNavigation(Web web, WebNavigationSettings navigationSettings, Enums.NavigationType navigationType, ProvisioningTemplate template, ProvisioningTemplateCreationInformation creationInfo)
        {
            // By default avoid removing existing nodes
            var result = new StructuralNavigation {
                RemoveExistingNodes = false
            };

            Microsoft.SharePoint.Client.NavigationNodeCollection sourceNodes = navigationType == Enums.NavigationType.QuickLaunch ?
                                                                               web.Navigation.QuickLaunch : navigationType == Enums.NavigationType.TopNavigationBar ? web.Navigation.TopNavigationBar : web.LoadSearchNavigation();

            if (sourceNodes == null)
            {
                return(result);
            }

            var clientContext = web.Context;

            clientContext.Load(web, w => w.ServerRelativeUrl, w => w.Language);
            clientContext.Load(sourceNodes);
            clientContext.ExecuteQueryRetry();
            var defaultCulture = new CultureInfo((int)web.Language);

            if (!sourceNodes.ServerObjectIsNull.Value)
            {
                result.NavigationNodes.AddRange(from n in sourceNodes.AsEnumerable()
                                                select n.ToDomainModelNavigationNode(web, creationInfo.PersistMultiLanguageResources, defaultCulture));

                if (creationInfo.PersistMultiLanguageResources)
                {
                    //need to get nodes and children for any other then default language
                    foreach (var language in template.SupportedUILanguages.Where(c => c.LCID != defaultCulture.LCID))
                    {
                        var currentCulture = new CultureInfo(language.LCID);
                        clientContext.Load(web, w => w.ServerRelativeUrl);
                        clientContext.Load(sourceNodes);
                        var acceptLanguage = clientContext.PendingRequest.RequestExecutor.WebRequest.Headers["Accept-Language"];
                        clientContext.PendingRequest.RequestExecutor.WebRequest.Headers["Accept-Language"] = currentCulture.Name;
                        clientContext.ExecuteQueryRetry();

                        if (!sourceNodes.ServerObjectIsNull.Value)
                        {
                            //we dont need to add to result - just extract Titles - to List as we need to
                            var alternateLang = (from n in sourceNodes.AsEnumerable()
                                                 select n.ToDomainModelNavigationNode(web, creationInfo.PersistMultiLanguageResources, currentCulture)).ToList();
                        }

                        clientContext.PendingRequest.RequestExecutor.WebRequest.Headers["Accept-Language"] = acceptLanguage;
                    }
                }
            }
            return(result);
        }
        static void SetManagedNavigation(SPSite site, string groupName, string termSetName)
        {
            TaxonomySession       session   = new TaxonomySession(site);
            TermStore             termStore = session.TermStores["Managed Metadata Service"];
            Group                 group     = termStore.Groups[groupName];
            TermSet               termSet   = group.TermSets[termSetName];
            WebNavigationSettings settings  = new WebNavigationSettings(site.RootWeb);

            settings.GlobalNavigation.Source      = StandardNavigationSource.TaxonomyProvider;
            settings.GlobalNavigation.TermStoreId = termStore.Id;
            settings.GlobalNavigation.TermSetId   = termSet.Id;
            settings.Update();
        }
        private ManagedNavigation GetManagedNavigation(Web web, WebNavigationSettings navigationSettings, Boolean currentNavigation)
        {
            var result = new ManagedNavigation
            {
                TermStoreId = currentNavigation ? navigationSettings.CurrentNavigation.TermStoreId.ToString() : navigationSettings.GlobalNavigation.TermStoreId.ToString(),
                TermSetId   = currentNavigation ? navigationSettings.CurrentNavigation.TermSetId.ToString() : navigationSettings.GlobalNavigation.TermSetId.ToString(),
            };

            // Apply any token replacement for taxonomy IDs
            TokenizeManagedNavigationTaxonomyIds(web, result);

            return(result);
        }
Example #13
0
    public void ConfigureTaxonomyNavigation()
    {
        using (SPSite site = new SPSite(TestConfig.ServerUrl))
        {
            using (SPWeb web = site.OpenWeb())
            {
                TaxonomySession taxonomySession = new TaxonomySession(site, updateCache: true);

                NavigationTermSet termSet = DemoUtilities.RecreateSampleNavTermSet(
                    this.TestContext, taxonomySession, web);

                // Clear any old settings.
                WebNavigationSettings webNavigationSettings = new WebNavigationSettings(web);
                webNavigationSettings.ResetToDefaults();
                webNavigationSettings.Update(taxonomySession);

                TaxonomyNavigation.FlushSiteFromCache(site);

                this.WaitForSync();

                // Verify the TermSet object is not running.
                NavigationTermSet actualTermSet;

                actualTermSet = TaxonomyNavigation.GetTermSetForWeb(web,
                                                                    StandardNavigationProviderNames.GlobalNavigationTaxonomyProvider,
                                                                    includeInheritedSettings: true);

                Assert.IsTrue(actualTermSet == null);

                // Assign the new settings.
                webNavigationSettings = new WebNavigationSettings(web);

                // GlobalNavigation = top menu (aka "top nav")
                // CurrentNavigation = left menu (aka "quick launch")
                webNavigationSettings.GlobalNavigation.Source      = StandardNavigationSource.TaxonomyProvider;
                webNavigationSettings.GlobalNavigation.TermStoreId = termSet.TermStoreId;
                webNavigationSettings.GlobalNavigation.TermSetId   = termSet.Id;
                webNavigationSettings.Update(taxonomySession);

                TaxonomyNavigation.FlushSiteFromCache(site);

                this.WaitForSync();

                actualTermSet = TaxonomyNavigation.GetTermSetForWeb(web,
                                                                    StandardNavigationProviderNames.GlobalNavigationTaxonomyProvider,
                                                                    includeInheritedSettings: true);

                Assert.AreEqual(termSet.Id, actualTermSet.Id);
            }
        }
    }
Example #14
0
        private static WebNavigationSettings FindTaxonomyWebNavigationSettingsInWebOrInParents(SPWeb web, out SPWeb webWithNavigationSettings)
        {
            var currentWebNavSettings = new WebNavigationSettings(web);

            webWithNavigationSettings = web;

            if (currentWebNavSettings.GlobalNavigation.Source == StandardNavigationSource.InheritFromParentWeb &&
                web.ParentWeb != null)
            {
                // current web inherits its settings from its parent, so we gotta look upwards to the parent webs
                // recursively until we find a match
                return(FindTaxonomyWebNavigationSettingsInWebOrInParents(web.ParentWeb, out webWithNavigationSettings));
            }

            return(currentWebNavSettings);
        }
Example #15
0
        // Retrieved target xml data is not matching with source xml navigation types so changing navigation settings to get correct data.
        public void ChangeNavigationSettings(ClientContext cc, StandardNavigationSource gSource, StandardNavigationSource cSource)
        {
            TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(cc);

            taxonomySession.UpdateCache();
            cc.Load(taxonomySession, ts => ts.TermStores);
            cc.ExecuteQueryRetry();

            var navigationSettings = new WebNavigationSettings(cc, cc.Web);

            navigationSettings.GlobalNavigation.Source  = gSource;
            navigationSettings.CurrentNavigation.Source = cSource;
            navigationSettings.Update(taxonomySession);

            try
            {
                cc.ExecuteQueryRetry();
            }
            catch (Exception) // if termset not found then set newly created termset to managed navigation
            {
                TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore();
                Microsoft.SharePoint.Client.Taxonomy.TermGroup group   = termStore.GetTermGroupByName("TG_1"); // TG_1 is a term group mentioned in navigation_add_1605.xml
                Microsoft.SharePoint.Client.Taxonomy.TermSet   termset = group.TermSets.GetByName("TS_1_1");   // TS_1_1 is a term set mentioned in navigation_add_1605.xml
                cc.Load(termStore);
                cc.Load(group, g => g.TermSets);
                cc.Load(termset);
                cc.ExecuteQuery();

                if (StandardNavigationSource.TaxonomyProvider == gSource)
                {
                    navigationSettings.GlobalNavigation.TermStoreId = termStore.Id;
                    navigationSettings.GlobalNavigation.TermSetId   = termset.Id;
                }

                if (StandardNavigationSource.TaxonomyProvider == cSource)
                {
                    navigationSettings.CurrentNavigation.TermStoreId = termStore.Id;
                    navigationSettings.CurrentNavigation.TermSetId   = termset.Id;
                }

                navigationSettings.GlobalNavigation.Source  = gSource;
                navigationSettings.CurrentNavigation.Source = cSource;
                navigationSettings.Update(taxonomySession);
                cc.ExecuteQueryRetry();
            }
        }
Example #16
0
        /// <summary>
        /// Sets the current navigation asynchronous.
        /// </summary>
        /// <param name="web">The web.</param>
        /// <param name="source">The source.</param>
        /// <param name="displaySettings">The display settings.</param>
        public async Task SetCurrentNavigationAsync(Web web, StandardNavigationSource source, NavigationType displaySettings)
        {
            var targetWeb = web ?? _web;
            var ctx       = targetWeb.Context;
            var settings  = new WebNavigationSettings(ctx, targetWeb);

            settings.CurrentNavigation.Source = source;
            settings.Update(TaxonomySession.GetTaxonomySession(ctx));
            ctx.Load(targetWeb, w => w.AllProperties);
            await ctx.ExecuteQueryAsync();

            targetWeb.AllProperties["__CurrentNavigationIncludeTypes"] = ((int)displaySettings).ToString();
            targetWeb.AllProperties["__NavigationShowSiblings"]        = bool.TrueString;
            targetWeb.Update();
            ctx.Load(targetWeb);
            await ctx.ExecuteQueryAsync();
        }
Example #17
0
        /// <summary>
        /// Method to take a term configured as a term driven page to a simple link url.
        /// </summary>
        /// <param name="web">The web for which we want to change a term's target URL in its taxonomy navigation term set</param>
        /// <param name="termInfo">The metadata term to reset</param>
        public void ResetTermDrivenPageToSimpleLinkUrl(SPWeb web, TermInfo termInfo)
        {
            // Get the web-specific navigation settings
            var webNavigationSettings = new WebNavigationSettings(web);

            var taxonomySession = new TaxonomySession(web.Site);
            var defaultStore    = taxonomySession.TermStores[webNavigationSettings.GlobalNavigation.TermStoreId];
            var termSet         = defaultStore.GetTermSet(webNavigationSettings.GlobalNavigation.TermSetId);

            // Get the taxonomy term
            var term = termSet.GetTerm(termInfo.Id);

            if (term != null)
            {
                term.SetLocalCustomProperty(SystemTargetUrl, string.Empty);
                term.TermSet.TermStore.CommitAll();
            }
        }
        //Added for Feature 152
        public void SetManagedNavigationOn(Guid termSetID)
        {
            TaxonomySession ts  = TaxonomySession.GetTaxonomySession(_ctx);
            var             tsc = ts.TermStores;

            _ctx.Load(tsc);
            _ctx.ExecuteQuery();

            WebNavigationSettings navSettings = new WebNavigationSettings(_ctx, _ctx.Web);

            navSettings.GlobalNavigation.Source      = StandardNavigationSource.TaxonomyProvider;
            navSettings.GlobalNavigation.TermStoreId = tsc[0].Id;
            navSettings.GlobalNavigation.TermSetId   = termSetID;
            navSettings.Update(ts);

            _ctx.Load(navSettings);
            _ctx.ExecuteQuery();
        }
        private StructuralNavigation GetStructuralNavigation(Web web, WebNavigationSettings navigationSettings, Boolean currentNavigation)
        {
            // By default avoid removing existing nodes
            var result = new StructuralNavigation {
                RemoveExistingNodes = false
            };

            Microsoft.SharePoint.Client.NavigationNodeCollection sourceNodes = currentNavigation ?
                                                                               web.Navigation.QuickLaunch : web.Navigation.TopNavigationBar;

            web.Context.Load(web, w => w.ServerRelativeUrl);
            web.Context.Load(sourceNodes);
            web.Context.ExecuteQueryRetry();

            result.NavigationNodes.AddRange(from n in sourceNodes.AsEnumerable()
                                            select n.ToDomainModelNavigationNode(web));

            return(result);
        }
Example #20
0
        private bool WebSupportsExtractNavigation(Web web)
        {
            bool isNavSupported = true;

            // The Navigation handler for managed metedata only works for sites with Publishing Features enabled
            if (!web.IsPublishingWeb())
            {
                // NOTE: Here we could have the same edge case of method WebSupportsProvisionNavigation.
                // Just as a reminder ...
                var navigationSettings = new WebNavigationSettings(web.Context, web);
                navigationSettings.EnsureProperties(ns => ns.CurrentNavigation, ns => ns.GlobalNavigation);
                if (navigationSettings.CurrentNavigation.Source == StandardNavigationSource.TaxonomyProvider)
                {
                    isNavSupported = false;
                }
                if (navigationSettings.GlobalNavigation.Source == StandardNavigationSource.TaxonomyProvider)
                {
                    isNavSupported = false;
                }
            }
            return(isNavSupported);
        }
        /// Configures the web to use Taxonomy Navigation with the sample term set.
        public static NavigationTermSet SetUpSampleNavTermSet(TestContext testContext,
            TaxonomySession taxonomySession, SPWeb web)
        {
            NavigationTermSet termSet = RecreateSampleNavTermSet(testContext, taxonomySession, web);

            // Clear any old settings.
            WebNavigationSettings webNavigationSettings = new WebNavigationSettings(web);
            webNavigationSettings.ResetToDefaults();

            webNavigationSettings.GlobalNavigation.Source = StandardNavigationSource.TaxonomyProvider;
            webNavigationSettings.GlobalNavigation.TermStoreId = termSet.TermStoreId;
            webNavigationSettings.GlobalNavigation.TermSetId = termSet.Id;

            webNavigationSettings.CurrentNavigation.Source = StandardNavigationSource.TaxonomyProvider;
            webNavigationSettings.CurrentNavigation.TermStoreId = termSet.TermStoreId;
            webNavigationSettings.CurrentNavigation.TermSetId = termSet.Id;

            webNavigationSettings.Update(taxonomySession);

            TaxonomyNavigation.FlushSiteFromCache(web.Site);

            return termSet;
        }
Example #22
0
        /// Configures the web to use Taxonomy Navigation with the sample term set.
        public static NavigationTermSet SetUpSampleNavTermSet(TestContext testContext,
                                                              TaxonomySession taxonomySession, SPWeb web)
        {
            NavigationTermSet termSet = RecreateSampleNavTermSet(testContext, taxonomySession, web);

            // Clear any old settings.
            WebNavigationSettings webNavigationSettings = new WebNavigationSettings(web);

            webNavigationSettings.ResetToDefaults();

            webNavigationSettings.GlobalNavigation.Source      = StandardNavigationSource.TaxonomyProvider;
            webNavigationSettings.GlobalNavigation.TermStoreId = termSet.TermStoreId;
            webNavigationSettings.GlobalNavigation.TermSetId   = termSet.Id;

            webNavigationSettings.CurrentNavigation.Source      = StandardNavigationSource.TaxonomyProvider;
            webNavigationSettings.CurrentNavigation.TermStoreId = termSet.TermStoreId;
            webNavigationSettings.CurrentNavigation.TermSetId   = termSet.Id;

            webNavigationSettings.Update(taxonomySession);

            TaxonomyNavigation.FlushSiteFromCache(web.Site);

            return(termSet);
        }
Example #23
0
        /// <summary>
        /// Sets the web navigation settings.
        /// </summary>
        /// <param name="web">The web.</param>
        /// <param name="settings">The settings.</param>
        public void SetWebNavigationSettings(SPWeb web, ManagedNavigationInfo settings)
        {
            var taxonomySession = new TaxonomySession(web.Site);

            if (taxonomySession.TermStores.Count > 0)
            {
                // we assume we're always dealing with the site coll's default term store
                var termStore = taxonomySession.DefaultSiteCollectionTermStore;
                var group     = this.taxonomyService.GetTermGroupFromStore(termStore, settings.TermGroup.Name);
                var termSet   = this.taxonomyService.GetTermSetFromGroup(termStore, group, settings.TermSet.Label);

                // Flag the term set as a navigation term set
                termSet.SetCustomProperty("_Sys_Nav_IsNavigationTermSet", "True");
                termSet.TermStore.CommitAll();

                var navigationSettings = new WebNavigationSettings(web);

                navigationSettings.GlobalNavigation.Source      = StandardNavigationSource.TaxonomyProvider;
                navigationSettings.GlobalNavigation.TermStoreId = termStore.Id;
                navigationSettings.GlobalNavigation.TermSetId   = termSet.Id;

                navigationSettings.CurrentNavigation.Source      = StandardNavigationSource.TaxonomyProvider;
                navigationSettings.CurrentNavigation.TermStoreId = termStore.Id;
                navigationSettings.CurrentNavigation.TermSetId   = termSet.Id;

                navigationSettings.AddNewPagesToNavigation       = settings.AddNewPagesToNavigation;
                navigationSettings.CreateFriendlyUrlsForNewPages = settings.CreateFriendlyUrlsForNewsPages;
                navigationSettings.Update(taxonomySession);

                if (settings.PreserveTaggingOnTermSet)
                {
                    termSet.IsAvailableForTagging = true;
                    termSet.TermStore.CommitAll();
                }
            }
        }
        /// <summary>
        /// Updates navigation settings for the current web
        /// </summary>
        /// <param name="web"></param>
        /// <param name="navigationSettings"></param>
        public static void UpdateNavigationSettings(this Web web, AreaNavigationEntity navigationSettings)
        {
            //Read all the properties of the web
            web.Context.Load(web, w => w.AllProperties);
            web.Context.ExecuteQueryRetry();

            if (!ArePublishingFeaturesActivated(web.AllProperties))
            {
                throw new ArgumentException("Structural navigation settings are only supported for publishing sites");
            }

            // Use publishing CSOM API to switch between managed metadata and structural navigation
            var taxonomySession = TaxonomySession.GetTaxonomySession(web.Context);
            web.Context.Load(taxonomySession);
            web.Context.ExecuteQueryRetry();
            var webNav = new WebNavigationSettings(web.Context, web);
            if (!navigationSettings.GlobalNavigation.ManagedNavigation)
            {
                webNav.GlobalNavigation.Source = StandardNavigationSource.PortalProvider;
            }
            else
            {
                webNav.GlobalNavigation.Source = StandardNavigationSource.TaxonomyProvider;
            }

            if (!navigationSettings.CurrentNavigation.ManagedNavigation)
            {
                webNav.CurrentNavigation.Source = StandardNavigationSource.PortalProvider;
            }
            else
            {
                webNav.CurrentNavigation.Source = StandardNavigationSource.TaxonomyProvider;
            }
            webNav.Update(taxonomySession);
            web.Context.ExecuteQueryRetry();

            //Read all the properties of the web again after the above update
            web.Context.Load(web, w => w.AllProperties);
            web.Context.ExecuteQueryRetry();

            if (!navigationSettings.GlobalNavigation.ManagedNavigation)
            {
                var globalNavigationIncludeType = MapToNavigationIncludeTypes(navigationSettings.GlobalNavigation);
                web.AllProperties[GlobalNavigationIncludeTypes] = globalNavigationIncludeType;
                web.AllProperties[GlobalDynamicChildLimit] = navigationSettings.GlobalNavigation.MaxDynamicItems;
            }

            if (!navigationSettings.CurrentNavigation.ManagedNavigation)
            {
                var currentNavigationIncludeType = MapToNavigationIncludeTypes(navigationSettings.CurrentNavigation);
                web.AllProperties[CurrentNavigationIncludeTypes] = currentNavigationIncludeType;
                web.AllProperties[CurrentDynamicChildLimit] = navigationSettings.CurrentNavigation.MaxDynamicItems;

                // Call web.update before the IsSubSite call as this might do an ExecuteQuery. Without the update called the changes will be lost
                web.Update();
                // For the current navigation there's an option to show the sites siblings in structural navigation
                if (web.IsSubSite())
                {
                    web.AllProperties[NavigationShowSiblings] = navigationSettings.CurrentNavigation.ShowSiblings.ToString();
                }
            }

            // if there's either global or current structural navigation then update the sorting settings
            if (!navigationSettings.GlobalNavigation.ManagedNavigation || !navigationSettings.CurrentNavigation.ManagedNavigation)
            {
                // If there's automatic sorting or pages are shown with automatic page sorting then we can set all sort options
                if ((navigationSettings.Sorting == StructuralNavigationSorting.Automatically) ||
                    (navigationSettings.Sorting == StructuralNavigationSorting.ManuallyButPagesAutomatically && (navigationSettings.GlobalNavigation.ShowPages || navigationSettings.CurrentNavigation.ShowPages)))
                {
                    // All sort options can be set
                    web.AllProperties[NavigationOrderingMethod] = (int)navigationSettings.Sorting;
                    web.AllProperties[NavigationAutomaticSortingMethod] = (int)navigationSettings.SortBy;
                    web.AllProperties[NavigationSortAscending] = navigationSettings.SortAscending.ToString();
                }
                else
                {
                    // if pages are not shown we can set sorting to either automatic or manual
                    if (!navigationSettings.GlobalNavigation.ShowPages && !navigationSettings.CurrentNavigation.ShowPages)
                    {
                        if (navigationSettings.Sorting == StructuralNavigationSorting.ManuallyButPagesAutomatically)
                        {
                            throw new ArgumentException("Sorting can only be set to StructuralNavigationSorting.ManuallyButPagesAutomatically when ShowPages has been selected in either the global or current structural navigation settings");
                        }
                    }

                    web.AllProperties[NavigationOrderingMethod] = (int)navigationSettings.Sorting;
                }
            }

            //Persist all property updates at once
            web.Update();
            web.Context.ExecuteQueryRetry();
        }
 private StructuralNavigation GetCurrentStructuralNavigation(Web web, WebNavigationSettings navigationSettings)
 {
     return(GetStructuralNavigation(web, navigationSettings, true));
 }
 private StructuralNavigation GetGlobalStructuralNavigation(Web web, WebNavigationSettings navigationSettings)
 {
     return(GetStructuralNavigation(web, navigationSettings, false));
 }
        public override ProvisioningTemplate ExtractObjects(Web web, ProvisioningTemplate template, ProvisioningTemplateCreationInformation creationInfo)
        {
            using (var scope = new PnPMonitoredScope(this.Name))
            {
                GlobalNavigationType  globalNavigationType;
                CurrentNavigationType currentNavigationType;

                // The Navigation handler works only for sites with Publishing Features enabled
                if (!web.IsPublishingWeb())
                {
                    scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_Navigation_Context_web_is_not_publishing);
                    return(template);
                }

                // Retrieve the current web navigation settings
                var navigationSettings = new WebNavigationSettings(web.Context, web);
                web.Context.Load(navigationSettings, ns => ns.CurrentNavigation, ns => ns.GlobalNavigation);
                web.Context.ExecuteQueryRetry();

                switch (navigationSettings.GlobalNavigation.Source)
                {
                case StandardNavigationSource.InheritFromParentWeb:
                    // Global Navigation is Inherited
                    globalNavigationType = GlobalNavigationType.Inherit;
                    break;

                case StandardNavigationSource.TaxonomyProvider:
                    // Global Navigation is Managed
                    globalNavigationType = GlobalNavigationType.Managed;
                    break;

                case StandardNavigationSource.PortalProvider:
                default:
                    // Global Navigation is Structural
                    globalNavigationType = GlobalNavigationType.Structural;
                    break;
                }

                switch (navigationSettings.CurrentNavigation.Source)
                {
                case StandardNavigationSource.InheritFromParentWeb:
                    // Current Navigation is Inherited
                    currentNavigationType = CurrentNavigationType.Inherit;
                    break;

                case StandardNavigationSource.TaxonomyProvider:
                    // Current Navigation is Managed
                    currentNavigationType = CurrentNavigationType.Managed;
                    break;

                case StandardNavigationSource.PortalProvider:
                default:
                    // Current Navigation is Structural
                    if (AreSiblingsEnabledForCurrentStructuralNavigation(web))
                    {
                        currentNavigationType = CurrentNavigationType.Structural;
                    }
                    else
                    {
                        currentNavigationType = CurrentNavigationType.StructuralLocal;
                    }
                    break;
                }

                template.Navigation = new Model.Navigation(
                    new GlobalNavigation(globalNavigationType,
                                         globalNavigationType == GlobalNavigationType.Structural ? GetGlobalStructuralNavigation(web, navigationSettings) : null,
                                         globalNavigationType == GlobalNavigationType.Managed ? GetGlobalManagedNavigation(web, navigationSettings) : null),
                    new CurrentNavigation(currentNavigationType,
                                          currentNavigationType == CurrentNavigationType.Structural | currentNavigationType == CurrentNavigationType.StructuralLocal ? GetCurrentStructuralNavigation(web, navigationSettings) : null,
                                          currentNavigationType == CurrentNavigationType.Managed ? GetCurrentManagedNavigation(web, navigationSettings) : null)
                    );
            }

            return(template);
        }
Example #28
0
        /// <summary>
        /// Sets the web navigation settings.
        /// </summary>
        /// <param name="web">The web.</param>
        /// <param name="settings">The settings.</param>
        public void SetWebNavigationSettings(SPWeb web, ManagedNavigationInfo settings)
        {
            var taxonomySession = new TaxonomySession(web.Site);
            if (taxonomySession.TermStores.Count > 0)
            {
                var termStore = settings.TermSet.ResolveParentTermStore(taxonomySession);
                var group = settings.TermSet.ResolveParentGroup(taxonomySession, web.Site);
                var termSet = this.taxonomyService.GetTermSetFromGroup(termStore, group, settings.TermSet.Label);

                // Flag the term set as a navigation term set
                termSet.SetCustomProperty(SystemIsNavigationTermSet, "True");
                termStore.CommitAll();

                var navigationSettings = new WebNavigationSettings(web);

                navigationSettings.GlobalNavigation.Source = StandardNavigationSource.TaxonomyProvider;
                navigationSettings.GlobalNavigation.TermStoreId = termStore.Id;
                navigationSettings.GlobalNavigation.TermSetId = termSet.Id;

                navigationSettings.CurrentNavigation.Source = StandardNavigationSource.TaxonomyProvider;
                navigationSettings.CurrentNavigation.TermStoreId = termStore.Id;
                navigationSettings.CurrentNavigation.TermSetId = termSet.Id;

                navigationSettings.AddNewPagesToNavigation = settings.AddNewPagesToNavigation;
                navigationSettings.CreateFriendlyUrlsForNewPages = settings.CreateFriendlyUrlsForNewsPages;
                navigationSettings.Update(taxonomySession);

                if (settings.PreserveTaggingOnTermSet)
                {
                    termSet.IsAvailableForTagging = true;
                    termStore.CommitAll();
                }
            }
        }
Example #29
0
        /// <summary>
        /// Reset web navigation to its default configuration. Disabled the term set as navigation term set.
        /// </summary>
        /// <param name="web">The web</param>
        /// <param name="settings">The managed navigation settings. Set null if you want to keep the associated termset unchanged</param>
        public void ResetWebNavigationToDefault(SPWeb web, ManagedNavigationInfo settings)
        {
            var taxonomySession = new TaxonomySession(web.Site);
            if (taxonomySession.TermStores.Count > 0)
            {
                if (settings != null)
                {
                    // Disable the navigation flag on the the term set
                    var termStore = taxonomySession.DefaultSiteCollectionTermStore;
                    var group = this.taxonomyService.GetTermGroupFromStore(termStore, settings.TermGroup.Name);
                    var termSet = this.taxonomyService.GetTermSetFromGroup(termStore, group, settings.TermSet.Label);

                    const string PropertyName = "_Sys_Nav_IsNavigationTermSet";
                    string propertyValue;
                    if (termSet.CustomProperties.TryGetValue(PropertyName, out propertyValue))
                    {
                        termSet.DeleteCustomProperty(PropertyName);
                        termSet.TermStore.CommitAll();
                    }
                }

                var navigationSettings = new WebNavigationSettings(web);
                navigationSettings.ResetToDefaults();
                navigationSettings.Update(taxonomySession);
            }
        }
 private StructuralNavigation GetCurrentStructuralNavigation(Web web, WebNavigationSettings navigationSettings)
 {
     return GetStructuralNavigation(web, navigationSettings, true);
 }
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite currentSite = properties.Feature.Parent as SPSite;

            if (currentSite != null)
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPWeb currentWeb = currentSite.RootWeb;

                    InitializeWebAppProperties(currentWeb);
                    //Set the master page to be our CustomMaster page.
                    currentWeb.AllowUnsafeUpdates = true;

                    string masterURL = currentWeb.Site.RootWeb.ServerRelativeUrl + "/_catalogs/masterpage/MasterPage/CustomMaster.master";
                    string customMasterURL = currentWeb.Site.RootWeb.ServerRelativeUrl + "/_catalogs/masterpage/MasterPage/CustomMaster.master";
                    currentWeb.MasterUrl = masterURL;
                    currentWeb.CustomMasterUrl = customMasterURL;
                    currentWeb.Update();

                    //Some templates might have subsites from start.
                    foreach (SPWeb subWeb in currentWeb.Site.RootWeb.GetSubwebsForCurrentUser())
                    {
                        ChangeMasterPage(subWeb, masterURL, customMasterURL);
                    }

                    TaxonomySession taxonomySession = new TaxonomySession(currentWeb.Site, updateCache: true);

                    //Use the first Termstore object to see if Taxonomy Service is offline or missing.
                    if (taxonomySession.TermStores.Count == 0)
                        throw new InvalidOperationException("The Taxonomy Service is offline or missing.");

                    string globalTermSetID = "Empty";
                    string localTermSetID = "Empty";

                    //Check property bag for TermSetID key and value. If it doesn't exist create it.

                    if (!currentSite.RootWeb.AllProperties.ContainsKey(globalTermSetIDPropertyKey))
                    {
                        currentSite.RootWeb.AllProperties.Add(globalTermSetIDPropertyKey, globalTermSetID);
                        currentSite.RootWeb.Update();
                    }
                    if (!currentSite.RootWeb.AllProperties.ContainsKey(localTermSetIDPropertyKey))
                    {
                        currentSite.RootWeb.AllProperties.Add(localTermSetIDPropertyKey, localTermSetID);
                        currentSite.RootWeb.Update();
                    }

                    if (!currentSite.RootWeb.AllProperties.ContainsKey(suiteLinksTermStoreKey))
                    {
                        currentSite.RootWeb.AllProperties.Add(suiteLinksTermStoreKey, termStoreName);
                        currentSite.RootWeb.Update();
                    }
                    if (!currentSite.RootWeb.AllProperties.ContainsKey(suiteLinksTermSetID))
                    {
                        currentSite.RootWeb.AllProperties.Add(suiteLinksTermSetID, termSetID.ToString());
                        currentSite.RootWeb.Update();
                    }

                    //Get the values from the property bags.
                    globalTermSetID = Convert.ToString(currentWeb.Site.RootWeb.AllProperties[globalTermSetIDPropertyKey]);
                    localTermSetID = Convert.ToString(currentWeb.Site.RootWeb.AllProperties[localTermSetIDPropertyKey]);

                    TermStore termStore = taxonomySession.TermStores[termStoreName];

                    if (globalTermSetID == "Empty")
                    {
                        TermSet newTermSet = CreateTermSet(termStore, Guid.NewGuid(), currentWeb, TermStoreType.Global);
                        currentSite.RootWeb.AllProperties[globalTermSetIDPropertyKey] = newTermSet.Id.ToString();
                        currentSite.RootWeb.Update();
                        globalTermSetID = newTermSet.Id.ToString();

                    }
                    if (localTermSetID == "Empty")
                    {
                        TermSet newTermSet = CreateTermSet(termStore, Guid.NewGuid(), currentWeb, TermStoreType.Local);
                        currentSite.RootWeb.AllProperties[localTermSetIDPropertyKey] = newTermSet.Id.ToString();
                        currentSite.RootWeb.Update();
                        localTermSetID = newTermSet.Id.ToString();
                    }

                    TermSet globalTermSet = termStore.GetTermSet(new Guid(globalTermSetID));
                    TermSet localTermSet = termStore.GetTermSet(new Guid(localTermSetID));

                    Group termSetGroup = termStore.GetSiteCollectionGroup(currentSite);

                    WebNavigationSettings webNavigationSettings = new WebNavigationSettings(currentWeb);
                    webNavigationSettings.GlobalNavigation.Source = StandardNavigationSource.TaxonomyProvider;
                    webNavigationSettings.CurrentNavigation.Source = StandardNavigationSource.TaxonomyProvider;

                    webNavigationSettings.GlobalNavigation.TermStoreId = termStore.Id;
                    webNavigationSettings.GlobalNavigation.TermSetId = new Guid(globalTermSetID);

                    webNavigationSettings.CurrentNavigation.TermStoreId = termStore.Id;
                    webNavigationSettings.CurrentNavigation.TermSetId = new Guid(localTermSetID);

                    webNavigationSettings.AddNewPagesToNavigation = false;
                    webNavigationSettings.CreateFriendlyUrlsForNewPages = true;

                    webNavigationSettings.Update();
                    currentWeb.Update();

                });

            }
        }
        /// <summary>
        /// Determines whether the current Web has the managed navigation enabled
        /// </summary>
        /// <param name="web">The target web.</param>
        /// <param name="navigationKind">The kind of navigation (Current or Global).</param>
        /// <returns>A boolean result of the test.</returns>
        public static Boolean IsManagedNavigationEnabled(this Web web, ManagedNavigationKind navigationKind)
        {
            Boolean result = false;
            var navigationSettings = new WebNavigationSettings(web.Context, web);
            web.Context.Load(navigationSettings, ns => ns.CurrentNavigation, ns => ns.GlobalNavigation);
            web.Context.Load(web.ParentWeb, pw => pw.ServerRelativeUrl);
            web.Context.ExecuteQueryRetry();

            StandardNavigationSettings targeNavigationSettings =
                navigationKind == ManagedNavigationKind.Current ?
                navigationSettings.CurrentNavigation : navigationSettings.GlobalNavigation;

            if (targeNavigationSettings.Source == StandardNavigationSource.InheritFromParentWeb &&
                (web.ParentWeb.ServerObjectIsNull.HasValue && !web.ParentWeb.ServerObjectIsNull.Value))
            {
                Uri currentWebUri = new Uri(web.Url);
                Uri parentWebUri = new Uri(String.Format("{0}://{1}{2}", currentWebUri.Scheme, currentWebUri.Host, web.ParentWeb.ServerRelativeUrl));

                using (ClientContext parentContext = web.Context.Clone(parentWebUri))
                {
                    result = IsManagedNavigationEnabled(parentContext.Web, navigationKind);
                }
            }
            else
            {
                result = targeNavigationSettings.Source == StandardNavigationSource.TaxonomyProvider;
            }

            return (result);
        }
Example #33
0
        /// <summary>
        /// Method to take a term configured as a term driven page to a simple link url.
        /// </summary>
        /// <param name="web">The web for which we want to change a term's target URL in its taxonomy navigation term set</param>
        /// <param name="termInfo">The metadata term to reset</param>
        public void ResetTermDrivenPageToSimpleLinkUrl(SPWeb web, TermInfo termInfo)
        {
            // Get the web-specific navigation settings
            var webNavigationSettings = new WebNavigationSettings(web);
            
            var taxonomySession = new TaxonomySession(web.Site);
            var defaultStore = taxonomySession.TermStores[webNavigationSettings.GlobalNavigation.TermStoreId];
            var termSet = defaultStore.GetTermSet(webNavigationSettings.GlobalNavigation.TermSetId);

            // Get the taxonomy term
            var term = termSet.GetTerm(termInfo.Id);

            if (term != null)
            {
                term.SetLocalCustomProperty(SystemTargetUrl, string.Empty);
                term.TermSet.TermStore.CommitAll();
            }
        }
        private ManagedNavigation GetManagedNavigation(Web web, WebNavigationSettings navigationSettings, Boolean currentNavigation)
        {
            var result = new ManagedNavigation
            {
                TermStoreId = currentNavigation ? navigationSettings.CurrentNavigation.TermStoreId.ToString() : navigationSettings.GlobalNavigation.TermStoreId.ToString(),
                TermSetId = currentNavigation ? navigationSettings.CurrentNavigation.TermSetId.ToString() : navigationSettings.GlobalNavigation.TermSetId.ToString(),
            };

            // Apply any token replacement for taxonomy IDs
            TokenizeManagedNavigationTaxonomyIds(web, result);

            return (result);
        }
        private StructuralNavigation GetStructuralNavigation(Web web, WebNavigationSettings navigationSettings, Boolean currentNavigation)
        {
            // By default avoid removing existing nodes
            var result = new StructuralNavigation { RemoveExistingNodes = false };
            Microsoft.SharePoint.Client.NavigationNodeCollection sourceNodes = currentNavigation ?
                web.Navigation.QuickLaunch : web.Navigation.TopNavigationBar;

            web.Context.Load(web, w => w.ServerRelativeUrl);
            web.Context.Load(sourceNodes);
            web.Context.ExecuteQueryRetry();

            result.NavigationNodes.AddRange(from n in sourceNodes.AsEnumerable()
                                            select n.ToDomainModelNavigationNode(web));

            return (result);
        }
        /// <summary>
        /// Determines whether the current Web has the managed navigation enabled
        /// </summary>
        /// <param name="web">The target web.</param>
        /// <param name="navigationKind">The kind of navigation (Current or Global).</param>
        /// <returns>A boolean result of the test.</returns>
        public static bool IsManagedNavigationEnabled(this Web web, ManagedNavigationKind navigationKind)
        {
            var result = false;
            var navigationSettings = new WebNavigationSettings(web.Context, web);
            web.Context.Load(navigationSettings, ns => ns.CurrentNavigation, ns => ns.GlobalNavigation);
            web.Context.Load(web.ParentWeb, pw => pw.ServerRelativeUrl);
            web.Context.ExecuteQueryRetry();

            var targetNavigationSettings =
                navigationKind == ManagedNavigationKind.Current ?
                navigationSettings.CurrentNavigation : navigationSettings.GlobalNavigation;

            if (targetNavigationSettings.Source == StandardNavigationSource.InheritFromParentWeb &&
                !web.ParentWeb.ServerObjectIsNull())
            {
                var currentWebUri = new Uri(web.Url);
                var parentWebUri = new Uri($"{currentWebUri.Scheme}://{currentWebUri.Host}{web.ParentWeb.ServerRelativeUrl}");

                using (var parentContext = web.Context.Clone(parentWebUri))
                {
                    result = IsManagedNavigationEnabled(parentContext.Web, navigationKind);
                }
            }
            else
            {
                result = targetNavigationSettings.Source == StandardNavigationSource.TaxonomyProvider;
            }

            return (result);
        }
Example #37
0
        /// <summary>
        /// Updates navigation settings for the current web
        /// </summary>
        /// <param name="web">Web to process</param>
        /// <param name="navigationSettings">Navigation settings to update</param>
        public static void UpdateNavigationSettings(this Web web, AreaNavigationEntity navigationSettings)
        {
            //Read all the properties of the web
            web.Context.Load(web, w => w.AllProperties);
            web.Context.ExecuteQueryRetry();

            if (!ArePublishingFeaturesActivated(web.AllProperties))
            {
                throw new ArgumentException("Structural navigation settings are only supported for publishing sites");
            }

            // Use publishing CSOM API to switch between managed metadata and structural navigation
            var taxonomySession = TaxonomySession.GetTaxonomySession(web.Context);

            web.Context.Load(taxonomySession);
            web.Context.ExecuteQueryRetry();
            var webNav = new WebNavigationSettings(web.Context, web);

            if (navigationSettings.GlobalNavigation.InheritFromParentWeb)
            {
                if (web.IsSubSite())
                {
                    webNav.GlobalNavigation.Source = StandardNavigationSource.InheritFromParentWeb;
                }
                else
                {
                    throw new ArgumentException("Cannot inherit global navigation on root site.");
                }
            }
            else if (!navigationSettings.GlobalNavigation.ManagedNavigation)
            {
                webNav.GlobalNavigation.Source = StandardNavigationSource.PortalProvider;
            }
            else
            {
                webNav.GlobalNavigation.Source = StandardNavigationSource.TaxonomyProvider;
            }

            if (navigationSettings.CurrentNavigation.InheritFromParentWeb)
            {
                if (web.IsSubSite())
                {
                    webNav.CurrentNavigation.Source = StandardNavigationSource.InheritFromParentWeb;
                }
                else
                {
                    throw new ArgumentException("Cannot inherit current navigation on root site.");
                }
            }
            else if (!navigationSettings.CurrentNavigation.ManagedNavigation)
            {
                webNav.CurrentNavigation.Source = StandardNavigationSource.PortalProvider;
            }
            else
            {
                webNav.CurrentNavigation.Source = StandardNavigationSource.TaxonomyProvider;
            }

            // If managed metadata navigation is used, set settings related to page creation
            if (navigationSettings.GlobalNavigation.ManagedNavigation || navigationSettings.CurrentNavigation.ManagedNavigation)
            {
                webNav.AddNewPagesToNavigation       = navigationSettings.AddNewPagesToNavigation;
                webNav.CreateFriendlyUrlsForNewPages = navigationSettings.CreateFriendlyUrlsForNewPages;
            }

            webNav.Update(taxonomySession);
            web.Context.ExecuteQueryRetry();

            //Read all the properties of the web again after the above update
            web.Context.Load(web, w => w.AllProperties);
            web.Context.ExecuteQueryRetry();

            if (!navigationSettings.GlobalNavigation.ManagedNavigation)
            {
                var globalNavigationIncludeType = MapToNavigationIncludeTypes(navigationSettings.GlobalNavigation);
                web.AllProperties[GlobalNavigationIncludeTypes] = globalNavigationIncludeType;
                web.AllProperties[GlobalDynamicChildLimit]      = navigationSettings.GlobalNavigation.MaxDynamicItems;
            }

            if (!navigationSettings.CurrentNavigation.ManagedNavigation)
            {
                var currentNavigationIncludeType = MapToNavigationIncludeTypes(navigationSettings.CurrentNavigation);
                web.AllProperties[CurrentNavigationIncludeTypes] = currentNavigationIncludeType;
                web.AllProperties[CurrentDynamicChildLimit]      = navigationSettings.CurrentNavigation.MaxDynamicItems;

                // Call web.update before the IsSubSite call as this might do an ExecuteQuery. Without the update called the changes will be lost
                web.Update();
                // For the current navigation there's an option to show the sites siblings in structural navigation
                if (web.IsSubSite())
                {
                    web.AllProperties[NavigationShowSiblings] = navigationSettings.CurrentNavigation.ShowSiblings.ToString();
                }
            }

            // if there's either global or current structural navigation then update the sorting settings
            if (!navigationSettings.GlobalNavigation.ManagedNavigation || !navigationSettings.CurrentNavigation.ManagedNavigation)
            {
                // If there's automatic sorting or pages are shown with automatic page sorting then we can set all sort options
                if ((navigationSettings.Sorting == StructuralNavigationSorting.Automatically) ||
                    (navigationSettings.Sorting == StructuralNavigationSorting.ManuallyButPagesAutomatically && (navigationSettings.GlobalNavigation.ShowPages || navigationSettings.CurrentNavigation.ShowPages)))
                {
                    // All sort options can be set
                    web.AllProperties[NavigationOrderingMethod]         = (int)navigationSettings.Sorting;
                    web.AllProperties[NavigationAutomaticSortingMethod] = (int)navigationSettings.SortBy;
                    web.AllProperties[NavigationSortAscending]          = navigationSettings.SortAscending.ToString();
                }
                else
                {
                    // if pages are not shown we can set sorting to either automatic or manual
                    if (!navigationSettings.GlobalNavigation.ShowPages && !navigationSettings.CurrentNavigation.ShowPages)
                    {
                        if (navigationSettings.Sorting == StructuralNavigationSorting.ManuallyButPagesAutomatically)
                        {
                            throw new ArgumentException("Sorting can only be set to StructuralNavigationSorting.ManuallyButPagesAutomatically when ShowPages has been selected in either the global or current structural navigation settings");
                        }
                    }

                    web.AllProperties[NavigationOrderingMethod] = (int)navigationSettings.Sorting;
                }
            }

            //Persist all property updates at once
            web.Update();
            web.Context.ExecuteQueryRetry();
        }
Example #38
0
 private StructuralNavigation GetCurrentStructuralNavigation(Web web, WebNavigationSettings navigationSettings, ProvisioningTemplate template, ProvisioningTemplateCreationInformation creationInfo)
 {
     return(GetStructuralNavigation(web, navigationSettings, Enums.NavigationType.QuickLaunch, template, creationInfo));
 }
 private ManagedNavigation GetCurrentManagedNavigation(Web web, WebNavigationSettings navigationSettings)
 {
     return GetManagedNavigation(web, navigationSettings, true);
 }
    public void ConfigureTaxonomyNavigation()
    {
        ClientContext clientContext = new ClientContext(TestConfig.ServerUrl);
        TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);
        taxonomySession.UpdateCache();

        NavigationTermSet termSet = DemoUtilities.RecreateSampleNavTermSet(
            this.TestContext, clientContext, taxonomySession, clientContext.Web);

        // Clear out any old settings
        WebNavigationSettings webNavigationSettings = new WebNavigationSettings(clientContext, clientContext.Web);
        webNavigationSettings.ResetToDefaults();
        webNavigationSettings.Update(taxonomySession);

        TaxonomyNavigation.FlushSiteFromCache(clientContext, clientContext.Site);
        clientContext.ExecuteQuery();

        this.WaitForSync();

        // Verify the TermSet is not running
        NavigationTermSet actualTermSet;
        ExceptionHandlingScope scope = new ExceptionHandlingScope(clientContext);
        using (scope.StartScope())
        {
            using (scope.StartTry())
            {
                actualTermSet = TaxonomyNavigation.GetTermSetForWeb(clientContext, clientContext.Web,
                    "GlobalNavigationTaxonomyProvider", includeInheritedSettings: true);
            }
            using (scope.StartCatch())
            {
            }
        }
        clientContext.ExecuteQuery();

        Assert.IsTrue(actualTermSet.ServerObjectIsNull.Value);

        // Assign the new settings
        webNavigationSettings = new WebNavigationSettings(clientContext, clientContext.Web);

        clientContext.Load(webNavigationSettings,
            w => w.GlobalNavigation,
            w => w.CurrentNavigation
        );
        clientContext.Load(termSet,
            ts => ts.TermStoreId,
            ts => ts.Id
        );
        clientContext.ExecuteQuery();

        // GlobalNavigation = top menu (aka "top nav")
        // CurrentNavigation = left menu (aka "quick launch")
        webNavigationSettings.GlobalNavigation.Source = StandardNavigationSource.TaxonomyProvider;
        webNavigationSettings.GlobalNavigation.TermStoreId = termSet.TermStoreId;
        webNavigationSettings.GlobalNavigation.TermSetId = termSet.Id;
        webNavigationSettings.Update(taxonomySession);

        TaxonomyNavigation.FlushSiteFromCache(clientContext, clientContext.Site);
        clientContext.ExecuteQuery();

        this.WaitForSync();

        actualTermSet = TaxonomyNavigation.GetTermSetForWeb(clientContext, clientContext.Web,
            "GlobalNavigationTaxonomyProvider", includeInheritedSettings: true);
        clientContext.Load(actualTermSet, ts => ts.Id);
        clientContext.ExecuteQuery();

        Assert.AreEqual(termSet.Id, actualTermSet.Id);
    }
 private StructuralNavigation GetGlobalStructuralNavigation(Web web, WebNavigationSettings navigationSettings)
 {
     return GetStructuralNavigation(web, navigationSettings, false);
 }
Example #42
0
        /// <summary>
        /// Sets the web navigation settings.
        /// </summary>
        /// <param name="web">The web.</param>
        /// <param name="settings">The settings.</param>
        public void SetWebNavigationSettings(SPWeb web, ManagedNavigationInfo settings)
        {
            var taxonomySession = new TaxonomySession(web.Site);
            if (taxonomySession.TermStores.Count > 0)
            {
                // we assume we're always dealing with the site coll's default term store
                var termStore = taxonomySession.DefaultSiteCollectionTermStore;
                var group = this.taxonomyService.GetTermGroupFromStore(termStore, settings.TermGroup.Name);
                var termSet = this.taxonomyService.GetTermSetFromGroup(termStore, group, settings.TermSet.Label);

                // Flag the term set as a navigation term set
                termSet.SetCustomProperty("_Sys_Nav_IsNavigationTermSet", "True");
                termSet.TermStore.CommitAll();

                var navigationSettings = new WebNavigationSettings(web);

                navigationSettings.GlobalNavigation.Source = StandardNavigationSource.TaxonomyProvider;
                navigationSettings.GlobalNavigation.TermStoreId = termStore.Id;
                navigationSettings.GlobalNavigation.TermSetId = termSet.Id;

                navigationSettings.CurrentNavigation.Source = StandardNavigationSource.TaxonomyProvider;
                navigationSettings.CurrentNavigation.TermStoreId = termStore.Id;
                navigationSettings.CurrentNavigation.TermSetId = termSet.Id;

                navigationSettings.AddNewPagesToNavigation = settings.AddNewPagesToNavigation;
                navigationSettings.CreateFriendlyUrlsForNewPages = settings.CreateFriendlyUrlsForNewsPages;
                navigationSettings.Update(taxonomySession);

                if (settings.PreserveTaggingOnTermSet)
                {
                    termSet.IsAvailableForTagging = true;
                    termSet.TermStore.CommitAll();
                }
            }
        }
 private ManagedNavigation GetGlobalManagedNavigation(Web web, WebNavigationSettings navigationSettings)
 {
     return GetManagedNavigation(web, navigationSettings, false);
 }
Example #44
0
        private static WebNavigationSettings FindTaxonomyWebNavigationSettingsInWebOrInParents(SPWeb web, out SPWeb webWithNavigationSettings)
        {
            var currentWebNavSettings = new WebNavigationSettings(web);
            webWithNavigationSettings = web;

            if (currentWebNavSettings.GlobalNavigation.Source == StandardNavigationSource.InheritFromParentWeb
                && web.ParentWeb != null)
            {
                // current web inherits its settings from its parent, so we gotta look upwards to the parent webs
                // recursively until we find a match
                return FindTaxonomyWebNavigationSettingsInWebOrInParents(web.ParentWeb, out webWithNavigationSettings);
            }

            return currentWebNavSettings;
        }
        public override ProvisioningTemplate ExtractObjects(Web web, ProvisioningTemplate template, ProvisioningTemplateCreationInformation creationInfo)
        {
            using (var scope = new PnPMonitoredScope(this.Name))
            {
                GlobalNavigationType globalNavigationType;
                CurrentNavigationType currentNavigationType;

                // The Navigation handler works only for sites with Publishing Features enabled
                if (!web.IsPublishingWeb())
                {
                    scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_Navigation_Context_web_is_not_publishing);
                    return template;
                }

                // Retrieve the current web navigation settings
                var navigationSettings = new WebNavigationSettings(web.Context, web);
                web.Context.Load(navigationSettings, ns => ns.CurrentNavigation, ns => ns.GlobalNavigation);
                web.Context.ExecuteQueryRetry();

                switch (navigationSettings.GlobalNavigation.Source)
                {
                    case StandardNavigationSource.InheritFromParentWeb:
                        // Global Navigation is Inherited
                        globalNavigationType = GlobalNavigationType.Inherit;
                        break;
                    case StandardNavigationSource.TaxonomyProvider:
                        // Global Navigation is Managed
                        globalNavigationType = GlobalNavigationType.Managed;
                        break;
                    case StandardNavigationSource.PortalProvider:
                    default:
                        // Global Navigation is Structural
                        globalNavigationType = GlobalNavigationType.Structural;
                        break;
                }

                switch (navigationSettings.CurrentNavigation.Source)
                {
                    case StandardNavigationSource.InheritFromParentWeb:
                        // Current Navigation is Inherited
                        currentNavigationType = CurrentNavigationType.Inherit;
                        break;
                    case StandardNavigationSource.TaxonomyProvider:
                        // Current Navigation is Managed
                        currentNavigationType = CurrentNavigationType.Managed;
                        break;
                    case StandardNavigationSource.PortalProvider:
                    default:
                        // Current Navigation is Structural
                        if (AreSiblingsEnabledForCurrentStructuralNavigation(web))
                        {
                            currentNavigationType = CurrentNavigationType.Structural;
                        }
                        else
                        {
                            currentNavigationType = CurrentNavigationType.StructuralLocal;
                        }
                        break;
                }

                template.Navigation = new Model.Navigation(
                    new GlobalNavigation(globalNavigationType,
                        globalNavigationType == GlobalNavigationType.Structural ? GetGlobalStructuralNavigation(web, navigationSettings) : null,
                        globalNavigationType == GlobalNavigationType.Managed ? GetGlobalManagedNavigation(web, navigationSettings) : null),
                    new CurrentNavigation(currentNavigationType,
                        currentNavigationType == CurrentNavigationType.Structural | currentNavigationType == CurrentNavigationType.StructuralLocal ? GetCurrentStructuralNavigation(web, navigationSettings) : null,
                        currentNavigationType == CurrentNavigationType.Managed ? GetCurrentManagedNavigation(web, navigationSettings) : null)
                    );
            }

            return template;
        }
 private ManagedNavigation GetGlobalManagedNavigation(Web web, WebNavigationSettings navigationSettings)
 {
     return(GetManagedNavigation(web, navigationSettings, false));
 }
Example #47
0
        public override TokenParser ProvisionObjects(Web web, ProvisioningTemplate template, TokenParser parser, ProvisioningTemplateApplyingInformation applyingInformation)
        {
            using (var scope = new PnPMonitoredScope(this.Name))
            {
                if (template.Navigation != null)
                {
                    if (!WebSupportsProvisionNavigation(web, template))
                    {
                        scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_Navigation_Context_web_is_not_publishing);
                        return(parser);
                    }

                    // Check if this is not a noscript site as navigation features are not supported
                    bool isNoScriptSite = web.IsNoScriptSite();

                    // Retrieve the current web navigation settings
                    var navigationSettings = new WebNavigationSettings(web.Context, web);
                    web.Context.Load(navigationSettings, ns => ns.CurrentNavigation, ns => ns.GlobalNavigation);
                    web.Context.ExecuteQueryRetry();

                    navigationSettings.AddNewPagesToNavigation       = template.Navigation.AddNewPagesToNavigation;
                    navigationSettings.CreateFriendlyUrlsForNewPages = template.Navigation.CreateFriendlyUrlsForNewPages;

                    if (template.Navigation.GlobalNavigation != null)
                    {
                        switch (template.Navigation.GlobalNavigation.NavigationType)
                        {
                        case GlobalNavigationType.Inherit:
                            navigationSettings.GlobalNavigation.Source = StandardNavigationSource.InheritFromParentWeb;
                            web.Navigation.UseShared = true;
                            break;

                        case GlobalNavigationType.Managed:
                            if (template.Navigation.GlobalNavigation.ManagedNavigation == null)
                            {
                                throw new ApplicationException(CoreResources.Provisioning_ObjectHandlers_Navigation_missing_global_managed_navigation);
                            }
                            navigationSettings.GlobalNavigation.Source      = StandardNavigationSource.TaxonomyProvider;
                            navigationSettings.GlobalNavigation.TermStoreId = Guid.Parse(parser.ParseString(template.Navigation.GlobalNavigation.ManagedNavigation.TermStoreId));
                            navigationSettings.GlobalNavigation.TermSetId   = Guid.Parse(parser.ParseString(template.Navigation.GlobalNavigation.ManagedNavigation.TermSetId));
                            break;

                        case GlobalNavigationType.Structural:
                        default:
                            if (template.Navigation.GlobalNavigation.StructuralNavigation == null)
                            {
                                throw new ApplicationException(CoreResources.Provisioning_ObjectHandlers_Navigation_missing_global_structural_navigation);
                            }
                            navigationSettings.GlobalNavigation.Source = StandardNavigationSource.PortalProvider;

                            ProvisionGlobalStructuralNavigation(web,
                                                                template.Navigation.GlobalNavigation.StructuralNavigation, parser, applyingInformation.ClearNavigation, scope);

                            break;
                        }
                        if (!isNoScriptSite)
                        {
                            navigationSettings.Update(TaxonomySession.GetTaxonomySession(web.Context));
                            web.Context.ExecuteQueryRetry();
                        }
                    }

                    if (template.Navigation.CurrentNavigation != null)
                    {
                        switch (template.Navigation.CurrentNavigation.NavigationType)
                        {
                        case CurrentNavigationType.Inherit:
                            navigationSettings.CurrentNavigation.Source = StandardNavigationSource.InheritFromParentWeb;
                            break;

                        case CurrentNavigationType.Managed:
                            if (template.Navigation.CurrentNavigation.ManagedNavigation == null)
                            {
                                throw new ApplicationException(CoreResources.Provisioning_ObjectHandlers_Navigation_missing_current_managed_navigation);
                            }
                            navigationSettings.CurrentNavigation.Source      = StandardNavigationSource.TaxonomyProvider;
                            navigationSettings.CurrentNavigation.TermStoreId = Guid.Parse(parser.ParseString(template.Navigation.CurrentNavigation.ManagedNavigation.TermStoreId));
                            navigationSettings.CurrentNavigation.TermSetId   = Guid.Parse(parser.ParseString(template.Navigation.CurrentNavigation.ManagedNavigation.TermSetId));
                            break;

                        case CurrentNavigationType.StructuralLocal:
                            if (!isNoScriptSite)
                            {
                                web.SetPropertyBagValue(NavigationShowSiblings, "false");
                            }
                            if (template.Navigation.CurrentNavigation.StructuralNavigation == null)
                            {
                                throw new ApplicationException(CoreResources.Provisioning_ObjectHandlers_Navigation_missing_current_structural_navigation);
                            }
                            navigationSettings.CurrentNavigation.Source = StandardNavigationSource.PortalProvider;

                            ProvisionCurrentStructuralNavigation(web,
                                                                 template.Navigation.CurrentNavigation.StructuralNavigation, parser, applyingInformation.ClearNavigation, scope);

                            break;

                        case CurrentNavigationType.Structural:
                        default:
                            if (!isNoScriptSite)
                            {
                                web.SetPropertyBagValue(NavigationShowSiblings, "true");
                            }
                            if (template.Navigation.CurrentNavigation.StructuralNavigation == null)
                            {
                                throw new ApplicationException(CoreResources.Provisioning_ObjectHandlers_Navigation_missing_current_structural_navigation);
                            }
                            navigationSettings.CurrentNavigation.Source = StandardNavigationSource.PortalProvider;

                            ProvisionCurrentStructuralNavigation(web,
                                                                 template.Navigation.CurrentNavigation.StructuralNavigation, parser, applyingInformation.ClearNavigation, scope);

                            break;
                        }

                        if (!isNoScriptSite)
                        {
                            navigationSettings.Update(TaxonomySession.GetTaxonomySession(web.Context));
                            web.Context.ExecuteQueryRetry();
                        }
                    }
                }
            }

            return(parser);
        }
 private ManagedNavigation GetCurrentManagedNavigation(Web web, WebNavigationSettings navigationSettings)
 {
     return(GetManagedNavigation(web, navigationSettings, true));
 }
Example #49
0
        public override ProvisioningTemplate ExtractObjects(Web web, ProvisioningTemplate template, ProvisioningTemplateCreationInformation creationInfo)
        {
            using (var scope = new PnPMonitoredScope(this.Name))
            {
                GlobalNavigationType  globalNavigationType;
                CurrentNavigationType currentNavigationType;

                if (!WebSupportsExtractNavigation(web))
                {
                    scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_Navigation_Context_web_is_not_publishing);
                    return(template);
                }

                // Retrieve the current web navigation settings
                var navigationSettings = new WebNavigationSettings(web.Context, web);
                navigationSettings.EnsureProperties(ns => ns.AddNewPagesToNavigation, ns => ns.CreateFriendlyUrlsForNewPages,
                                                    ns => ns.CurrentNavigation, ns => ns.GlobalNavigation);

                switch (navigationSettings.GlobalNavigation.Source)
                {
                case StandardNavigationSource.InheritFromParentWeb:
                    // Global Navigation is Inherited
                    globalNavigationType = GlobalNavigationType.Inherit;
                    break;

                case StandardNavigationSource.TaxonomyProvider:
                    // Global Navigation is Managed
                    globalNavigationType = GlobalNavigationType.Managed;
                    break;

                case StandardNavigationSource.PortalProvider:
                default:
                    // Global Navigation is Structural
                    globalNavigationType = GlobalNavigationType.Structural;
                    break;
                }

                switch (navigationSettings.CurrentNavigation.Source)
                {
                case StandardNavigationSource.InheritFromParentWeb:
                    // Current Navigation is Inherited
                    currentNavigationType = CurrentNavigationType.Inherit;
                    break;

                case StandardNavigationSource.TaxonomyProvider:
                    // Current Navigation is Managed
                    currentNavigationType = CurrentNavigationType.Managed;
                    break;

                case StandardNavigationSource.PortalProvider:
                default:
                    // Current Navigation is Structural
                    if (AreSiblingsEnabledForCurrentStructuralNavigation(web))
                    {
                        currentNavigationType = CurrentNavigationType.Structural;
                    }
                    else
                    {
                        currentNavigationType = CurrentNavigationType.StructuralLocal;
                    }
                    break;
                }

                var navigationEntity = new Model.Navigation(new GlobalNavigation(globalNavigationType,
                                                                                 globalNavigationType == GlobalNavigationType.Structural ? GetGlobalStructuralNavigation(web, navigationSettings) : null,
                                                                                 globalNavigationType == GlobalNavigationType.Managed ? GetGlobalManagedNavigation(web, navigationSettings) : null),
                                                            new CurrentNavigation(currentNavigationType,
                                                                                  currentNavigationType == CurrentNavigationType.Structural | currentNavigationType == CurrentNavigationType.StructuralLocal ? GetCurrentStructuralNavigation(web, navigationSettings) : null,
                                                                                  currentNavigationType == CurrentNavigationType.Managed ? GetCurrentManagedNavigation(web, navigationSettings) : null)
                                                            );

                navigationEntity.AddNewPagesToNavigation       = navigationSettings.AddNewPagesToNavigation;
                navigationEntity.CreateFriendlyUrlsForNewPages = navigationSettings.CreateFriendlyUrlsForNewPages;

                // If a base template is specified then use that one to "cleanup" the generated template model
                if (creationInfo.BaseTemplate != null)
                {
                    if (!navigationEntity.Equals(creationInfo.BaseTemplate.Navigation))
                    {
                        template.Navigation = navigationEntity;
                    }
                }
                else
                {
                    template.Navigation = navigationEntity;
                }
            }

            return(template);
        }
    public void ConfigureTaxonomyNavigation()
    {
        using (SPSite site = new SPSite(TestConfig.ServerUrl))
        {
            using (SPWeb web = site.OpenWeb())
            {
                TaxonomySession taxonomySession = new TaxonomySession(site, updateCache: true);

                NavigationTermSet termSet = DemoUtilities.RecreateSampleNavTermSet(
                    this.TestContext, taxonomySession, web);

                // Clear any old settings.
                WebNavigationSettings webNavigationSettings = new WebNavigationSettings(web);
                webNavigationSettings.ResetToDefaults();
                webNavigationSettings.Update(taxonomySession);

                TaxonomyNavigation.FlushSiteFromCache(site);

                this.WaitForSync();

                // Verify the TermSet object is not running.
                NavigationTermSet actualTermSet;

                actualTermSet = TaxonomyNavigation.GetTermSetForWeb(web,
                    StandardNavigationProviderNames.GlobalNavigationTaxonomyProvider,
                    includeInheritedSettings: true);

                Assert.IsTrue(actualTermSet == null);

                // Assign the new settings.
                webNavigationSettings = new WebNavigationSettings(web);

                // GlobalNavigation = top menu (aka "top nav")
                // CurrentNavigation = left menu (aka "quick launch")
                webNavigationSettings.GlobalNavigation.Source = StandardNavigationSource.TaxonomyProvider;
                webNavigationSettings.GlobalNavigation.TermStoreId = termSet.TermStoreId;
                webNavigationSettings.GlobalNavigation.TermSetId = termSet.Id;
                webNavigationSettings.Update(taxonomySession);

                TaxonomyNavigation.FlushSiteFromCache(site);

                this.WaitForSync();

                actualTermSet = TaxonomyNavigation.GetTermSetForWeb(web,
                    StandardNavigationProviderNames.GlobalNavigationTaxonomyProvider,
                    includeInheritedSettings: true);

                Assert.AreEqual(termSet.Id, actualTermSet.Id);
            }
        }
    }
        public override TokenParser ProvisionObjects(Web web, ProvisioningTemplate template, TokenParser parser, ProvisioningTemplateApplyingInformation applyingInformation)
        {
            using (var scope = new PnPMonitoredScope(this.Name))
            {
                if (template.Navigation != null)
                {
                    // The Navigation handler works only for sites with Publishing Features enabled
                    if (!web.IsPublishingWeb())
                    {
                        scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_Navigation_Context_web_is_not_publishing);
                        return parser;
                    }

                    // Retrieve the current web navigation settings
                    var navigationSettings = new WebNavigationSettings(web.Context, web);
                    web.Context.Load(navigationSettings, ns => ns.CurrentNavigation, ns => ns.GlobalNavigation);
                    web.Context.ExecuteQueryRetry();

                    if (template.Navigation.GlobalNavigation != null)
                    {
                        switch (template.Navigation.GlobalNavigation.NavigationType)
                        {
                            case GlobalNavigationType.Inherit:
                                navigationSettings.GlobalNavigation.Source = StandardNavigationSource.InheritFromParentWeb;
                                break;
                            case GlobalNavigationType.Managed:
                                if (template.Navigation.GlobalNavigation.ManagedNavigation == null)
                                {
                                    throw new ApplicationException(CoreResources.Provisioning_ObjectHandlers_Navigation_missing_global_managed_navigation);
                                }
                                navigationSettings.GlobalNavigation.Source = StandardNavigationSource.TaxonomyProvider;
                                navigationSettings.GlobalNavigation.TermStoreId = Guid.Parse(parser.ParseString(template.Navigation.GlobalNavigation.ManagedNavigation.TermStoreId));
                                navigationSettings.GlobalNavigation.TermSetId = Guid.Parse(parser.ParseString(template.Navigation.GlobalNavigation.ManagedNavigation.TermSetId));
                                break;
                            case GlobalNavigationType.Structural:
                            default:
                                if (template.Navigation.GlobalNavigation.StructuralNavigation == null)
                                {
                                    throw new ApplicationException(CoreResources.Provisioning_ObjectHandlers_Navigation_missing_global_structural_navigation);
                                }
                                ProvisionGlobalStructuralNavigation(web,
                                    template.Navigation.GlobalNavigation.StructuralNavigation, parser);
                                break;
                        }
                        web.Context.ExecuteQueryRetry();
                    }

                    if (template.Navigation.CurrentNavigation != null)
                    {
                        switch (template.Navigation.CurrentNavigation.NavigationType)
                        {
                            case CurrentNavigationType.Inherit:
                                navigationSettings.CurrentNavigation.Source = StandardNavigationSource.InheritFromParentWeb;
                                break;
                            case CurrentNavigationType.Managed:
                                if (template.Navigation.CurrentNavigation.ManagedNavigation == null)
                                {
                                    throw new ApplicationException(CoreResources.Provisioning_ObjectHandlers_Navigation_missing_current_managed_navigation);
                                }
                                navigationSettings.CurrentNavigation.Source = StandardNavigationSource.TaxonomyProvider;
                                navigationSettings.CurrentNavigation.TermStoreId = Guid.Parse(parser.ParseString(template.Navigation.CurrentNavigation.ManagedNavigation.TermStoreId));
                                navigationSettings.CurrentNavigation.TermSetId = Guid.Parse(parser.ParseString(template.Navigation.CurrentNavigation.ManagedNavigation.TermSetId));
                                break;
                            case CurrentNavigationType.StructuralLocal:
                                web.SetPropertyBagValue(NavigationShowSiblings, "false");
                                if (template.Navigation.CurrentNavigation.StructuralNavigation == null)
                                {
                                    throw new ApplicationException(CoreResources.Provisioning_ObjectHandlers_Navigation_missing_current_structural_navigation);
                                }
                                ProvisionCurrentStructuralNavigation(web,
                                    template.Navigation.CurrentNavigation.StructuralNavigation, parser);
                                break;
                            case CurrentNavigationType.Structural:
                            default:
                                if (template.Navigation.CurrentNavigation.StructuralNavigation == null)
                                {
                                    throw new ApplicationException(CoreResources.Provisioning_ObjectHandlers_Navigation_missing_current_structural_navigation);
                                }
                                ProvisionCurrentStructuralNavigation(web,
                                    template.Navigation.CurrentNavigation.StructuralNavigation, parser);
                                break;
                        }
                        web.Context.ExecuteQueryRetry();
                    }
                }
            }

            return parser;
        }
        public override TokenParser ProvisionObjects(Web web, ProvisioningTemplate template, TokenParser parser, ProvisioningTemplateApplyingInformation applyingInformation)
        {
            using (var scope = new PnPMonitoredScope(this.Name))
            {
                if (template.Navigation != null)
                {
                    // The Navigation handler works only for sites with Publishing Features enabled
                    if (!web.IsPublishingWeb())
                    {
                        scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_Navigation_Context_web_is_not_publishing);
                        return(parser);
                    }

                    // Retrieve the current web navigation settings
                    var navigationSettings = new WebNavigationSettings(web.Context, web);
                    web.Context.Load(navigationSettings, ns => ns.CurrentNavigation, ns => ns.GlobalNavigation);
                    web.Context.ExecuteQueryRetry();

                    if (template.Navigation.GlobalNavigation != null)
                    {
                        switch (template.Navigation.GlobalNavigation.NavigationType)
                        {
                        case GlobalNavigationType.Inherit:
                            navigationSettings.GlobalNavigation.Source = StandardNavigationSource.InheritFromParentWeb;
                            break;

                        case GlobalNavigationType.Managed:
                            if (template.Navigation.GlobalNavigation.ManagedNavigation == null)
                            {
                                throw new ApplicationException(CoreResources.Provisioning_ObjectHandlers_Navigation_missing_global_managed_navigation);
                            }
                            navigationSettings.GlobalNavigation.Source      = StandardNavigationSource.TaxonomyProvider;
                            navigationSettings.GlobalNavigation.TermStoreId = Guid.Parse(parser.ParseString(template.Navigation.GlobalNavigation.ManagedNavigation.TermStoreId));
                            navigationSettings.GlobalNavigation.TermSetId   = Guid.Parse(parser.ParseString(template.Navigation.GlobalNavigation.ManagedNavigation.TermSetId));
                            break;

                        case GlobalNavigationType.Structural:
                        default:
                            if (template.Navigation.GlobalNavigation.StructuralNavigation == null)
                            {
                                throw new ApplicationException(CoreResources.Provisioning_ObjectHandlers_Navigation_missing_global_structural_navigation);
                            }
                            ProvisionGlobalStructuralNavigation(web,
                                                                template.Navigation.GlobalNavigation.StructuralNavigation, parser);
                            break;
                        }
                        web.Context.ExecuteQueryRetry();
                    }

                    if (template.Navigation.CurrentNavigation != null)
                    {
                        switch (template.Navigation.CurrentNavigation.NavigationType)
                        {
                        case CurrentNavigationType.Inherit:
                            navigationSettings.CurrentNavigation.Source = StandardNavigationSource.InheritFromParentWeb;
                            break;

                        case CurrentNavigationType.Managed:
                            if (template.Navigation.CurrentNavigation.ManagedNavigation == null)
                            {
                                throw new ApplicationException(CoreResources.Provisioning_ObjectHandlers_Navigation_missing_current_managed_navigation);
                            }
                            navigationSettings.CurrentNavigation.Source      = StandardNavigationSource.TaxonomyProvider;
                            navigationSettings.CurrentNavigation.TermStoreId = Guid.Parse(parser.ParseString(template.Navigation.CurrentNavigation.ManagedNavigation.TermStoreId));
                            navigationSettings.CurrentNavigation.TermSetId   = Guid.Parse(parser.ParseString(template.Navigation.CurrentNavigation.ManagedNavigation.TermSetId));
                            break;

                        case CurrentNavigationType.StructuralLocal:
                            web.SetPropertyBagValue(NavigationShowSiblings, "false");
                            if (template.Navigation.CurrentNavigation.StructuralNavigation == null)
                            {
                                throw new ApplicationException(CoreResources.Provisioning_ObjectHandlers_Navigation_missing_current_structural_navigation);
                            }
                            ProvisionCurrentStructuralNavigation(web,
                                                                 template.Navigation.CurrentNavigation.StructuralNavigation, parser);
                            break;

                        case CurrentNavigationType.Structural:
                        default:
                            if (template.Navigation.CurrentNavigation.StructuralNavigation == null)
                            {
                                throw new ApplicationException(CoreResources.Provisioning_ObjectHandlers_Navigation_missing_current_structural_navigation);
                            }
                            ProvisionCurrentStructuralNavigation(web,
                                                                 template.Navigation.CurrentNavigation.StructuralNavigation, parser);
                            break;
                        }
                        web.Context.ExecuteQueryRetry();
                    }
                }
            }

            return(parser);
        }
Example #53
0
        /// <summary>
        /// Reset web navigation to its default configuration. Disabled the term set as navigation term set.
        /// </summary>
        /// <param name="web">The web</param>
        /// <param name="settings">The managed navigation settings. Set null if you want to keep the associated termset unchanged</param>
        public void ResetWebNavigationToDefault(SPWeb web, ManagedNavigationInfo settings)
        {
            var taxonomySession = new TaxonomySession(web.Site);
            if (taxonomySession.TermStores.Count > 0)
            {
                if (settings != null)
                {
                    // Disable the navigation flag on the the term set
                    var termStore = settings.TermSet.ResolveParentTermStore(taxonomySession);
                    var group = settings.TermSet.ResolveParentGroup(taxonomySession, web.Site);
                    var termSet = this.taxonomyService.GetTermSetFromGroup(termStore, group, settings.TermSet.Label);

                    string propertyValue;
                    if (termSet.CustomProperties.TryGetValue(SystemIsNavigationTermSet, out propertyValue))
                    {
                        termSet.DeleteCustomProperty(SystemIsNavigationTermSet);
                        termStore.CommitAll();
                    }
                }

                var navigationSettings = new WebNavigationSettings(web);
                navigationSettings.ResetToDefaults();
                navigationSettings.Update(taxonomySession);
            }
        }