Showing posts with label Term Set. Show all posts
Showing posts with label Term Set. Show all posts

Thursday, June 19, 2014

Adding Item to list with Managed Metadata Column using Javascript Client Object Model in SharePoint 2013

On my previous post, I went through how to create term group, term set and terms in SharePoint 2013 using javascript object model. 

In this post, I will utilize that term set and then will check how to create item which contain Managed metadata Column.

Create a list will Managed Metadata Column






To update managed metadata column, you need to get the associate termSet for the column and also get the term Id from that termset.

Below is the code to add the item with managed metadata column. Read Comments in code for more description.


 jQuery(function($) {  
      var scriptbase = _spPageContextInfo.webServerRelativeUrl + "_layouts/15/";  
      $.getScript(scriptbase + "SP.Runtime.js",  
     function () {  
       $.getScript(scriptbase + "SP.js", function(){  
            $.getScript(scriptbase + "SP.Taxonomy.js", ExecuteTaxonomyFunctions);  
       });  
     }  
   );  
 });  
 function ExecuteTaxonomyFunctions()  
 {  
      var context = SP.ClientContext.get_current();  
      var list = context.get_web().get_lists().getByTitle("Resources");  
    
      var field = list.get_fields().getByInternalNameOrTitle("ResourceType");  
      var txField = context.castTo(field, SP.Taxonomy.TaxonomyField);  
      context.load(field);  
      context.load(txField);  
      //Get the response from server to get the termsetId  
      context.executeQueryAsync(function(){  
        //Get the term set ID  
           var termSetId = txField.get_termSetId().toString();  
           var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);  
           var termStore = taxSession.getDefaultSiteCollectionTermStore();  
           var termSet = termStore.getTermSet(termSetId);  
   
           // Get the matching terms based on names  
           var lmi = new SP.Taxonomy.LabelMatchInformation(context);  
           lmi.set_lcid(1033);  
           lmi.set_trimUnavailable(true);  
           lmi.set_termLabel("UI Developer");  
                
           var termMatches = termSet.getTerms(lmi);  
           context.load(termMatches);  
           //Get the matching term and then add to list item  
           context.executeQueryAsync(function () {  
           if (termMatches && termMatches.get_count() > 0)  
           {  
               // Get the first matching term. As I know it has only one  
               // You need to iterate and get the correct term if you have same name terms  
               var term = termMatches.get_item(0);  
              
              // Add Item to list   
               var itemCreateInfo = new SP.ListItemCreationInformation();  
               var oListItem = list.addItem(itemCreateInfo);  
               oListItem.set_item('Title', 'Akash Karda');  
               var termFieldValue = new SP.Taxonomy.TaxonomyFieldValue();  
               termFieldValue.set_label(term.get_name());  
               termFieldValue.set_termGuid(term.get_id().toString());  
               termFieldValue.set_wssId(-1);  
               txField.setFieldValueByValue(oListItem, termFieldValue);  
               oListItem.update();       
   
               context.executeQueryAsync(function () {  
                 alert('Item Added');  
                }, function (sender, args) {  
                  alert(args.get_message() + '\n' + args.get_stackTrace());  
                });  
   
           }  
         }, function (sender, args) {  
            alert(args.get_message() + '\n' + args.get_stackTrace());  
         });  
      },function(sender,args){  
           alert(args.get_message());  
      });  
 }  
   
Item Added to List

Working with Taxonomy and Javascript object model in SharePoint 2013

In SharePoint 2010, it was not possible to work with taxonomy types using javascript object model. SharePoint 2013 has introduced a new feature to work with taxonomy by using SP.Taxonomy.js

