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);  
      //Get the response from server to get the termsetId  
        //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_termLabel("UI Developer");  
           var termMatches = termSet.getTerms(lmi);  
           //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();  
               txField.setFieldValueByValue(oListItem, termFieldValue);  
               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());  
Item Added to List

1 comment: