The problem I am having is when I make a PATCH call to the SharePoint LookUp field. Every time I make the call all the Id´s already in the multivalue lookup column gets overwritten. My temporary fix is to retrieve all Id´s in the LookUp field and then add the new Id to the array that contains all the existing Id´s and then make the PATCH call with that array . Shouldn't the PATCH call just add the new id to the LookUp field without erasing the already existing Ids in the field? This problem feels tricky to explain. Hope I am clear enough. Thanks for all the help!
The code:
function linkContract(ajaxData) {
var def = jQuery.Deferred();
//Url
var url = `${_spPageContextInfo.webAbsoluteUrl}/_api/web/lists/getbytitle('Avtal')/items('${window._cache.avtalsInfoId}')`;
//requestHeaders
var requestHeaders = {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-HTTP-Method": "PATCH",
"IF-MATCH": `${window._cache.avtal_eTag}`,
"X-RequestDigest": jQuery('#__REQUESTDIGEST').val()
}
//Array with old Id´s that I add the new Id to and then PATCH it to the column
window._cache.relateradeAvtalArray.push(parseInt(ajaxData.RelateradeAvtalId));
//Data
var data = {
__metadata: {
"type": "SP.Data.AvtalItem"
},
RelateradeAvtalId: {
'results': window._cache.relateradeAvtalArray//<--Array with old and new id´s
},
};
//requestBod
var requestBody = JSON.stringify(data);
//Post
var postLookId = jQuery.ajax({
url: url,
type: "PATCH",
headers: requestHeaders,
data: requestBody
});
//Done
postLookId.done(function (data, textStatus, jqXHR) {
def.resolve(data);
});
//Fail
postLookId.fail(function (jqXHR, textStatus, errorThrown) {
})
return def.promise();
}
PATCH
method allows partial updates of entities but not properties of entity, in another words properties specified in request payload will be replaced while entity's current state will be preserved.
For example, with the following request
Url: /_api/web/lists/getbytitle(Tasks)/items(1)
Method. POST
Headers: {"Accept":"application/json;odata=verbose","Content-Type":"application/json; odata=verbose","X-HTTP-Method":"MERGE","If-Match":"\"36\"","X-RequestDigest":"0x6B24D8B9C10F251812478A2D313EBD36D5373993A11CF9EF1D8414555C5E1CC1D0AD596633E2AEF4D5194CB534CF25006C913BDEC885F6914B4D2803DAB6FC04,27 Jan 2018 13:17:32 -0000"}
Data: {"__metadata":{"type":"SP.Data.TasksListItem"},"PredecessorsId":{"__metadata":{"type":"Collection(Edm.Int32)"},"results":[3]}}
the Predecessors
field value will be replaced in existing list item resource of Tasks list
To merge Lookup field value you could consider the following solution: