Exemplo n.º 1
0
        private static void SetTermDrivenPageSettingsOnTermSet(TermDrivenPageSettingInfo termDrivenPageInfo, TaxonomySession currentSession, SPSite currentSite)
        {
            // Get the term set group by name
            // Note, when you build the term store hierachy by XML using Gary Lapointe Cmdlet, the term group ID isn't kept
            var termSotre = termDrivenPageInfo.TermSet.ResolveParentTermStore(currentSession);
            var group     = termDrivenPageInfo.TermSet.ResolveParentGroup(currentSession, currentSite);

            if (group != null)
            {
                // Get the term set
                var termSet = group.TermSets[termDrivenPageInfo.TermSet.Id];

                // Set URLs
                if (!string.IsNullOrEmpty(termDrivenPageInfo.TargetUrlForChildTerms))
                {
                    termSet.SetCustomProperty(SystemTargetUrlForChildTerms, termDrivenPageInfo.TargetUrlForChildTerms);
                }

                if (!string.IsNullOrEmpty(termDrivenPageInfo.CatalogTargetUrlForChildTerms))
                {
                    termSet.SetCustomProperty(SystemCatalogTargetUrlForChildTerms, termDrivenPageInfo.CatalogTargetUrlForChildTerms);
                }

                termSotre.CommitAll();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Set term driven page settings in the term store
        /// </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="termDrivenPageInfo">The term driven page setting info</param>
        public void SetTermDrivenPageSettings(SPWeb web, TermDrivenPageSettingInfo termDrivenPageInfo)
        {
            // Force the taxonomy session to update the cache due to previous changes
            // Note: this is necessary in the context of the components installation script
            var taxonomySession = new TaxonomySession(web.Site, true);

            if (taxonomySession.TermStores.Count > 0)
            {
                // Term Set setting
                if (termDrivenPageInfo.IsTermSet)
                {
                    SetTermDrivenPageSettingsOnTermSet(termDrivenPageInfo, taxonomySession, web.Site);
                }

                // Term setting
                if (termDrivenPageInfo.IsTerm)
                {
                    this.SetTermDrivenPageSettingsOnTerm(web, termDrivenPageInfo, taxonomySession);
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Set term driven page settings in the term store
        /// </summary>
        /// <param name="site">The site</param>
        /// <param name="termDrivenPageInfo">The term driven page setting info</param>
        public void SetTermDrivenPageSettings(SPSite site, TermDrivenPageSettingInfo termDrivenPageInfo)
        {
            // Force the taxonomy session to update the cache due to previous changes
            // Note: this is necessary in the context of the components installation script
            var taxonomySession = new TaxonomySession(site, true);

            if (taxonomySession.TermStores.Count > 0)
            {
                var defaultTermStore = taxonomySession.DefaultSiteCollectionTermStore;

                // Term Set setting
                if (termDrivenPageInfo.IsTermSet)
                {
                    // Get the term set group by name
                    // Note, when you build the term store hierachy by XML using Gary Lapointe Cmdlet, the term group ID isn't kept
                    var group = this.taxonomyService.GetTermGroupFromStore(defaultTermStore, termDrivenPageInfo.TermSet.Group.Name);

                    if (group != null)
                    {
                        // Get the term set
                        var termSet = group.TermSets[termDrivenPageInfo.TermSet.Id];

                        // Set URLs
                        if (!string.IsNullOrEmpty(termDrivenPageInfo.TargetUrlForChildTerms))
                        {
                            termSet.SetCustomProperty("_Sys_Nav_TargetUrlForChildTerms", termDrivenPageInfo.TargetUrlForChildTerms);
                        }

                        if (!string.IsNullOrEmpty(termDrivenPageInfo.CatalogTargetUrlForChildTerms))
                        {
                            termSet.SetCustomProperty("_Sys_Nav_CatalogTargetUrlForChildTerms", termDrivenPageInfo.CatalogTargetUrlForChildTerms);
                        }

                        termSet.TermStore.CommitAll();
                    }
                }

                // Term setting
                if (termDrivenPageInfo.IsTerm)
                {
                    // Get the taxonomy term
                    var term = this.taxonomyService.GetTermForId(site, termDrivenPageInfo.Term.Id);

                    if (term != null)
                    {
                        var terms = new List <Term> {
                            term
                        };
                        terms.AddRange(term.ReusedTerms);

                        // For the orginal term and its reuses
                        foreach (var currentTerm in terms)
                        {
                            string isNavigationTermSet;

                            // Check if the term term set is flagged as navigation term set
                            // By default a TermSet doesn't have the custom property "_Sys_Nav_IsNavigationTermSet" so we can't acces it directly in the collection
                            currentTerm.TermSet.CustomProperties.TryGetValue("_Sys_Nav_IsNavigationTermSet", out isNavigationTermSet);

                            // If the term set allow navigation
                            if (!string.IsNullOrEmpty(isNavigationTermSet))
                            {
                                // Get the associated navigation term set
                                var navigationTermSet = NavigationTermSet.GetAsResolvedByWeb(
                                    currentTerm.TermSet,
                                    site.RootWeb,
                                    StandardNavigationProviderNames.CurrentNavigationTaxonomyProvider);

                                // Get the navigation term
                                var navigationTerm = navigationTermSet.Terms.FirstOrDefault(t => t.Id.Equals(currentTerm.Id));
                                if (navigationTerm != null)
                                {
                                    navigationTerm.ExcludeFromCurrentNavigation =
                                        termDrivenPageInfo.ExcludeFromCurrentNavigation;
                                    navigationTerm.ExcludeFromGlobalNavigation =
                                        termDrivenPageInfo.ExcludeFromGlobalNavigation;
                                }

                                if (termDrivenPageInfo.IsSimpleLinkOrHeader)
                                {
                                    if (!string.IsNullOrEmpty(termDrivenPageInfo.SimpleLinkOrHeader))
                                    {
                                        currentTerm.SetLocalCustomProperty(
                                            "_Sys_Nav_SimpleLinkUrl",
                                            termDrivenPageInfo.SimpleLinkOrHeader);
                                    }
                                }
                                else
                                {
                                    // Set URLs properties
                                    if (!string.IsNullOrEmpty(termDrivenPageInfo.TargetUrl))
                                    {
                                        currentTerm.SetLocalCustomProperty("_Sys_Nav_TargetUrl", termDrivenPageInfo.TargetUrl);
                                    }

                                    if (!string.IsNullOrEmpty(termDrivenPageInfo.TargetUrlForChildTerms))
                                    {
                                        currentTerm.SetLocalCustomProperty(
                                            "_Sys_Nav_TargetUrlForChildTerms",
                                            termDrivenPageInfo.TargetUrlForChildTerms);
                                    }

                                    if (!string.IsNullOrEmpty(termDrivenPageInfo.CatalogTargetUrl))
                                    {
                                        currentTerm.SetLocalCustomProperty(
                                            "_Sys_Nav_CatalogTargetUrl",
                                            termDrivenPageInfo.CatalogTargetUrl);
                                    }

                                    if (!string.IsNullOrEmpty(termDrivenPageInfo.CatalogTargetUrlForChildTerms))
                                    {
                                        currentTerm.SetLocalCustomProperty(
                                            "_Sys_Nav_CatalogTargetUrlForChildTerms",
                                            termDrivenPageInfo.CatalogTargetUrlForChildTerms);
                                    }
                                }

                                // Commit all updates
                                currentTerm.TermStore.CommitAll();
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 4
0
        private void SetTermDrivenPageSettingsOnTerm(SPWeb currentWeb, TermDrivenPageSettingInfo termDrivenPageInfo, TaxonomySession currentSession)
        {
            SPWeb webWithNavSettings    = null;
            var   webNavigationSettings = FindTaxonomyWebNavigationSettingsInWebOrInParents(currentWeb, out webWithNavSettings);

            if (webNavigationSettings != null &&
                webNavigationSettings.GlobalNavigation.Source == StandardNavigationSource.TaxonomyProvider)
            {
                var termStore = termDrivenPageInfo.Term.TermSet.ResolveParentTermStore(currentSession);

                if (termStore.Id != webNavigationSettings.GlobalNavigation.TermStoreId)
                {
                    string termStoreMismatchErrorMsg = string.Format(
                        CultureInfo.InvariantCulture,
                        "NavigationHelper.SetTermDrivenPageSettingsOnTerm: The parent term store for the specified TermInfo (TermStoreId={0}) does not match the current webNavigationSettings.GlobalNavigation.TermStoreId ({1}).",
                        termStore.Id,
                        webNavigationSettings.GlobalNavigation.TermStoreId);

                    throw new NotSupportedException(termStoreMismatchErrorMsg);
                }

                var previousThreadCulture   = CultureInfo.CurrentCulture;
                var previousThreadUiCulture = CultureInfo.CurrentUICulture;
                var previousWorkingLanguage = termStore.WorkingLanguage;

                try
                {
                    CultureInfo currentWebCulture   = webWithNavSettings.Locale;
                    CultureInfo currentWebUiCulture = new CultureInfo((int)webWithNavSettings.Language);

                    // Force thread culture/uiculture and term store working language
                    Thread.CurrentThread.CurrentCulture   = currentWebCulture;
                    Thread.CurrentThread.CurrentUICulture = currentWebUiCulture;
                    termStore.WorkingLanguage             = currentWebCulture.LCID; // force the working language to fit with the current web language

                    var termSet = termStore.GetTermSet(webNavigationSettings.GlobalNavigation.TermSetId);

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

                    if (term != null)
                    {
                        string isNavigationTermSet;

                        // Check if the term term set is flagged as navigation term set
                        // By default a TermSet doesn't have the custom property "_Sys_Nav_IsNavigationTermSet" so we can't acces it directly in the collection
                        term.TermSet.CustomProperties.TryGetValue(SystemIsNavigationTermSet, out isNavigationTermSet);

                        // If the term set allow navigation
                        if (!string.IsNullOrEmpty(isNavigationTermSet))
                        {
                            // Get the associated navigation term set
                            var navigationTermSet = NavigationTermSet.GetAsResolvedByWeb(
                                term.TermSet,
                                webWithNavSettings,
                                StandardNavigationProviderNames.GlobalNavigationTaxonomyProvider);

                            navigationTermSet = navigationTermSet.GetAsEditable(currentSession);

                            // Get the navigation term
                            var navigationTerm = FindTermInNavigationTermsCollection(navigationTermSet.Terms, term.Id);
                            if (navigationTerm != null)
                            {
                                // Gotta re-fetch the navigation term as an "editable" instance in order to avoid UnauthorizedAccessExceptions
                                navigationTerm = navigationTerm.GetAsEditable(currentSession);

                                navigationTerm.ExcludeFromCurrentNavigation = termDrivenPageInfo.ExcludeFromCurrentNavigation;
                                navigationTerm.ExcludeFromGlobalNavigation  = termDrivenPageInfo.ExcludeFromGlobalNavigation;

                                if (termDrivenPageInfo.IsSimpleLinkOrHeader)
                                {
                                    if (!string.IsNullOrEmpty(termDrivenPageInfo.SimpleLinkOrHeader))
                                    {
                                        if (navigationTerm.LinkType == NavigationLinkType.FriendlyUrl)
                                        {
                                            // clear any existing target URL on the term
                                            navigationTerm.TargetUrl.Value = string.Empty;
                                            navigationTerm.TargetUrlForChildTerms.Value        = string.Empty;
                                            navigationTerm.CatalogTargetUrl.Value              = string.Empty;
                                            navigationTerm.CatalogTargetUrlForChildTerms.Value = string.Empty;
                                        }

                                        if (navigationTerm.LinkType != NavigationLinkType.SimpleLink)
                                        {
                                            navigationTerm.LinkType = NavigationLinkType.SimpleLink;
                                        }

                                        navigationTerm.SimpleLinkUrl = termDrivenPageInfo.SimpleLinkOrHeader;
                                    }
                                }
                                else
                                {
                                    // Set URLs properties
                                    if (navigationTerm.LinkType != NavigationLinkType.FriendlyUrl)
                                    {
                                        navigationTerm.LinkType = NavigationLinkType.FriendlyUrl;
                                    }

                                    if (!string.IsNullOrEmpty(termDrivenPageInfo.TargetUrl))
                                    {
                                        navigationTerm.TargetUrl.Value = termDrivenPageInfo.TargetUrl;
                                    }

                                    if (!string.IsNullOrEmpty(termDrivenPageInfo.TargetUrlForChildTerms))
                                    {
                                        navigationTerm.TargetUrlForChildTerms.Value = termDrivenPageInfo.TargetUrlForChildTerms;
                                    }

                                    if (!string.IsNullOrEmpty(termDrivenPageInfo.CatalogTargetUrl))
                                    {
                                        navigationTerm.CatalogTargetUrl.Value = termDrivenPageInfo.CatalogTargetUrl;
                                    }

                                    if (!string.IsNullOrEmpty(termDrivenPageInfo.CatalogTargetUrlForChildTerms))
                                    {
                                        navigationTerm.CatalogTargetUrlForChildTerms.Value = termDrivenPageInfo.CatalogTargetUrlForChildTerms;
                                    }
                                }

                                // Commit all updates
                                termStore.CommitAll();
                            }
                            else
                            {
                                this.logger.Warn(
                                    "TaxonomyHelper.SetTermDrivenPageSettingsOnTerm: Failed to find corresponding NavigationTerm for term ID={0} in term set ID={1} Name={2}",
                                    term.Id,
                                    navigationTermSet.Id,
                                    navigationTermSet.TaxonomyName);
                            }
                        }
                    }
                }
                finally
                {
                    // Restore previous thread cultures and term store working language
                    Thread.CurrentThread.CurrentCulture   = previousThreadCulture;
                    Thread.CurrentThread.CurrentUICulture = previousThreadUiCulture;
                    termStore.WorkingLanguage             = previousWorkingLanguage;
                }
            }
            else
            {
                this.logger.Warn(
                    "TaxonomyHelper.SetTermDrivenPageSettingsOnTerm: Failed to find taxonomy-type WebNavigationSettings in web ID={0} Url={1} or in any of its parent webs. At least one SPWeb in the hierarchy should have a GlobalNavigation setting of source type Taxonomy.",
                    currentWeb.ID,
                    currentWeb.Url);
            }
        }