0

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;
    });
    });
1
  • You are using wrong ListItemEntityTypeFullName in payload. Try using like var item = {"__metadata": { "type": contenttype },"Title": "newItemTitle"}; assuming you are getting correct value from getItemTypeForListName() function. Let me know if this works for you. Commented Jun 8, 2022 at 13:19

1 Answer 1

0

I think you are using wrong ListItemEntityTypeFullName in payload.

If you are getting correct ListItemEntityTypeFullName value from getItemTypeForListName() function, try using payload like:

var item = {"__metadata": { "type": contenttype }, "Title": "newItemTitle"};

Check my answer here to understand more about getting correct ListItemEntityTypeFullName: Pass file name having space to update operation using REST API

1
  • Same issue, it didn't resolve the problem, I have used { "type": "SP.Data.MysurveyListItem" } gotten from calling api /_api/web/lists/GetByTitle('listName')/ListItemEntityTypeFullName end point and it was the same value Commented Jun 8, 2022 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.