0

Using the Sharepoint Online REST API I have successfully created a new file using a POST request - https://company.sharepoint.com/_api/lists/getbytitle('MyTitle')/Files/add(name='test.jpg',overwrite=true)

Following the documentation I'm now trying to update the file metadata -

If you want to update a file's metadata, you have to construct an endpoint that reaches the file as a list item.

Construct an endpoint that looks like this: https://siteurl/_api/web/lists/getbytitle('Documents')/items(item id)

When creating the file the response I get back from the POST request is something like the following -

{
    "d": {
        "__metadata": {
            "id": "https://company.sharepoint.com/_api/Web/Lists(guid'288441230-b910-4665-ae1d-a359b06c9af8')/files('2_QQs7Rb_UOV1fUNAJw1bJxTMrEZ5qlEmmNfyYNoND5ftgyx2P4lQ4dUdToJAvCF')",
            "uri": "https://company.sharepoint.com/_api/Web/Lists(guid'288441230-b910-4665-ae1d-a359b06c9af8')/files('2_QQs7Rb_UOV1fUNAJw1bJxTMrEZ5qlEmmNfyYNoND5ftgyx2P4lQ4dUdToJAvCF')",
            "type": "MS.FileServices.File"
        }
    },
    ...
}

My question is how do you retrieve the item id as obviously the urls here are different to the url required by update metadata method.

4
  • Check the answer to this question: stackoverflow.com/questions/18662332/… - try adding ?$expand=ListItemAllFields to the request url when uploading the file. Mar 4, 2019 at 14:37
  • 1
    Thanks Damjan. I did see that post earlier and tried adding the $expand=ListItemAllFields parameter to the post request but it doesn't seem to make any difference to the number of fields returned. Mar 4, 2019 at 14:49
  • Is that all you get from SP Online? I'm using SP2013 on prem and get quite a bit more back in the "d" object. But I do also have to make another call to get the ID and from there I can then make a third call to set metadata.
    – Rothrock
    Mar 4, 2019 at 16:02
  • To be fair there is a bit more data in the "d" object, I just didn't include it all. Nothing that references the id. However, that's reassuring if you also have to perform 3 separate requests to set the metadata. Mar 4, 2019 at 16:21

1 Answer 1

5

This will return the ID:

/siteURL/_api/web/lists/getbytitle('Documents')/Items?$filter=FileLeafRef eq 'AdventureWorksBikesWithDetails.xlsx'&$select=Id

This will return:

{
    "d": {
        "results": [
            {
                "__metadata": {
                    "id": "2ccf8662-1f02-4c57-b505-120e425660da",
                    "uri": "https://siteURL/_api/Web/Lists(guid'c8612335-fb7e-4edf-bf1d-b9186a8b987b')/Items(15)",
                    "etag": "\"2\"",
                    "type": "SP.Data.Shared_x0020_DocumentsItem"
                },
                "Id": 15,
                "ID": 15
            }
        ]
    }
}
2
  • Thanks Mike, that works a treat. So to clarify there is no way to retrieve this during the initial post? It seems like creating a file and setting metadata is something that should be easier than making two separate calls. Mar 4, 2019 at 15:15
  • 1
    Thanks for that. @ChrisEdgington, you don't need to do an extra call, just add ?$expand=ListItemAllFields&$select=Id after the POST URL and the id will be included in the response. Apr 18 at 14:44

Your Answer

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

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