Retrieve id after REST API Insert

Today I needed to use REST API to insert data into a list and I also needed to retrieve the id of the new inserted item. The code below successfully inserted my data into the list but the success handler wasn’t firing.

$.ajax({
	url: fullUrl,
	type: "POST",
	data: JSON.stringify({
		'__metadata': { 'type': 'SP.Data.Travel_x0020_RequestListItem' },
		'Guidance1': guidance1,
		'Guidance2': guidance2,
		'Guidance3': guidance3
		}),
	headers: {
		"accept": "application/json;odata=verbose",
		"content-type": "application/json;odata=verbose",
		"X-RequestDigest": $("#__REQUESTDIGEST").val()
	},
	success: function(data){
		alert("Retried id of new inserted item"+data.d.Id);
		console.log(data.d.Id);
	},
	error: function(data){
		alert("Epic fail");
	}
});
}

After a bit of googling and being on SharePoint stack exchange, it was suggested that I change “type” to “method”. Once I did that, it worked.

$.ajax({
	url: fullUrl,
	method: "POST",
	data: JSON.stringify({

I hope this helps someone.

SharePoint

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: