Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This is the most verbose way of accessing the OM that is possible.
- //This is so as to familiarize myself with the JSOM for the Taxonomy service.
- //It is the WORST kind of demo code!
- //My idea was just to be able to go to my O365 dev site and play in the console.
- //The SP.Taxonomy name space is found in _layouts/15/sp.taxonomy.js
- //Load the script
- var fileref = document.createElement('script');
- fileref.setAttribute("type","text/javascript");
- fileref.setAttribute("src", "_layouts/15/sp.taxonomy.js");
- document.getElementsByTagName("head")[0].appendChild(fileref);
- var context = SP.ClientContext.get_current();
- var taxonomySession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
- //How do you determine the Term Stores that are available?
- var termStores = taxonomySession.get_termStores();
- context.load(taxonomySession);
- context.load(termStores);
- var termStoresEnum;
- var currentTermStore;
- context.executeQueryAsync(function(){
- termStoresEnum = termStores.getEnumerator();
- console.log("Available Term Stores:\n");
- while (termStoresEnum.moveNext()) {
- currentTermStore = termStoresEnum.get_current();
- console.log( "Name: " + currentTermStore.get_name() +
- " ID:" + currentTermStore.get_id() +
- " Language: " + currentTermStore.get_languages() );
- }
- },
- function(){
- console.log("Error loading termstore.");
- });
- //What is returned by Execute Query Async?
- [{
- "SchemaVersion": "15.0.0.0",
- "LibraryVersion": "16.0.2524.1200",
- "ErrorInfo": null,
- "TraceCorrelationId": "2647739c-ebe6-d05b-9278-0fcd71853134"
- },
- 11, {
- "IsNull": false
- },
- 12, {
- "_ObjectIdentity_": "fec14c62-7c3b-561b-851b-c80d7802b224:ss:"
- },
- 14, {
- "IsNull": false
- },
- 15, {
- "_ObjectType_": "SP.Taxonomy.TaxonomySession",
- "_ObjectIdentity_": "fec14c62-7c3b-321b-851b-c80d7802b224:ss:",
- "OfflineTermStoreNames": [
- ]
- },
- 16, {
- "_ObjectType_": "SP.Taxonomy.TermStoreCollection",
- "_Child_Items_": [{
- "_ObjectType_": "SP.Taxonomy.TermStore",
- "_ObjectIdentity_": "fec14c62-7c3b-481b-851b-c80d7802b224:st:jBvd4CKJqEKgXy799vjajw==",
- "DefaultLanguage": 1033,
- "Id": "\/Guid(e0dd1b8c-8922-42a8-a05f-262df6f8da8f)\/",
- "IsOnline": true,
- "Languages": [
- 1033
- ],
- "Name": "Managed Metadata Service Application Proxy",
- "WorkingLanguage": 1033
- }]
- }
- ]
- //You might only have one termstore.
- //How do you get the default term store?
- var termStore = taxonomySession.getDefaultSiteCollectionTermStore();
- context.load(termStore);
- context.executeQueryAsync(function(){
- console.log( "Name: " + termStore.get_name() +
- " ID:" + termStore.get_id() +
- " Language: " + termStore.get_languages() );
- },
- function(){
- console.log("Something went wrong");
- });
- //How do you get the groups in a term store?
- var groups = termStore.get_groups();
- context.load(groups);
- var groupsEnum;
- context.executeQueryAsync(function(){
- groupsEnum = groups.getEnumerator();
- while(groupsEnum.moveNext()){
- var currentGroup = groupsEnum.get_current();
- console.log("Group name: " + currentGroup.get_name() + " ID: " + currentGroup.get_id() + "\n");
- }
- },
- function(){
- console.log("Could not load groups.");
- });
- //Once you know which groups can be accessed, you can look up specific groups by ID.
- var peopleGroup = groups.getById('59b265c3-d9e5-433e-acf8-a18a2e5b9839');
- var peopleTermSets = peopleGroup.get_termSets();
- var departments;
- context.load(peopleTermSets);
- context.executeQueryAsync(function(){
- var peopleTermSetsEnum = peopleTermSets.getEnumerator();
- while(peopleTermSetsEnum.moveNext()){
- if(peopleTermSetsEnum.get_current().get_name() === 'Department'){
- departments = peopleTermSetsEnum.get_current();
- break;
- }
- }
- },function(){});
- var terms = departments.getAllTerms();
- context.load(terms);
- var departmentNames = [];
- context.executeQueryAsync(function(){
- var termsEnum = terms.getEnumerator();
- while(termsEnum.moveNext()){
- var currentTerm = termsEnum.get_current();
- departmentNames.push({name: currentTerm.get_name(), id: currentTerm.get_id().toString(); });
- }
- }, function(){});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement