Search code examples
restmicrosoft-graph-apihttprequestsharepoint-online

Set look up field value for sharepoint drive item using microsoft graph api rest


On Sharepoint online, I have a document library with files and a list with some metadata, which I want to link to the files. And since you can't use look up fields with document library items but only list items, I want to have a look up field at the file that links to the corresponding list item, like this:

(https://i.sstatic.net/FziXW.png)

Now obviously, it works by setting the field value manually, but using the graph api, I can't seem to find the right URL to make the http request to, as it's different from a normal list item. Of which I have managed to change the lookup field value.

The URL to get to the file looks basically like this:

https://microsoftgraph.chinacloudapi.cn/v1.0/sites/root/drives/<driveID>/items/<itemID>

(Yes, I am trying to post to MS cloud in China)

I have tried to send my patch request directly to this url with the request body (the lookup field is called "Metadata":

{
    "MetadataLookupId": "686"
}

but I get this response:

{
    "error": {
        "code": "invalidRequest",
        "message": "Invalid request",
        "innerError": {
            "date": "2024-01-08T11:01:00",
            "request-id": "xxx",
            "client-request-id": "xxx"
        }
    }
}

Unlike list items, drive items don't have a "fields" property. But I tried it with /fields behind the url above nonetheless, and got this response:

{
    "error": {
        "code": "itemNotFound",
        "message": "Item not found",
        "innerError": {
            "date": "2024-01-08T11:02:09",
            "request-id": "xxx",
            "client-request-id": "xxx"
        }
    }
}

Could someone tell me if what I am trying to do is even possible with api requests, and if yes, what url, which headers and what request body I would have to use?

This is also my first time posting, so please ask me if anything is unclear.


Solution

  • It should be quite simple. driveItem has relationship to associated listItem.

    Send PATCH request to this URL

    https://microsoftgraph.chinacloudapi.cn/v1.0/sites/root/drives/<driveID>/items/<itemID>/listItem/fields
    {
        "MetadataLookupId": "686"
    }