Taxonomy sp.js

Utilities.GetTermById = function (TermSetId, TermGuid, success, error) {
 var context = SP.ClientContext.get_current();
 var session = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
 var termStore = session.getDefaultSiteCollectionTermStore();
 var termSet = termStore.getTermSet(TermSetId);
 var terms = termSet.get_terms();

context.load(termSet);
 context.load(terms);
 context.executeQueryAsync(function () {
 var termEnumerator = terms.getEnumerator();
 var allTerms = [];
 while (termEnumerator.moveNext()) {
 var currentTerm = termEnumerator.get_current();
 if (TermGuid != '') {
 if (currentTerm.get_id().toString() == TermGuid) {
 allTerms.push(currentTerm);
 break;
 }
 }
 else
 allTerms.push(currentTerm);
 }
 success(allTerms);
 }, function (sender, args) {
 console.log(args.get_message());
 });
}

/* retourne un tableau des terms d'une termSet
Cet appel retourne les élémens de 2 niveaux 
la fontion retourne aussi ID de parent
*/
Utilities.GetTermSetItems = function (TermSetId, success, error) {
 var context = SP.ClientContext.get_current();
 var session = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
 var termStore = session.getDefaultSiteCollectionTermStore();
 var termSet = termStore.getTermSet(TermSetId);

var terms = termSet.get_terms();

context.load(termSet);
 context.load(terms);
 context.executeQueryAsync(function () {
 var termEnumerator = terms.getEnumerator();
 var allTerms = [];
 while (termEnumerator.moveNext()) {
 var currentTerm = termEnumerator.get_current();
 
 allTerms.push(currentTerm);
 }
 success(TermSetId,allTerms);
 }, function (sender, args) {
 console.log(args.get_message());
 });
}


/* retourne la liste des terms d'un term passé en ID
*/
Utilities.GetTermTerms = function (TermSetId,termId, success, error) {
 var context = SP.ClientContext.get_current();
 var session = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
 var termStore = session.getDefaultSiteCollectionTermStore();
 var termSet = termStore.getTermSet(TermSetId);
 var term = termSet.getTerm(termId);
 var terms = term.get_terms();

context.load(term);
 context.load(terms);
 context.executeQueryAsync(function () {
 
 var termEnumerator = terms.getEnumerator();
 var allTerms = [];
 while (termEnumerator.moveNext()) {
 var currentTerm = termEnumerator.get_current();

allTerms.push(currentTerm);
 }
 success(term, allTerms);
 }, function (sender, args) {
 console.log(args.get_message());
 });
}


/* retourne la liste des terms de parent de term passé en ID
*/
Utilities.GetParentTermTerms = function (TermSetId, termId, success, error) {
 var context = SP.ClientContext.get_current();
 var session = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
 var termStore = session.getDefaultSiteCollectionTermStore();
 var termSet = termStore.getTermSet(TermSetId);
 var parent = termSet.getTerm(termId).get_parent();
 var terms = parent.get_terms();

context.load(parent);
 context.load(terms);
 context.executeQueryAsync(function () {

var termEnumerator = terms.getEnumerator();
 var allTerms = [];
 while (termEnumerator.moveNext()) {
 var currentTerm = termEnumerator.get_current();

allTerms.push(currentTerm);
 }
 success(parent, allTerms);
 }, function (sender, args) {
 console.log(args.get_message());
 });
}

/* retourne un term by ID
*/
Utilities.GetTermById = function (TermSetId, termId, success, error) {
 var context = SP.ClientContext.get_current();
 var session = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
 var termStore = session.getDefaultSiteCollectionTermStore();
 var termSet = termStore.getTermSet(TermSetId);

var term = termSet.getTerm(termId);
 context.load(terms);
 context.executeQueryAsync(function () {
 success(term);

}, function (sender, args) {
 console.log(args.get_message());
 });
}
Advertisements
This entry was posted in sharepoint online, Taxpnomy. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s