使用 jsom 在 Sharepoint 中获取术语及其子术语

Get a term and its subterms in Sharepoint with jsom

我设法获取了一组术语中的所有术语,但我只对获取特定术语及其子术语感兴趣。我已经按照这个问题的答案https://sharepoint.stackexchange.com/questions/78514/accessing-terms-in-term-store-using-jsom-in-sharepoint-2013但是我只需要得到一个学期就无法得到

试试下面的代码片段:

   <script type="text/javascript">
$(document).ready(function(){   

    var scriptbase = _spPageContextInfo.webServerRelativeUrl + "_layouts/15/";

    $.getScript(scriptbase + "SP.Runtime.js",
        function () {
            $.getScript(scriptbase + "SP.js", function(){

                $.getScript(scriptbase + "SP.Taxonomy.js",function(){
                
                execOperation('3797f7a9-7902-47c1-a5bb-44b6ccaca1b4');
                
                }); 

            });
        }
    );

});

function execOperation(parentTermId) {
    var context = SP.ClientContext.get_current();
    var session = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
    var termStore = session.getDefaultSiteCollectionTermStore();
    
    //var parentTermId = '3797f7a9-7902-47c1-a5bb-44b6ccaca1b4'; //parent Term Id
    var parentTerm = termStore.getTerm(parentTermId);
    var terms = parentTerm.get_terms();  //load child Terms
    
    
    context.load(terms);
    context.executeQueryAsync(
    function(){
       //print child Terms
       for(var i = 0; i < terms.get_count();i++){
           var term = terms.getItemAtIndex(i);
           console.log(term.get_name());
           if(term.get_termsCount() > 0)
           {
              execOperation(term.get_id());
           }
           
       }
    
    }, 
    function(sender,args){
      console.log(args.get_message());    
    });

}
 
</script>

参考:

SharePoint2013 Taxonomy get all child term of a specific parent term using JSOM

更新一些关于调试代码片段的细节供您参考: