Advertisement
RobertKaucher

Managed Metadata Service JSOM Examples for SharePoint 2013 -

Feb 11th, 2014
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //This is the most verbose way of accessing the OM that is possible.
  2. //This is so as to familiarize myself with the JSOM for the Taxonomy service.
  3. //It is the WORST kind of demo code!
  4. //My idea was just to be able to go to my O365 dev site and play in the console.
  5. //The SP.Taxonomy name space is found in _layouts/15/sp.taxonomy.js
  6.  
  7. //Load the script
  8. var fileref = document.createElement('script');
  9. fileref.setAttribute("type","text/javascript");
  10. fileref.setAttribute("src", "_layouts/15/sp.taxonomy.js");
  11. document.getElementsByTagName("head")[0].appendChild(fileref);
  12.  
  13.  
  14. var context = SP.ClientContext.get_current();
  15. var taxonomySession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
  16.  
  17. //How do you determine the Term Stores that are available?
  18. var termStores = taxonomySession.get_termStores();
  19.  
  20. context.load(taxonomySession);
  21. context.load(termStores);
  22.  
  23. var termStoresEnum;
  24. var currentTermStore;
  25.  
  26. context.executeQueryAsync(function(){
  27.         termStoresEnum = termStores.getEnumerator();
  28.      
  29.         console.log("Available Term Stores:\n");
  30.        
  31.         while (termStoresEnum.moveNext()) {
  32.             currentTermStore =  termStoresEnum.get_current();
  33.             console.log( "Name: " + currentTermStore.get_name() +
  34.                          " ID:" + currentTermStore.get_id() +
  35.                          " Language: " + currentTermStore.get_languages() );
  36.         }
  37.     },
  38.     function(){
  39.         console.log("Error loading termstore.");
  40.     });
  41.  
  42.  
  43. //What is returned by Execute Query Async?
  44. [{
  45.         "SchemaVersion": "15.0.0.0",
  46.         "LibraryVersion": "16.0.2524.1200",
  47.         "ErrorInfo": null,
  48.         "TraceCorrelationId": "2647739c-ebe6-d05b-9278-0fcd71853134"
  49.     },
  50.     11, {
  51.         "IsNull": false
  52.     },
  53.     12, {
  54.         "_ObjectIdentity_": "fec14c62-7c3b-561b-851b-c80d7802b224:ss:"
  55.     },
  56.     14, {
  57.         "IsNull": false
  58.     },
  59.     15, {
  60.         "_ObjectType_": "SP.Taxonomy.TaxonomySession",
  61.         "_ObjectIdentity_": "fec14c62-7c3b-321b-851b-c80d7802b224:ss:",
  62.         "OfflineTermStoreNames": [
  63.  
  64.         ]
  65.     },
  66.     16, {
  67.         "_ObjectType_": "SP.Taxonomy.TermStoreCollection",
  68.         "_Child_Items_": [{
  69.             "_ObjectType_": "SP.Taxonomy.TermStore",
  70.             "_ObjectIdentity_": "fec14c62-7c3b-481b-851b-c80d7802b224:st:jBvd4CKJqEKgXy799vjajw==",
  71.             "DefaultLanguage": 1033,
  72.             "Id": "\/Guid(e0dd1b8c-8922-42a8-a05f-262df6f8da8f)\/",
  73.             "IsOnline": true,
  74.             "Languages": [
  75.                 1033
  76.             ],
  77.             "Name": "Managed Metadata Service Application Proxy",
  78.             "WorkingLanguage": 1033
  79.         }]
  80.     }
  81. ]
  82.  
  83. //You might only have one termstore.
  84. //How do you get the default term store?
  85.  
  86. var termStore = taxonomySession.getDefaultSiteCollectionTermStore();
  87. context.load(termStore);
  88. context.executeQueryAsync(function(){
  89.         console.log( "Name: " + termStore.get_name() +
  90.                          " ID:" + termStore.get_id() +
  91.                          " Language: " + termStore.get_languages() );
  92.     },
  93.     function(){
  94.         console.log("Something went wrong");
  95.     });
  96.  
  97. //How do you get the groups in a term store?
  98.  
  99. var groups = termStore.get_groups();
  100. context.load(groups);
  101. var groupsEnum;
  102.  
  103. context.executeQueryAsync(function(){
  104.                 groupsEnum = groups.getEnumerator();
  105.  
  106.                 while(groupsEnum.moveNext()){
  107.                     var currentGroup = groupsEnum.get_current();
  108.                     console.log("Group name: " + currentGroup.get_name() + " ID: " + currentGroup.get_id() + "\n");
  109.                 }
  110.             },
  111.             function(){
  112.                 console.log("Could not load groups.");
  113.             });
  114.  
  115. //Once you know which groups can be accessed, you can look up specific groups by ID.
  116. var peopleGroup = groups.getById('59b265c3-d9e5-433e-acf8-a18a2e5b9839');
  117.  
  118. var peopleTermSets = peopleGroup.get_termSets();
  119. var departments;
  120. context.load(peopleTermSets);
  121. context.executeQueryAsync(function(){
  122.     var peopleTermSetsEnum = peopleTermSets.getEnumerator();
  123.     while(peopleTermSetsEnum.moveNext()){
  124.         if(peopleTermSetsEnum.get_current().get_name() === 'Department'){
  125.             departments = peopleTermSetsEnum.get_current();
  126.             break;
  127.         }
  128.     }
  129. },function(){});
  130.  
  131. var terms = departments.getAllTerms();
  132. context.load(terms);
  133. var departmentNames = [];
  134.  
  135. context.executeQueryAsync(function(){
  136.     var termsEnum = terms.getEnumerator();
  137.     while(termsEnum.moveNext()){
  138.         var currentTerm = termsEnum.get_current();
  139.         departmentNames.push({name: currentTerm.get_name(), id: currentTerm.get_id().toString(); });
  140.     }
  141. }, function(){});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement