Question

I am trying to get the Local Properties for the Navigation Term that is currently being viewed.

For example... If I navigate to http://sp2013/about/team I need to get the Local Custom Properties for the term Team.

Current Solution (performance issues):

  1. Get the Term in the URL window.location.pathname of the current page
  2. Get the default Term Store
  3. Get Group that Term is in
  4. Get Term Set that Term is in
  5. Get Term About (from /about/team)
  6. Get Term Team (from /about/team) and then read Custom Properties.

The performance of this is quite bad because there are so many executeQueryAsync's being called, and the deeper the URL the more queries there will be!!

Other Possible Solutions??

  • Is there a way to just get the Navigation Term that is being viewed on current page with one query?
  • Is there a way to search ALL the Terms in a Term Set a retrieve Term based on Friendly Url (where Friendly Url = window.location.pathname)?
Was it helpful?

Solution 2

Solved the performance issue with the help of Robert as well as came up with an optimised solution by using getTerms() to search the entire Term Store for required Term:

// Get SP Context
var context = SP.ClientContext.get_current();
// Get the default Term Store for context
var session = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
var termStore = session.getDefaultSiteCollectionTermStore();
// Set up Term Query for termStore.getTerms()
var termQuery = new SP.Taxonomy.LabelMatchInformation(context);
    // The language code identifier (LCID) of the Term.labels to be matched
    termQuery.set_lcid(1033);
    // Determines if only terms available for tagging are returned
    termQuery.set_trimUnavailable(false);
    // The Label of the Term to get
    termQuery.set_termLabel("My Term");
// Get Terms based on termQuery
var termsStoreTerms = termStore.getTerms(termQuery);
// Load em' and run execute query!
context.load(session);
context.load(termStore);
context.load(termsStoreTerms);
context.executeQueryAsync(
    function(){
        // Get all the terms based on search
        var termsEnum = termsStoreTerms.getEnumerator();

        // Loop through results
        while(termsEnum.moveNext()){
            // Current Item in Enumerator Loop
            var currentTerm = termsEnum.get_current();

            // Get Local Custom Properties
            var termLocalCustomProperties = currentTerm.get_localCustomProperties();
        }
    }, function(sender, args){
        // Failure getting Term
        var error = 'Failure getting Term: ' + args.get_message() + '\n' + args.get_stackTrace
        alert(error);
    });

OTHER TIPS

You could use _spFriendlyUrlPageContextInfo.termId to get the Term information(GUID) for the current page.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top