I have a requirement where I need to update the list item if it is already exist and create a new one if the item doesn't exist. Everything I need to manage from single method as I am taking data from custom form to update item. Is there any way to do this in sharepoint online rest api? I am using below method to do update item
public static UpdateSaveSectionC(formData: any,id:any): PromiseLike<string> {
// Return a promise
const siteURL= "https://abc.sharepoint.com/sites/process";
return new Promise((resolve, reject) => {
for (var i = 0; i < formData.Category.length; i++) {
const restUrl = siteURL + `/_api/web/lists/getbytitle('List')/items(${id[i]})`;
const headers = { 'accept': 'application/json;odata=verbose', 'content-Type': 'application/json;odata=verbose','X-HTTP-Method': 'MERGE','IF-MATCH': '*'};
const listTitle = "List";
const data = {
'__metadata': { 'type': 'SP.Data.' + listTitle + 'ListItem','results':[] },
Category: formData.Category[i],
Recommendationsuggestion: formData.Recommendationsuggestion[i],
}
Helper.executeJson(restUrl, "POST", headers, JSON.stringify($.extend(true,{}, data)))
.then((response) => {
// Resolve the request
resolve("success");
}).catch( (e) => {
if(e.responseJSON.error.message.value.indexOf("The request ETag value") != -1)
{
resolve("Please refresh the page, and resubmit your changes");
}
});
}
`