16

Is it possible to update a List Item's Lookup Field Value via the Rest API? I can retrieve the field value if I use $select and $expand, but I can't find documentation anywhere that explains how to update it.

20

Yes, you can. For each lookup field, the list has an implicit field that holds the id of the item in the target list. The name of this implicit field is the name of the lookup field plus Id. In Milind's answer, the lookup field is named State and the id of the item in the target list is 2. So, when you do the update using the REST API you would set StateId = 2.

var call = jQuery.ajax({
    url: _spPageContextInfo.webAbsoluteUrl +
        "/_api/Web/Lists/getByTitle('Sales Opportunities')/Items(5)",
    type: "POST",
    data: JSON.stringify({
        "__metadata": { type: "SP.Data.Sales_x0020_OpportunitiesListItem" },
        StateId: 2
    }),
    headers: {
        Accept: "application/json;odata=verbose",
        "Content-Type": "application/json;odata=verbose",
        "X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
        "IF-MATCH": item.__metadata.etag,
        "X-Http-Method": "PATCH"
    }
});
  • Was struggling updating a lookup field from a SpFx webpart and didn't know about the Id suffix, thanks a lot! ;) – Abbas Jun 22 '17 at 14:40
13

It depends whether Lookup field value is multiple or not, the following formats are used when updating field value via REST:

  • Single Lookup value format: LookupFieldName: LookupIdVal
  • Mutiple Lookup value format: LookupFieldName: {"results": [LookupIdVal1,LookupIdVal2] }

Examples

How to update single lookup value

var itemProperties = {
        "__metadata": { "type": 'SP.Data.TasksListItem' }, 
        "ProjectId": 1
    }; 
updateListItem('Tasks',4,itemProperties,
   function(){    
   },
   function(error){
    console.log(JSON.stringify(error));
   }
);

How to update multiple lookup value

var itemProperties = {
        "__metadata": { "type": 'SP.Data.TasksListItem' }, 
        "ProjectsId": {"results": [1,3] }
    }; 
updateListItem('Tasks',4,itemProperties,
   function(){    
   },
   function(error){
    console.log(JSON.stringify(error));
   }
);

where

function updateListItem(listName, itemId, itemProperties, success, failure) {

    $.ajax({
            url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items(" + itemId + ")",
            type: "POST",
            contentType: "application/json;odata=verbose",
            data: JSON.stringify(itemProperties),
            headers: {
                "Accept": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "X-HTTP-Method": "MERGE",
                "If-Match": '*'
            },
            success: function (data) {
                success();
            },
            error: function (data) {
                failure(data);
            }
    });
}

References

Manipulating list items in SharePoint Hosted Apps using the REST API

1

You can use SPServices' UpdateListItems method, like below:

$().SPServices({
   operation: "UpdateListItems",
   async: false,
   debug:true,
   listName: "Sales Opportunities",
   ID: 5,
   valuepairs: [["State", "2;#Rhode Island"]],
   completefunc: function (xData, Status) {
      alert(xData.responseText);
   }
});

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.