Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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)?
share|improve this question
    
Why are there many executeQueryAsync's? Can't you just load the last part you need and then call executeQueryAsync once? –  Robert Lindgren Mar 27 at 7:47
    
@RobertLindgren you just blew my mind! Thanks so much for your direction, all this time I thought I had to first load the object before accessing it, problem solved I will add my updated code. Tnx again! –  pagemedias Apr 1 at 2:07

2 Answers 2

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

share|improve this answer
    
Can't find this object, in code or documentation, did you spell it correctly? Could you link the page concerning it? –  Christoffel de Gruyter Aug 20 at 19:56
    
It's not a code object (if it exists) it is a JavaScript object, so it should be available in the dev console of a browser or similar (again, if it exists, I have not double checked ) –  Robert Lindgren Aug 20 at 20:10
1  
as @RobertLindgren mentioned, It is a JavaScript Object and I figured it out using Firebug Console on the Browser. I used it in couple of my CSWP Display Templates to get the Current Page Term(Title, TermID) _spFriendlyUrlPageContextInfo.termId for GUID and _spFriendlyUrlPageContextInfo.title for Term –  user32668 Aug 21 at 14:10
    
If your site uses managed metadata navigation, sharepoint adds this javascript snippet server-side (before page load) in the global namespace. Zero performance hit grabbing the value: var _spFriendlyUrlPageContextInfo = { termId: '3ab1ae31-1f09-4195-b0dd-ecd6b40ca0e3', termSetId: 'd5c96b1b-8bfc-4625-bf11-862a7f296933', termStoreId: 'a6db5754-4226-4c8e-a928-a6c53390a270', title: 'Term Display Name' }; –  hitdrumhard Nov 21 at 22:04
up vote 1 down vote accepted

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);
    });
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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