码迷,mamicode.com
首页 > 编程语言 > 详细

Sharepoint 2013 Retrieve Taxonomy Term Store via Javascript

时间:2014-12-09 14:00:44      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   os   sp   for   java   

This blog will show you how to retrieve term store, term group, term set and term via JavaScript

1. First, you need understand Managed Metadata Service, it used for storing metadata, and have the database in dependent, then all the site collection can reuse the data, such as site column(Metadata field), Metadata Navigation and search data. here is its construct:

Managed Metadata Service -> Term Stores -> Term Groups -> Term Sets -> Terms -> Term

2. Second, since you retrieve metadata via JS, so sharepoint need provide the relevant api, here are the JS file provided by sharepoint OOB file:

SP.Runtime.js, SP.js, SP.Taxonomy.js, then you must make sure load those js before you call the api(only sharepoint 2013 provide the api).

3. Implement content in detail

Note: I get the default term store directly, in default, sharepoint don‘t set it, so you have to set in Admin center, or the code will encounter issues.

here is the example to retrieve the metadata navigation and print out.

// JavaScript source code

<script>

_spBodyOnLoadFunctionNames.push('Start');


function Start(){
    var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";

    $.getScript(scriptbase + "SP.Runtime.js",

    function () {

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

            $.getScript(scriptbase + "SP.Taxonomy.js", function () {
                var termsList = "";
                context = SP.ClientContext.get_current();
                
                getTermStores();

                console.log("success");
            });

        });

    });
}

function getTermStores() {
 
    this.session = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
    //this.termStores = this.session.get_termStores();
    this.groups = session.getDefaultSiteCollectionTermStore().get_groups();
    context.load(this.groups);

    context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
 
function onQuerySucceeded(sender, args){
    var groupList = "List: \n";
    var groupsEnum = groups.getEnumerator();
    while (groupsEnum.moveNext()) {
        var currentGroup = groupsEnum.get_current();
        var groupName = currentGroup.get_name();
        if(groupName == "Navigation"){
            this.termSets = currentGroup.get_termSets();

            context.load(this.termSets, 'Include(Name, Id, CustomProperties)');
 
            context.executeQueryAsync(Function.createDelegate(this, this.onTermSetsSucceeded), Function.createDelegate(this, this.onTermSetsFailed));
            
            groupList += groupName + "\n";
            console.log(groupList)
        }

        
    }
       
}

function onQueryFailed(sender, args){
 
    //failure loading termstores.
    console.log("failed to get groups");
}

function onTermSetsSucceeded(sender, args){
    var termSetEnum = this.termSets.getEnumerator();
 
    var termSetList = "Term Sets: \n"
 
    while(termSetEnum.moveNext()){
 
        var currentTermSet = termSetEnum.get_current();
 
        var termSetName = currentTermSet.get_name();
        if(termSetName == "NavTop"){
            this.terms = currentTermSet.get_terms();
            context.load(this.terms, 'Include(Name, Id, CustomProperties, LocalCustomProperties, TermsCount)');
            context.executeQueryAsync(Function.createDelegate(this, this.onTermsSucceeded), Function.createDelegate(this, this.onTermsFailed));           
           
        }
      
    }
 
   
}

function onTermSetsFailed(sender, args){

    alert("failed to get termsets");
}


function onTermsSucceeded(sender, args){

    var termsEnum = this.terms.getEnumerator();
 
    while (termsEnum.moveNext()) {
 
        var currentTerm = termsEnum.get_current();
 
        var termName = currentTerm.get_name();
 
        var termId = currentTerm.get_id();
        //currentTerm.get_objectData().get_properties()["CustomProperties"]["test"]
        var propertied = currentTerm.get_localCustomProperties();
        var groupValues = propertied.group != null ? propertied.group : "";
       
        console.log("propertied : " + propertied._Sys_Nav_HoverText + "\n" + propertied._Sys_Nav_SimpleLinkUrl + "\n" + groupValues);
 
        console.log(termName);
        //Check if term has child terms
 
        if (currentTerm.get_termsCount() > 0) {
 
            //Term has sub terms.
 
            recursiveTerms(currentTerm, 1);
 
        }
 

    }

}

function onTermsFailed(sender, args){

    alert("failed to get termsets");
}

 
function recursiveTerms(currentTerm, nestedLoop) {
 
    //Loop count for formatting purpose.
 
    var loop = nestedLoop + 1;
 
    //Get Term child terms
 
    var terms = currentTerm.get_terms();
 
    context.load(terms);
 
    context.executeQueryAsync(
 
    function () {
 
        var termsEnum = terms.getEnumerator();
 
        while (termsEnum.moveNext()) {
 
            var newCurrentTerm = termsEnum.get_current();
 
            var termName = newCurrentTerm.get_name();
 
            termId = newCurrentTerm.get_id();
 
            //Tab Out format.
            var loops= "";
            for (var i = 0; i < loop; i++) {
                //  termsList += "\t";
                loops +="\t"; 
 
            }
 
            console.log(loops + termName);
 
            //Check if term has child terms.
 
            if (currentTerm.get_termsCount() > 0) {
 
                //Term has sub terms.
 
                recursiveTerms(newCurrentTerm, loop);
 
            }
 
        }
 
    },
 
    function () {
        alert("fie");
        //failure to load terms
 
    });
 
}



</script>


The source link: https://cann0nf0dder.wordpress.com/2013/04/09/accessing-taxonomy-term-store-with-jsom/




Sharepoint 2013 Retrieve Taxonomy Term Store via Javascript

标签:style   blog   http   io   ar   os   sp   for   java   

原文地址:http://blog.csdn.net/tristan_dong/article/details/41822803

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!