2014-12-25 40 views

回答

8

您可以利用SP.Publishing.Navigation.NavigationTermSet.getAsResolvedByWeb Method來檢索導航術語集對象。下面的示例演示如何檢索導航術語集對象,並查找導航期限:

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function() { 
    SP.SOD.registerSod('sp.taxonomy.js', SP.Utilities.Utility.getLayoutsPageUrl('sp.taxonomy.js')); 
    SP.SOD.executeFunc('sp.taxonomy.js', 'SP.Taxonomy.TaxonomySession', function() { 
     SP.SOD.registerSod('sp.publishing.js', SP.Utilities.Utility.getLayoutsPageUrl('sp.publishing.js')); 
     SP.SOD.executeFunc('sp.publishing.js', 'SP.Publishing.Navigation.NavigationTermSet', function() { 

      var navTermSetId = 'ccef718f-fc01-4d27-b877-431f2e4bf136';  
      var navTermId = '1b04f1b2-f5f4-4c7b-a87f-28fb8665824b'; 
      loadNavigationTermSet(navTermSetId, 
       function(navTermSet){ 
        for(var i = 0;i < navTermSet.get_terms().get_count();i++) { 
        var navTerm = navTermSet.get_terms().getItemAtIndex(i);  
        if(navTerm.get_id().toString() == navTermId){ 
         console.log(navTerm.get_taxonomyName()); 
        } 
        } 
       }, 
       function(sender, args) 
       { 
        console.log('Request failed ' + args.get_message() + ':'+ args.get_stackTrace()); 
       } 
      );    
     });    
    }); 
}); 

其中

function loadNavigationTermSet(navTermSetId,success,error) 
{ 
    var ctx = SP.ClientContext.get_current(); 
    var taxonomySession = SP.Taxonomy.TaxonomySession.getTaxonomySession(ctx); 

    var termStore = taxonomySession.getDefaultSiteCollectionTermStore(); //retrieve default Term Store 
    var termSet = termStore.getTermSet(navTermSetId); 
    var navTermSet = SP.Publishing.Navigation.NavigationTermSet.getAsResolvedByWeb(ctx,termSet, ctx.get_web(), "GlobalNavigationTaxonomyProvider"); 

    ctx.load(navTermSet,'Terms'); 
    ctx.executeQueryAsync(function(){ 
     success(navTermSet); 
    }, 
    error); 
} 

例2

下面的例子演示如何檢索導航條款:

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function() { 
    SP.SOD.registerSod('sp.taxonomy.js', SP.Utilities.Utility.getLayoutsPageUrl('sp.taxonomy.js')); 
    SP.SOD.executeFunc('sp.taxonomy.js', 'SP.Taxonomy.TaxonomySession', function() { 
     SP.SOD.registerSod('sp.publishing.js', SP.Utilities.Utility.getLayoutsPageUrl('sp.publishing.js')); 
     SP.SOD.executeFunc('sp.publishing.js', 'SP.Publishing.Navigation.NavigationTermSet', function() { 

      var navTermSetId = 'ccef718f-fc01-4d27-b877-431f2e4bf136';  
      loadNavigationTerms(navTermSetId, 
       function(navTerms){ 
        for(var i = 0;i < navTerms.get_count();i++) { 
        var navTerm = navTerms.getItemAtIndex(i);  

        console.log(navTerm.get_id().toString()); 
        console.log(navTerm.get_title().get_value()); 
        console.log(navTerm.get_categoryImageUrl()); 
        } 
       }, 
       function(sender, args) 
       { 
        console.log('Request failed ' + args.get_message() + ':'+ args.get_stackTrace()); 
       } 
      );    
     });    
    }); 
}); 

其中

function loadNavigationTerms(navTermSetId,success,error) 
{ 
    var ctx = SP.ClientContext.get_current(); 
    var taxonomySession = SP.Taxonomy.TaxonomySession.getTaxonomySession(ctx); 

    var termStore = taxonomySession.getDefaultSiteCollectionTermStore(); //retrieve default Term Store 
    var termSet = termStore.getTermSet(navTermSetId); 
    var navTermSet = SP.Publishing.Navigation.NavigationTermSet.getAsResolvedByWeb(ctx,termSet, ctx.get_web(), "GlobalNavigationTaxonomyProvider"); 

    var navTerms = navTermSet.get_terms(); 
    ctx.load(navTerms,'Include(Id,Title,CategoryImageUrl)'); 
    ctx.executeQueryAsync(function(){ 
     success(navTerms); 
    }, 
    error); 
} 
+0

非常感謝您的回答。我真的幫助我很多。 現在我可以得到導航術語,但是當我在其屬性中訪問它時,如title,categoryImageUrl,它會拋出一個不存在的異常。或者有時候返回SP.Publishing.CustomizeString,它不能通過它自己的get_value函數來訪問(拋出一個異常「Value不會初始化......」)。你能解釋一下嗎?謝謝 – longvh92

+0

答案已經更新,看例子2 –