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.
4 Answers
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! ;)– AbbasJun 22, 2017 at 14:40
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
In case you're using Power Automate, when you use Send an HTTP request to SharePoint action, field name + Id
is not working. Use this syntax instead:
{
"lookup_field": {
"Id": <id>
}
}
-
What if I don't display the ID column of the lookup column but another column, let's say "product names". How do I pass multiple names of products to the lookup column named "product list" instead of the ID?– Der_FabFeb 4, 2022 at 14:10
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);
}
});