0

I have to update taxonomy field data using REST API in SharePoint 2013. How to do that? I am using below code to update data. But how to update taxonomy field value?

$.ajax({
                url: data.d.results[0].__metadata.uri,
                type: "POST",
                contentType: "application/json;odata=verbose",
                data: JSON.stringify({
                    '__metadata': { 'type': 'SP.Data.PagesItem' },
                    "Module": $('[id$=txtModule0_ctl00_TextField]').val(),
                    "Story" :$('[id$=Story_ctl00_BooleanField]').is(':checked'),
                    "Image":{ Url: $('[id$=Image_ctl00_UrlFieldUrl]').val(), Description: "" },
                }),
                headers: {
                    "Accept": "application/json;odata=verbose",
                    "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                    "X-HTTP-Method": "MERGE",
                    "If-Match": data.d.results[0].__metadata.etag
                },
                success: function (data) {
                    alert('data saved');
                },
                error: function (data) {

                }
            });
  • You have a taxonomy field which is attached to a term set. From your code I am assuming that its a page field on a page layout, and you must be having a taxonomy picker control on the page. Correct me if I am wrong. – Asad Refai May 27 '15 at 10:39
  • Yes you are correct. In java script I got the term set value but using rest Api i am not able to update the value. – Trimantra Software Solution May 27 '15 at 10:46
  • If its a site column on page layout, and you want to save a page. I believe field data are stored with out any code. <PageFieldTaxonomyFieldControl:TaxonomyFieldControl ID="TaxonomyFieldControl1" FieldName="YourFieldName" runat="server"></PageFieldTaxonomyFieldControl:TaxonomyFieldControl> . My question would be are you trying to use page field values and using it to store some where else? – Asad Refai May 27 '15 at 10:57
  • Yes I know that. But I have a requirement that I have to store data using code base programmatically. How to do that using Rest Api? – Trimantra Software Solution May 27 '15 at 11:00
  • I have update taxonomy field values just by assigning TERM ID, My code was JSOM. But I believe you can try passing term id with REST as well. – Asad Refai May 27 '15 at 11:02
4

It depends whether taxonomy field is multi or single valued.

In case of single-valued taxonomy field, the following example demonstrates how to update its value:

function executeJson(url,method,headers,payload) 
{
    method = method || 'GET';
    headers = headers || {};
    headers["Accept"] = "application/json;odata=verbose";
    if(method == "POST") {
        headers["X-RequestDigest"] = $("#__REQUESTDIGEST").val();
    }      
    var ajaxOptions = 
    {       
       url: url,   
       type: method,  
       contentType: "application/json;odata=verbose",
       headers: headers
    };
    if (typeof payload != 'undefined') {
      ajaxOptions.data = JSON.stringify(payload);
    }  
    return $.ajax(ajaxOptions);
}

function updateListItem(webUrl, listTitle,itemId, itemPayload) {
    var endpointUrl = webUrl + "/_api/web/lists/GetByTitle('" + listTitle +  "')/items(" + itemId + ")";
    var headers = {};
    headers["X-HTTP-Method"] = "MERGE";
    headers["If-Match"] = "*";
    return executeJson(endpointUrl,'POST',headers,itemPayload);
}

Usage

var webUrl = _spPageContextInfo.webAbsoluteUrl;
var listTitle = 'Contacts';
var itemId = 1;
var taxFieldName = 'TaxCountry';  //<-tax field name
var itemPayload = {
             '__metadata' : {'type': 'SP.Data.ContactsListItem'},
             taxFieldName : {
                 '__metadata' : {'type': 'SP.Taxonomy.TaxonomyFieldValue'},
                 'Label': 'Norway',  //<-set term label here
                 'TermGuid':'aaa54679-ce8a-44c2-ba29-0df41ee7deca',  //<- set term guid here 
                 'WssId': -1
              }
          };   



updateListItem(webUrl,listTitle,itemId,itemPayload)
   .done(function(){
       console.log('Tax field valued has been updated');
   })
   .fail(function(error){
       console.log(JSON.stringify(error));
   });
  • I am following your post but I am not able to make this code working. I am not getting any error, code succeed but the field value does not updated. – Aakash Maurya Jul 16 '16 at 5:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.