2

I'm using Managed Metadata Navigation and I'm trying to access it using CSOM. The problem I'm having is that I can't get the friendly URLs for the terms.

If I run this code using the Server Side Object Model, the call to NavigationTerm.GetWebRelativeFriendlyUrl() returns the web relative friendly URL as expected.

using (SPSite site = new SPSite("http://sp2013"))
{
    SPWeb web = site.RootWeb;

    NavigationTermSet navigationTermSet = TaxonomyNavigation.GetTermSetForWeb(web, "GlobalNavigationTaxonomyProvider", true);

    foreach (NavigationTerm term in navigationTermSet.Terms)
    {
        Console.WriteLine(term.TaxonomyName + " - " + term.GetWebRelativeFriendlyUrl());

        foreach (NavigationTerm childTerm in term.Terms)
        {
            Console.WriteLine(" -- " + childTerm.TaxonomyName + " - " + childTerm.GetWebRelativeFriendlyUrl());
        }
    }
}

If I run this code using Client Site Object Model, the call to NavigationTerm.GetWebRelativeFriendlyUrl() returns NULL.

using (ClientContext clientContext = new ClientContext("http://sp2013"))
{
    NavigationTermSet navigationTermSet = TaxonomyNavigation.GetTermSetForWeb(clientContext, clientContext.Web, "GlobalNavigationTaxonomyProvider", true);

    clientContext.Load(navigationTermSet, x => x.Terms);
    clientContext.ExecuteQuery();

    foreach (NavigationTerm term in navigationTermSet.Terms)
    {
        clientContext.Load(term, x => x.Terms);
        clientContext.ExecuteQuery();

        Console.WriteLine(term.TaxonomyName + " - " + term.GetWebRelativeFriendlyUrl().Value);

        foreach (NavigationTerm childTerm in term.Terms)
        {
            clientContext.Load(childTerm, x => x.Terms);
            clientContext.ExecuteQuery();

            Console.WriteLine(" --- " + childTerm.TaxonomyName + " - " + childTerm.GetWebRelativeFriendlyUrl().Value);
        }
    }
}

Any ideas what is wrong with the CSOM code or how I can get the friendly urls for the terms?

1 Answer 1

3

Ensure you run this call GetWebRelativeFriendlyUrl such as below:

var friendlyUrl = term.GetWebRelativeFriendlyUrl();

Before your call to:

clientContext.ExecuteQuery();

Then use the friendlyUrl in your output:

Console.WriteLine(" --- " + childTerm.TaxonomyName + " - " + friendlyUrl);

Small side note: The method GetWebRelativeFriendlyUrl returns a type ClientResult<T>. In order for the variable to be populated, you need to call ctx.ExecuteQuery() after the declaration. (where ctx is the established ClientContext.)

0

Your Answer

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

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