microsoft-graph-apimicrosoft-graph-sdksmicrosoft-graph-files

Retrieve Document through msgraph using Document ID


I have enabled the Document ID feature in my Office 365 Sharepoint collections. I would like to store the Document ID on our php server and then be able to allow the server to download the document using the document ID.

I have successfully set up both the microsoft-sdk-php (using the app only tenant permissions) and the phpSPO libraries (using the microsoft graph api interface). In both cases, I cannot figure out how to use the document ID to retrieve the actual name and path of a document. If I use the standard name/path of the file, I can successfully download it to the server.

With the microsoft-sdk-php library, I have tried searching the drive items, but cannot determine how to search by the document ID. The documentation on the Microsoft Graph API on using DriveItems/Search (using the filter q="...") seems very incomplete. I've tried multiple variations using in attempting to filter using DocId, _dlc_DocId, and other variations (again, the exact name of this field is not clear from the documentation).

I have also tried using curl to capture the redirect of the Document ID URL (xxxxxx.sharepoint.com/_layouts/15/DocIdRedir.aspx?ID=XXXXXXX-2222222222-86), but I either get forbidden errors or it fails to properly redirect.

I've also looked into using a query to search a sharepoint drive, but I haven't had success - in part because I cannot determine where and how the 'Document Id' field is stored.


Solution

  • I think I can answer this one for you. I was struggling with this myself so for anyone else in the same boat. I was working in .NET but the same should apply:

    The document id (and other 'properties' data) that you're looking for is not actually part of one drive and so you can't find it via the normal DriveItem endpoints. This data is actually sharepoint metadata stored elsewhere in sharepoint lists. In this case, the data you want is stored in a system list on sharepoint. The steps I used are:

    To do this you will need your sharepoint site id and the id or name of the library. To get the sharepoint list, use this API call:

    GET https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}
    or
    GET https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-title}
    

    Now you have your list, you can retrieve the list items. Note, you will need to expand fields :

    GET https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/items?expand=fields
    

    The above call should return the metadata you need in the 'fields' property. The document id field specifically is stored in the field called _dlc_DocIdUrl.

    I hope this helps you or someone else in the future!