3

We have a cross-site publishing setup with a managed navigation from terms that the items are marked with. On the item pages I have the term or terms that a given item is marked with. I have both their names and their GUIDs, but I imagine the GUIDs will be more direct.

For example I have a document that is marked with both Mobile and Tools terms and I have the following GUIDs for this item:

GP0|#0c422db8-7973-44e5-b35a-c46cf1b7aa49;
L0|#00c422db8-7973-44e5-b35a-c46cf1b7aa49|Learning team tools and process;
GTSet|#0a31782c-ceb0-459f-b625-d98753c26922;
GPP|#8a7d2039-55e5-4b65-99bb-354bc53a9d1e;
GP0|#bf624af6-190a-4047-b3bf-238c8a60e8d7;
L0|#0bf624af6-190a-4047-b3bf-238c8a60e8d7|Mobile-accessible learning

The ones that start with GP0 are the ones I am interested in finding the friendly urls for.

I'm finding a few tutorials on how to work with the term store in C# like this one but I need to do this client side with javascript and I'm not sure how to translate C# into the corresponding javascript api.

Does anyone know how to get the friendly url for a term given the guid? Thanks.

Edit: Code to get the term

I've found this code to retrieve the term from the GUID.

var scriptbase = _spPageContextInfo.webAbsoluteUrl+"/_layouts/15";

$.getScript(scriptbase +"/sp.runtime.js",function(){    
    $.getScript(scriptbase + "/sp.js", function() {
        $.getScript(scriptbase + "/sp.publishing.js",function(){
            $.getScript(scriptbase+"/sp.taxonomy.js",function(){
                getTermByGuid('974f8fd6-99f5-49f2-a5ee-cdf536f5cfcb');
            })
        })
    })
})

function getTermByGuid(guid){

    var ctx = SP.ClientContext.get_current();
    var web = ctx.get_web();
    var tax = SP.Taxonomy.TaxonomySession.getTaxonomySession(ctx);
    var term = tax.getTerm(guid);
    ctx.load(tax);
    ctx.load(term); 
    ctx.executeQueryAsync(function(){
        console.log(term);  
    })  
}

The Term has all kinds of info in it, but nothing for the friendly url -- at least that I can find. I don't know if it is there and I just can't figure out how to get it or if I need to make a request to include it.

Also I've found some other examples that reference SP.Publishing.Navigation.TaxonomyNavigation.getTermSetForWeb but not in terms of jsom, only C#.

  • I did find this dotsa's answer in this thread sharepoint.stackexchange.com/questions/136047/…. It is a bit of overkill -- gets everything in the term group and (in my case) has 124+ network calls and takes about 10+ seconds to run. Not so feasible for every page in my site. I can't see how to just extract the bit I need. – Rothrock Jul 19 '16 at 21:21
2

To retrieve navigation behavior and properties for a SP.Taxonomy.Term object the SP.Publishing.Navigation.NavigationTerm object is intended, the following example demonstrates how to retrieve those properties by Term Id:

var scriptbase = _spPageContextInfo.webAbsoluteUrl+"/_layouts/15";

$.getScript(scriptbase +"/sp.runtime.js",function(){    
    $.getScript(scriptbase + "/sp.js", function() {
        $.getScript(scriptbase + "/sp.publishing.js",function(){
            $.getScript(scriptbase+"/sp.taxonomy.js",function(){

                var termId = "--YOUR TERM GUID HERE--";


                var ctx = SP.ClientContext.get_current();
                var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(ctx);

                var term = taxSession.getTerm(termId);
                var navTerm = SP.Publishing.Navigation.NavigationTerm.getAsResolvedByWeb(ctx,term, ctx.get_web(), "CurrentNavigationTaxonomyProvider");

                var displayUrl = navTerm.getResolvedDisplayUrl();
                var friendlyUrl = navTerm.getWebRelativeFriendlyUrl();

                ctx.executeQueryAsync(function() {
                   console.log(friendlyUrl.get_value());
                   console.log(displayUrl.get_value());
                },
                function(sender,args) {
                   console.log(args.get_message());
                });
            })
        })
    })
})
  • This is helpful, but it isn't working. Neither the friendly nor the display url are showing the customized shortcut. They both return slightly different versions of the default url. I found some documentation which maybe suggested get_friendlyUrlSegment or get_catalogTargetUrl, but I can't get those to load. – Rothrock Apr 20 '17 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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