Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 6 down vote accepted

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"
    }
});
share|improve this answer
    
That did it, Thanks Rob! –  Omar Stewey Dec 9 '13 at 21:17

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

share|improve this answer

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);
   }
});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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