Sometimes I need to create a new item with an attachment to a list in Sharepoint by using REST API.
What I used to do to achieve this task was to first create the item in the Sharepoint list by using the code below:
$.ajax({
url: `${_spPageContextInfo.webAbsoluteUrl}/_api/web/lists/GetByTitle('${listName}')/items`,
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(item),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": document.getElementById('__REQUESTDIGEST').value,
}
}).then( (data) => {
const id = data.d.Id;
// function to upload attachment here
});
This works just fine. However, I am trying to drop using jquery and move to ES6 features like fetch. With fetch I am able to create a Sharepoint List Item with the code below, but I don't get the posted data in the callback, and therefore I am missing the newly created item Id that I need in order to upload the attachment
const url = `${_spPageContextInfo.webAbsoluteUrl}/_api/web/lists/GetByTitle('${resourceList}')/items`;
const options = {
method: "POST",
credentials: "same-origin",
headers: {
"Accept": "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"X-RequestDigest": document.getElementById('__REQUESTDIGEST').value
},
body: JSON.stringify(item)
};
fetch(url, options)
.then( (data) => {
const id = data.d.Id;
// not working, how can I get the newly created item Id in this callback?
});
Does anyone know how can i achieve the same result I used to get with jQuery ajax, by using javascript fetch?