skip to Main Content

I need to be able to find a driveItem from the Microsoft Graph API, given only a document URL in this sort of format:

https://tenant.sharepoint.com/sites/somesite/somefolder/somedocument.doc

I know it is possible to do the opposite, e.g. get a document URL from a driveItem, but how do I get the driveItem when I only have a document URL?

2

Answers


  1. For sample, I created folder in SharePoint site and uploaded document in the folder:

    enter image description here

    To fetch a drive Item ID given only a document URL via Microsoft Graph API, check the below:

    https://graph.microsoft.com/v1.0/sites/SiteID/drive/root/search(q='Document.docx')
    

    enter image description here

    To verify the same, I passed the Drive ID and Drive Item ID to check if it fetches the document:

    https://graph.microsoft.com/v1.0/drives/DriveID/items/DriveItemID
    

    enter image description here

    If you don’t have Site ID, you can pass the name of the site to fetch the site ID:

    https://graph.microsoft.com/v1.0/sites?search=YourSiteName
    

    ![enter image description here](https://i.imgur.com/Fic96sm.png)

    Login or Signup to reply.
  2. You can use Search API, filter driveItem entity type, and specify the document URL in the queryString

    POST https://graph.microsoft.com/v1.0/search/query
    
    {
        "requests": [
            {
                "entityTypes": [
                    "driveItem"
                ],
                "query": {
                    "queryString": "path:"https://tenant.sharepoint.com/sites/somesite/somefolder/somedocument.doc""
                }
            }
        ]
    }
    

    The response will contain resource property with inner id which is driveItemId you need. The parentReference property contains additional details like driveId

    Another way is to get file by relative path, but it requires the knowledge of siteId and driveId related to the certain document library.

    GET https://graph.microsoft.com/v1.0/sites/{siteId}/drives/{driveId}/root:/somefolder/somedocument.doc:/
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search