0

With this piece of code, I get the properties of all terms of termset. I'd like to get the Friendly URL also.

  terms.get().then((terms:ITerm[])=>{
            terms.forEach((term:any)=>{
              term.LocalCustomProperties._Sys_Nav_TargetUrl;
              console.log(term.LocalCustomProperties._Sys_Nav_TargetUrl);
            });
          });

I tried : _Sys_Nav_FriendlyUrlSegment but that does not work.


Navigation Node Type:

enter image description here

1

try the below code,

 terms.get().then((terms:ITerm[])=>{
            terms.forEach((term:any)=>{
              term.LocalCustomProperties._Sys_Nav_TargetUrl;
              console.log(term.LocalCustomProperties._Sys_Nav_SimpleLinkUrl);
            });
          });

The above code gives you the simple link.

AFAIK, term.LocalCustomProperties doesn't have the friendly URL property, so you can try the below approach, it will help you to get the friendly URL using JSOM, but not using PnP JS,

To retrieve the friendly URL you could utilize SP.Publishing.Navigation.NavigationTerm object which:

Specifies navigation behavior and properties for a SP.Taxonomy.Term
constructor (sp.taxonomy)
object that is used to drive the navigation and friendly URLs for a website.

The specified class contains getWebRelativeFriendlyUrl method which

returns the friendly URL segments as a concatenated path.

Example

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 = "--Term ID goes 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 friendlyUrl = navTerm.getWebRelativeFriendlyUrl();

                ctx.executeQueryAsync(function() {
                   console.log(friendlyUrl.get_value());
                },
                function(sender,args) {
                   console.log(args.get_message());
                });
            })
        })
    })
});

SOURCE

| improve this answer | |
  • When I select Simple Link in the Navigation Node Type, I can retrieve the Url with _Sys_Nav_SimpleLinkUrl, but I would like to do than automatically with a Friendly URL – bAbdenour Jan 6 at 9:59
  • updated the answer – Karthik Jaganathan Jan 6 at 10:46
  • I would like to use this JSOM script in my webpart, and I have to register the SharePoint JSOM API as external scripts like in this exemple (see Reference JSOM declaratively section). How can I do that? – bAbdenour Jan 12 at 12:53
  • Kindly raise this as a new question, so that more people will look at it and give you various approaches. – Karthik Jaganathan Jan 12 at 16:07
  • if you find my answer useful, kindly upvote and accept it as answer, so that it will help others too. – Karthik Jaganathan Jan 14 at 17:48

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.