使用javascript获取导航术语

时间:2014-12-25 03:32:05

标签: javascript sharepoint sharepoint-2013

是否有人尝试使用Javascript API在Sharepoint中获取导航术语?我在MSDN中找到了代码,但没有任何线索可以使用它。

http://msdn.microsoft.com/en-us/library/office/jj994618(v=office.15).aspx

您能否告诉我如何获取当前页面导航条款或使用javascript获取导航条款?

感谢。

1 个答案:

答案 0 :(得分:9)

您可以使用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);
}