11

I am creating a menu control that read terms from taxonomy term store. Get termSet and term is straight forward

        using (SPSite siteCollection = new SPSite(siteCollecitonUrl))
        {
            TaxonomySession taxSession = new TaxonomySession(siteCollection);
            TermStore taxStore = taxSession.TermStores[0];
            Group taxGroup = taxStore.GetSiteCollectionGroup(siteCollection);
            TermSet termSet = taxGroup.TermSets[strTermSetsName];

            foreach (term in termSet.terms)
            {
                term.???
            }
        }

but how to retrieve friendly URL or simple url link?

I could not find a property or method.

Thank you.

1
  • Hi, after your export the URL into a CSV, how do you import it back?
    – Mark L
    Commented Aug 24, 2015 at 9:50

4 Answers 4

12

You have to use NavigationTerm

To Create Term set:

  NavigationTerm term2 = navTermSet.CreateTerm("Term 2", NavigationLinkType.FriendlyUrl,
                    term2Guid);

/// Verify that the NavigationTermSetView is being applied correctly.

 Assert.AreEqual(web.ServerRelativeUrl + "/term-2", term2.GetResolvedDisplayUrl(null).ToString());

In order to add new terms to navigation term set, you can use the following code:

// Obtain navigation term set
NavigationTermSet navigationTermSet = NavigationTermSet.GetAsResolvedByWeb(termSet, web, "GlobalNavigationTaxonomyProvider");
// Specify that this term set can be used for site navigation
navigationTermSet.IsNavigationTermSet = true;
// Creates new term pointing to an already existing URL outside of SharePoint
NavigationTerm term = navigationTermSet.CreateTerm("Bing Search", NavigationLinkType.SimpleLink, Guid.NewGuid());
term.SimpleLinkUrl = "http://www.bing.com/";
// Creates new term pointing to an existing SharePoint page
NavigationTerm term2 = navigationTermSet.CreateTerm("Target page", NavigationLinkType.FriendlyUrl, Guid.NewGuid());
term2.TargetUrl.Value = "~site/Pages/TargetPage.aspx";

Hope this helps

http://msdn.microsoft.com/en-us/library/jj163978.aspx

3
  • Anuja, thank you for the comment but it did not help. My question is if I have term store setup and simply want to retrieve term properties especially friendly URL and simple link. I do not like to create new terms by using NavigationTerm.
    – wming
    Commented Feb 8, 2013 at 17:06
  • 1
    Hell Anuja. Your comment did not answer my question but point to the right direction. I managed to retrieve those properties by using NavigationTermSet.
    – wming
    Commented Feb 8, 2013 at 18:37
  • Glad that it helps to you
    – Anuja
    Commented Feb 10, 2013 at 18:53
3
foreach (term in termSet.terms)
{
    string URL = term.LocalCustomProperties["_Sys_Nav_SimpleLinkUrl"];
}
4
  • This is incorrect Commented Jan 4, 2014 at 12:51
  • @EricHerlitz What is incorrect about this? The solution worked for me when I tried it.
    – thm51mb
    Commented Apr 17, 2014 at 13:42
  • This works fairly well!! And if I am not using NavigationTermSet this has to be used (for TermSet).
    – Amit
    Commented Sep 18, 2014 at 6:47
  • I am unable to get the value using term.LocalCustomProperties["_Sys_Nav_SimpleLinkUrl"];. My navigation url is properly set in sharepoint. Any ideas ? Commented Dec 19, 2014 at 10:48
1

The missing link is the bridge between TermSet and NavigationTermSet. As Anuja correctly stated, the responsible method is

NavigationTermSet.GetAsResolvedByWeb

So, here's my complete example - took me more than a day to figure it out (a different approach was working in an ASPX page but failed in the Web Service I was programming).

using Microsoft.SharePoint.Publishing.Navigation;
using Microsoft.SharePoint.Taxonomy;
using System;

TaxonomySession taxonomySession = new TaxonomySession(site);
StandardNavigationSettings navigationSettings = new WebNavigationSettings(site.RootWeb).CurrentNavigation;
TermStore termStore = taxonomySession.TermStores[navigationSettings.TermStoreId];
TermSet termSet = termStore.GetTermSet(navigationSettings.TermSetId);
NavigationTermSet navigationTermSet = NavigationTermSet.GetAsResolvedByWeb(termSet, site.RootWeb, StandardNavigationProviderNames.GlobalNavigationTaxonomyProvider);

foreach (NavigationTerm navigationTerm in navigationTerms)
{
    Console.WriteLine("navigationTerm.GetResolvedDisplayUrl(string.Empty): " + navigationTerm.GetResolvedDisplayUrl(string.Empty));
}

Hope that helps...

1
  • I hope it helps too - sounds like something I'm trying to do, so I'm linking and I'll return when I get to that point. I'll upvote if it does help - thanks regardless! Commented May 21, 2015 at 19:31
0

Use the code snippet below to get TermStore LocalCustomProperties.

TermCollection termCollection = termSet.GetAllTerms();
foreach (Term term in termCollection)
{
    string linkUrl = string.Empty;
    term.LocalCustomProperties.TryGetValue("_Sys_Nav_SimpleLinkUrl", out linkUrl);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.