Sunday 31 May 2015

How to get "Simple Link Url" Property of Taxonomy Term using JSOM

Alright so, i was doing some Research and Development for finding "how to get simple link url" of a taxonomy navigation term from code-behind. I found that there were very few posts and some of them even had mistakes in their code snippets so i decided to make a blog and share correct information to you guys :) 

Now if you don't know how to create a Navigation term, here are some steps you'll need to go through.

1) First open up your taxonomy term store from the SharePoint  Site Settings > Term Store Management.

2) Create a demo Term-Set Group and Then a demo Term Set in the Term Store.

3) Now, go to the Demo Term-Set you've just created, you'll find "Intended Use" menu item. click on it and you'll see below page.



4). Now select "Use this Term Set for Site Navigation" and press Save

Alright, so you're now ready to create some navigation terms, i have created some navigation terms as per below pictures and now we will see how to set the Simple Link Url property of a term and get it from code-behind (JSOM).



1). Select any term that you have created, go into "Navigation" menu. 



2). Going to "Navigation" will look like as below picture, select "Simple Link or Header", and write down your Url or you can browse it from your existing site.



3). Now to get this Url from Code, there is only one line you will have to write (Which is highlighted in my code snippet), 
_________________________________________________________________________


context = new SP.ClientContext.get_current();
    
    var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(contextsidebar);
    //Term Stores
    var termStores = taxSession.get_termStores();
    //Name of the Term Store from which to get the Terms.
    var termStore = termStores.getByName("Taxonomy_3NhjDrbwzBe+Q+8TIIafFw==");
    
    //GUID of Term Set from which to get the Terms.
    var termSet = termStore.getTermSet("96a82fb7-dd1c-41d4-8223-c118f7ab52f8");


    var terms = termSet.get_terms();
    context .load(terms);

    context .executeQueryAsync(function () {

        var termEnumerator = terms.getEnumerator();
        //ToptermList = $('#sidebarUl');
        var mainul = $('#mainUL');

        while (termEnumerator.moveNext()) {

            var currentTerm = termEnumerator.get_current();
            
            termName = currentTerm.get_name();
            termId = currentTerm.get_id();
            var simpleLinkUrl = currentTerm.get_localCustomProperties()._Sys_Nav_SimpleLinkUrl;
                       
            if (currentTerm.get_termsCount() > 0) {

                //Term has sub terms.                       
            }
        }

        //alert(termList);
    }, function (sender, args) {
        // console.log(args.get_message());
    });

}

_________________________________________________________________________

Here are some more properties you can get by replacing the names in the code,

  • _Sys_Nav_Title: the Navigation Node Title
  • _Sys_Nav_FriendlyUrlSegment: the Friendly Url, also represented by the FriendlyUrlSegment property of theNavigationTerm class.
  • _Sys_Nav_TargetUrl: the target URL, also represented by the TargetUrl property of the NavigationTermclass.
  • _Sys_Nav_TargetUrlForChildTerms: the target URL for child terms, also represented by the TargetUrlForChildTerms property of the NavigationTerm class.
  • _Sys_Nav_CatalogTargetUrl: the catalog target URL, also represented by the CatalogTargetUrl property of theNavigationTerm class.
  • _Sys_Nav_CatalogTargetUrlForChildTerms: the catalog target URL for child terms, also represented by the CatalogTargetUrlForChildTerms property of the NavigationTerm class.

For Updating the Terms Navigation Custom Properties:
The Key to understand, by executing the SetLocalCustomProperty method on the_Sys_Nav_Title property, I was able to set the Navigation Node Title:
   term.SetLocalCustomProperty("_Sys_Nav_Title", "myLocation");
If you use that same method with a property name that does not exist, it will create a custom property (which is then visible on the Custom Properties tab); just like the SetCustomProperty method does.
Save your changes to the term store when you’re finished:
   termStore.CommitAll();


There will be some circumstances that you'll need to get this Url from Code-behind, otherwise you can also use SharePoint Managed Site Navigation provides great In-Built functionality to create top-menu using Navigation Term Set.


Hope this helps :)

Thank you for reading my blog :)





No comments:

Post a Comment