Here are some basic operations of taxonomy using javascipt object model.
1. Create a taxonomy Term Group
2. Create a taxonomy Term Set
3. Create taxonomy Terms
4. Read taxonomy group, termset and terms.


 var groupGuid="cd90a241-2737-48f7-b5a3-43fab9cdc0cb";  
 var termSetGuid="552ae870-07ce-4934-b5c7-c068022a6873";  
 jQuery(function($) {  
      var scriptbase = _spPageContextInfo.webServerRelativeUrl + "_layouts/15/";  
      $.getScript(scriptbase + "SP.Runtime.js",  
     function () {  
       $.getScript(scriptbase + "SP.js", function(){  
            $.getScript(scriptbase + "SP.Taxonomy.js", ExecuteTaxonomyFunctions);  
       });  
     }  
   );  
 });  
 function ExecuteTaxonomyFunctions()  
 {  
      CreateTaxonomyGroup();  
      CreateTermSet();  
      CreateTerms();  
      ReadTerms();  
 }  
 function CreateTaxonomyGroup()  
 {  
      var context = SP.ClientContext.get_current();  
      //Get the taxonomy session object  
   var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);  
   //Get the default termstore for the site collection  
   var termStore =taxSession.getDefaultSiteCollectionTermStore();  
      //New group with new guid  
      var newGroup = termStore.createGroup("IT",groupGuid);  
      context.load(newGroup);  
      context.executeQueryAsync(function(){  
            alert("Group Created with Name : "+ newGroup.get_name());  
      },function(sender,args){  
           alert(args.get_message());  
      });  
 }  
 function CreateTermSet()  
 {  
   var context = SP.ClientContext.get_current();  
   //Get the taxonomy session object  
   var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);  
   //Get the default termstore for the site collection  
   var termStore =taxSession.getDefaultSiteCollectionTermStore();  
   //Get the term group just created by using GUID  
   var termGroup= termStore.getGroup(groupGuid);  
   //Create a termset with new GUID, 1033 is lcid for US English  
   var termSet= termGroup.createTermSet("Developers", termSetGuid, 1033);  
        context.load(termSet);  
      context.executeQueryAsync(function(){  
            alert("Term Set Created with Name : "+ termSet.get_name());  
      },function(sender,args){  
           alert(args.get_message());  
      });  
 }  
 function CreateTerms()  
 {  
   var context = SP.ClientContext.get_current();  
   //Get the taxonomy session object  
   var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);  
   //Get the default termstore for the site collection  
   var termStore =taxSession.getDefaultSiteCollectionTermStore();  
   //Get the term group just created by using GUID  
   var termGroup= termStore.getGroup(groupGuid);  
      //Get the termset by GUID  
      var termSet=termStore.getTermSet(termSetGuid);  
      //Create term with new GUID  
      var term1= termSet.createTerm("Developer", 1033, "1c88007a-b6f0-4175-b91b-ac02f95376d9");  
      var term2= termSet.createTerm("UI Developer", 1033, "676186d9-434e-4998-a050-18043ab66d08");  
       context.load(term1);  
       context.load(term2);  
      context.executeQueryAsync(function(){  
            alert("Term Created with Names : "+ term1.get_name()+ "," + term2.get_name());  
      },function(sender,args){  
           alert(args.get_message());  
      });  
 }  
 function ReadTerms()  
 {  
      var context = SP.ClientContext.get_current();  
      //Get the taxonomy session object  
   var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);  
   //Get the default termstore for the site collection  
   var termStore =taxSession.getDefaultSiteCollectionTermStore();  
   //Get the term group just created by using GUID  
   var termGroup= termStore.getGroup(groupGuid);  
      //Get the termset by GUID  
      var termSet=termStore.getTermSet(termSetGuid);  
      var terms= termSet.getAllTerms();  
      context.load(termGroup);  
      context.load(termSet);  
      context.load(terms);  
      context.executeQueryAsync(function(){  
            var result = "Group:"+ termGroup.get_name()+" \n";  
            result+= "Term Set:"+ termSet.get_name()+" \n";  
            var termEnumerator = terms.getEnumerator();  
            result+= "Terms: \n";  
                while(termEnumerator.moveNext()){  
                     var currentTerm = termEnumerator.get_current();  
                     result+= currentTerm.get_name() + "\n";  
                }   
       alert(result);  
      },function(sender,args){  
           alert(args.get_message());  
      });  
 }  

Below is the result snapshot of terms created.

I added a another post which shows you how to add item in list which contain metadata column using javascript object model.