Couldn't update list item in SP 2013 using Rest
-
21-01-2021 - |
Question
I am trying to update a field in my list but I keep getting this error:
"A type named 'SP.Data.ttcuAnnListItem' could not be resolved by the model. When a model is available, each type name must resolve to a valid type"
This is the code:
function UpdateListItem(listName,itemId,titleVal){
var itemType = GetItemTypeForListName(listName);
var item = {
"__metadata": { "type": itemType },
"Title": titleVal
};
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items("+itemId+")",
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(item),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "MERGE",
"If-Match": "*"
},
success: function (data) {
alert('Success');
},
error: function (data) {
alert("Error");
$("#title1").html(JSON.stringify(data));
}
});
}
And the code for the GetItemTypeForListName function :-
// Get List Item Type metadata
function GetItemTypeForListName(name) {
return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";
}
La solution
Hello if you only want ListItemEntityTypeFullName once the you get the name from browser by browsing with below endpoint
your siteurl/_api/web/lists/getByTitle('ttcuAnn')?$select=ListItemEntityTypeFullName
or if you want to get in code below is the script
function getItemTypeList(listName){
return jQuery.ajax({
method: 'GET',
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('" + listName + "')?$select=ListItemEntityTypeFullName",
contentType: "application/json;odata=verbose",
headers: {
"Content-Type": "application/json;odata=verbose",
'Accept': "application/json;odata=verbose",
},
data: JSON.stringify(""),
});
}
function UpdateListItem(listName,itemId,titleVal,itemTypeList){
//var itemType = GetItemTypeForListName(listName);
var item = {
"__metadata": { "type": itemTypeList},
"Title": titleVal
};
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items("+itemId+")",
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(item),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "MERGE",
"If-Match": "*"
},
success: function (data) {
alert('Success');
},
error: function (data) {
alert("Error");
$("#title1").html(JSON.stringify(data));
}
});
}
getItemTypeList('ttcuAnn').then(function(data){UpdateListItem(listName,itemId,titleVal,data.d.ListItemEntityTypeFullName);},function(data){alert(''call failed!)})
Autres conseils
You need to confirm that the Metadata Type is in fact SP.Data.ttcuAnnListItem
.
You can do this easily by performing a Rest API GET request on the \items
endpoint. In the response you will see the correct type name. What you POST will need to match this. You can check this directly in the browser for a specific list using this url format https://<sitename>/_api/web/lists/getbytitle(<**ListName**>)/items
Or, If you wish to leave the function generic, you could update your GetItemTypeForListName
function to actually perform the GET request in order to parse it and return the correct type name.