I try to fetch items from list in SharePoint 2019 using Rest API but the request fail with the following response Message:
"could not be resolved by the model. When a model is available, each type name must resolve to a valid type"
{"odata.error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"A type named 'SP.Data.Metadata\":\"http://orgaplums/teamsite/_api/$metadata#SP.ApiData.Lists/@Element&$select=Title\",\"odata.type\":\"SP.List\",\"odata.id\":\"http://orgaplums/teamsite/_api/Web/Lists(guid'fc03bd4c-7908-401e-9acf-2329470867f3')\",\"odata.etag\":\"\\\"3\\\"\",\"odata.editLink\":\"Web/Lists(guid'fc03bd4c-7908-401e-9acf-2329470867f3')\",\"Title\":\"MySurvey\"ListItem' could not be resolved by the model. When a model is available, each type name must resolve to a valid type."}}}
Here is my code used to fetch from API:
public async postVote(surveyListId: string, question: string, choice: string): Promise<boolean> {
let listName = await this.getListName(surveyListId)
alert("List Name : " + listName)
var restUrl: string = this.context.pageContext.web.absoluteUrl;
restUrl += "/_api/Web/Lists(guid'";
restUrl += surveyListId;
restUrl += "')/items";
let contenttype = await this.getItemTypeForListName(listName)
console.log("List Contenttype : " +contenttype)
var item = {
"__metadata": { "type": "SP.Data.MysurveyListItem" },
"Title": "newItemTitle"
};
item[question] = choice;
var options: ISPHttpClientOptions = {
headers: {
"odata-version": "3.0",
"Accept": "application/json"
},
body: JSON.stringify(item),
webUrl: this.context.pageContext.web.absoluteUrl
};
return this.context.spHttpClient.post(restUrl, SPHttpClient.configurations.v1, options).then((response: SPHttpClientResponse) => {
return response.json().then((responseFormated: any) => {
return true;
});
}) as Promise<boolean>;
}
private getListName(listId: string): Promise<string> {
var restUrl: string = this.context.pageContext.web.absoluteUrl;
restUrl += "/_api/Web/Lists(guid'";
restUrl += listId;
restUrl += "')?$select=Title";
var options: ISPHttpClientOptions = {
headers: {
"odata-version": "3.0",
"Accept": "application/json"
}
};
return this.context.spHttpClient.get(restUrl, SPHttpClient.configurations.v1, options).then((response: SPHttpClientResponse) => {
return response.text().then((responseFormated: string) => {
var iTitle = responseFormated.indexOf("<d:Title>");
var newStr = responseFormated.slice(iTitle + 9, responseFormated.length);
newStr = newStr.slice(0, newStr.indexOf("</d:Title>"));
return newStr;
});
});
}
// private getItemTypeForListName(name: string): string {
// return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";
// }
private getItemTypeForListName(listId: string) :Promise<string> { //string {
var restUrl: string = this.context.pageContext.web.absoluteUrl;
restUrl += "/_api/Web/Lists(guid'";
restUrl += listId;
restUrl += "')?$select=ListItemEntityTypeFullName";
var options: ISPHttpClientOptions = {
headers: {
"odata-version": "3.0",
"Accept": "application/json"
}
};
return this.context.spHttpClient.get(restUrl, SPHttpClient.configurations.v1, options).then((response: SPHttpClientResponse) => {
return response.json().then((responseFormated: any) => {
return responseFormated.ListItemEntityTypeFullName;
});
});
ListItemEntityTypeFullName
in payload. Try using likevar item = {"__metadata": { "type": contenttype },"Title": "newItemTitle"};
assuming you are getting correct value fromgetItemTypeForListName()
function. Let me know if this works for you.