Forum Discussion

Dreamsat's avatar
Dreamsat
Brass Contributor
Jan 07, 2021

How to update a lookup field value of a list item in client side javascript?

I would like to update the value of a lookup field client side in javascript. I will use this clientside javascript in a content editor webpart. I am using the REST API of SharePoint Online. 

 

I try this but it is not working. What am I doing wrong?

 

 

<script type="text/javascript">

$(document).ready(function(){

//replace your list title

var item = {
"__metadata": { "type": "SP.Data.TestKH3ListItem" },
'WerkpakketId': 1
};


$.ajax({
url: "https://myCompany.sharepoint.com/sites/DMS-WF-Test-Infra/_api/web/lists/GetByTitle('TestKH3')/items(1)",
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(item),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "MERGE",
"If-Match": "*"
},
success: function (data) {
console.log(data + " success in updating item");
},
error: function (data) {
console.log(data);
}
});

})

 

 

When I try to update column "Title" from a listitem this script is working fine:

 

 

$(document).ready(function(){

//replace your list title

var item = {
"__metadata": { "type": "SP.Data.TestKH3ListItem" },
'Title': "Test123"
};


$.ajax({
url: "https://myCompany.sharepoint.com/sites/DMS-WF-Test-Infra/_api/web/lists/GetByTitle('TestKH3')/items(1)",
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(item),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "MERGE",
"If-Match": "*"
},
success: function (data) {
console.log(data + " success in updating item");
},
error: function (data) {
console.log(data);
}
});

})

 

No RepliesBe the first to reply

Resources