sharepointmicrosoft-graph-api

obtain item-id for microsoft graph api


I am trying to figure out how to obtain an "item-id" for a file in a sharepoint site for microsoft graph API.

Microsoft documentation: https://learn.microsoft.com/en-us/graph/api/driveitem-get?view=graph-rest-1.0&tabs=http

Here is what I tried for far:

Tried using https://developer.microsoft.com/en-us/graph/graph-explorer to guess the api link that will let me download the file I want.

I first tried to get the sharepointsite like so:

https://graph.microsoft.com/v1.0/sites/omidtechnfr.sharepoint.com:/sites/delploy

That worked:

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites/$entity",
    "createdDateTime": "2021-07-15T21:02:02.78Z",
    "description": "delploy",
    "id": "omidtechnfr.sharepoint.com,5e03397e-03b0-4d92-9658-f9163f0131b3,1d9f21da-4d74-45db-b5d4-e4a7f7f278a1",
    "lastModifiedDateTime": "2021-07-17T02:16:39Z",
    "name": "delploy",
    "webUrl": "https://omidtechnfr.sharepoint.com/sites/delploy",
    "displayName": "delploy",
    "root": {},
    "siteCollection": {
        "hostname": "omidtechnfr.sharepoint.com"
    }
}

So then I tried using what I ASSUME is the item-id for the SharePoint site delploy to try and get more information out of the site so I can try to find the item-id for the file I want to download:

https://graph.microsoft.com/v1.0/sites/5e03397e-03b0-4d92-9658-f9163f0131b3/drive/

And I got back some result but it doesn't so the item-id for me to do anything with it:

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives/$entity",
    "createdDateTime": "2021-07-11T05:16:50Z",
    "description": "",
    "id": "b!fjkDXrADkk2WWPkWPwExs9ohnx10TdtFtdTkp_fyeKF_K1_Z49FFSbzXVFRA5GKa",
    "lastModifiedDateTime": "2021-07-16T18:32:35Z",
    "name": "Documents",
    "webUrl": "https://omidtechnfr.sharepoint.com/sites/delploy/Shared%20Documents",
    "driveType": "documentLibrary",
    "createdBy": {
        "user": {
            "displayName": "System Account"
        }
    }

At this point I started to get desperate and a little angry. So I tried to just download the file from sharepoint and assume that the characters at the end of the download link was the item-id but that didn't work either: download link:

https://omidtechnfr.sharepoint.com/sites/delploy/_layouts/15/download.aspx?UniqueId=25e2fd4f%2Deb32%2D43ac%2D9141%2Dfa47a610a27c

Used https://www.urldecoder.org/ to decode what I am assuming is the item-id for the file I want to download

25e2fd4f-eb32-43ac-9141-fa47a610a27c

Then tried to desperately tack this on to the Microsoft graph api to see what happens:

https://graph.microsoft.com/v1.0/sites/5e03397e-03b0-4d92-9658-f9163f0131b3/drive/items/25e2fd4f-eb32-43ac-9141-fa47a610a27c

and it failed:

{
    "error": {
        "code": "itemNotFound",
        "message": "The resource could not be found.",
        "innerError": {
            "date": "2021-07-17T14:13:51",
            "request-id": "985e2893-5343-47ac-a93c-6629a00c4eb3",
            "client-request-id": "73a3b6d0-f1ce-bb51-b126-43f0957d15e3"
        }
    }
}

Solution

  • You could get the file by the path: https://graph.microsoft.com/v1.0/sites/{site-id}/drive/root:/{item-path}

    For example: https://graph.microsoft.com/v1.0/sites/{site-id}/drive/root:/test.docx

    If the file is in a folder, it would be like this: https://graph.microsoft.com/v1.0/sites/{site-id}/drive/root:/folder1/test.docx