This error occurs since tag
variable is not defined.
About eTag
When updating or deleting items within SharePoint lists via REST you
must specify the Entity Tag (eTag) value that was returned with the
item during the initial query. This enables SharePoint to determine if
the item has changed since it was requested. Alternatively you can
tell SharePoint to perform the operation regardless by specifying * as
the eTag value. For example:
If-Match
: item.__metadata.etag can be used to specify the actual eTag value (‘item’ is the object returned from SharePoint containing
the list item in JSON format).
If-Match
: *
can be used to match any eTag value resulting in the operation being performed regardless of the actual value.
How to update List Item via SharePoint REST interface
function updateJson(endpointUri,payload, success, error)
{
$.ajax({
url: endpointUri,
type: "POST",
data: JSON.stringify(payload),
contentType: "application/json;odata=verbose",
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest" : $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "MERGE",
"If-Match": "*"
},
success: success,
error: error
});
}
function getItemTypeForListName(name) {
return"SP.Data." + name.charAt(0).toUpperCase() + name.slice(1) + "ListItem";
}
function updateListItem(webUrl,listTitle,listItemId,itemProperties,success,failure)
{
var listItemUri = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items(" + listItemId + ")";
var itemPayload = {
'__metadata': {'type': getItemTypeForListName(listTitle)}
};
for(var prop in itemProperties){
itemPayload[prop] = itemProperties[prop];
}
updateJson(listItemUri,itemPayload,success,failure);
}
Usage
var itemProperties = {'Title':'John Doe'};
updateListItem(_spPageContextInfo.webAbsoluteUrl,'Contacts',1,itemProperties,printInfo,logError);
function printInfo()
{
console.log('Item has been created');
}
function logError(error){
console.log(JSON.stringify(error));
}
References
Manipulating list items in SharePoint Hosted Apps using the REST API
etag
.