sp-dev-docs icon indicating copy to clipboard operation
sp-dev-docs copied to clipboard

Update list item REST API not working as expected

Open MathieuMarchant opened this issue 7 years ago • 5 comments

Category

  • [ ] Question
  • [ ] Typo
  • [x] Bug
  • [ ] Additional article idea

Expected or Desired Behavior

The Update list item REST API should work as described in the documentation or the documentation should be updated.

https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-lists-and-list-items-with-rest#update-list-item

Observed Behavior

When using the Update list item REST API, it is not possible to make it work as described in the documentation. The following error is thrown

The property __metadata does not exist on type...

Suggested update

In the documentation it is explicitly mentioned to add the metadata field in the body. body: { '__metadata': { 'type': 'SP.Data.TestListItem' }, 'Title': 'TestUpdated'}

However this triggers an error. After removing the metadata from the body I was able to make the code work. So please update the documentation accordingly.

MathieuMarchant avatar Apr 30 '19 08:04 MathieuMarchant

Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.

msft-github-bot avatar Apr 30 '19 08:04 msft-github-bot

@EagleWizard28 @lucabandMSFT I don't think this is a bug. The note on the page clearly mentions getting ListItemEntityTypeFullName

To do this operation, you must know the ListItemEntityTypeFullName property of the list and pass that as the value of type in the HTTP request body.

Here's how you can get that: http://site url/_api/web/lists/GetByTitle('Test')?$select=ListItemEntityTypeFullName

unowiz avatar May 16 '19 12:05 unowiz

@unowiz it seems it doesn't matter which value I pass, I get the following error:

The property __metadata does not exist on type

So yes I was able to fetch the "ListItemEntityTypeFullName", but I'm unable to pass it. When I just drop the "_metadata" it works. Here some example code that works for me, which updates the list item. Take into account that the list has a custom content type.

    public async setItemComplete(itemId: number) {
        const itemUrl = `${this._normalizedWebUrl}/_api/web/lists/getByTitle('${this._ListTitle}')/items(${itemId})`;
        const client = new SPHttpClient();

        const updateData = {
            ResolutionDate: new Date().toISOString()
        };

        const httpClientOptions: FetchOptions = {
            headers: new Headers({
                 Accept: "application/json",
                "Content-Type": "application/json",
                "X-HTTP-Method": "MERGE",
                "IF-MATCH": "*",
            }),
            body: JSON.stringify(updateData)
        };

        return client.post(itemUrl, httpClientOptions);
    }

Am I overlooking something?

MathieuMarchant avatar May 20 '19 14:05 MathieuMarchant

Hi I am facing same issue. I am using Fetch api post request to create an Item, if I remove '__metadata' property then only item gets created otherwise it throws error. Is there anyone still facing this issue?

sureshchandanam avatar Jul 13 '19 16:07 sureshchandanam

Issues that have been closed & had no follow-up activity for at least 7 days are automatically locked. Please refer to our wiki for more details, including how to remediate this action if you feel this was done prematurely or in error: Issue List: Our approach to locked issues

msft-github-bot avatar Jan 24 '20 12:01 msft-github-bot