vendredi 14 juin 2013

SharePoint Get Convivial URL for a specific Term by Label

You can use the following function in order to retrieve the convivial url associated for a specific term if you use the navigation thanks to the termstore in sharepoint 2010 and 2013

        /// Get convivial URL for a specific term using Label
        /// </summary>
        /// <param name="navigation_Group_Name">Navigation group name for the site collection</param>
        /// <param name="navigation_TermSet_Name">Termset name used for termstore navigation</param>
        /// <param name="navigation_Nom_Term">Name of the term you want to retrieve</param>
        /// <param name="navigation_default_url">Default url used if none term match the label filter</param>
        /// <param name="site"></param>
        /// <returns>site relative convival url   /</returns>
        public static string GetUrlConvivialForTerm(
            string navigation_Group_Name,
            string navigation_TermSet_Name, 
            string navigation_Nom_Term, 
            string navigation_default_url,
            SPSite site)
            string url = string.Empty;
            //Utiliser SPContext SPContext.Current.Site
            TaxonomySession session = new TaxonomySession(site, false);
            TermStore store = session.TermStores[0];
            CacheManager cm = new CacheManager();
            TermSet tHE = TermManager.GetTermSet(navigation_Group_Name, navigation_TermSet_Name, site.RootWeb.Url);

            NavigationTermSet navTermSet = NavigationTermSet.GetAsResolvedByWeb(tHE, site.RootWeb,

            IEnumerable<NavigationTerm> terms = navTermSet.Terms.Where(t => t.GetTaxonomyTerm().Name.Equals(navigation_Nom_Term));
            if(terms != null)
                NavigationTerm term = terms.FirstOrDefault();
                if (term != null)
                    url = term.GetWebRelativeFriendlyUrl();

                return navigation_default_url;
                return url;

Aucun commentaire :

Enregistrer un commentaire