Search code examples
restoffice365microsoft-graph-apionedrive

How to get a working document preview link from MS Graph


I am trying to get a preview link for the useres recent documents over MS Graph. Unfortunately the link the endpoint returns does not work.

To get the informations about a file I first call GET https://graph.microsoft.com/beta/me/drive/recent. Then I copy the driveID and the id of a document from the parentReference property.

To get the preview link I use the Endpoint POST https://graph.microsoft.com/beta/drives/<DriveID>/items/<DocumentID>/preview and this works fine. But when I click on the link I get the error "This item might not exist or is no longer avaiable". The document exists on the SharePoint, otherwise it would not appear under "recent documents". The url looks like this: https://www.onedrive.com/embed?webUrl=xyz.sharepoint.com/sites/nameOfTheSite/docLibName&id=sites/nameOfTheSite/DocLibName&embed=xxx&authToken=xxx

I expect the response of the /preview endpoint should return a working url. Do I have to make some configurations on O365?

Many Thanks


Solution

  • The embed link (preview) does not look valid in your example since id parameter refers to container (library): sites/nameOfTheSite/DocLibName. It is not supported, instead embed link should refer to a file

    Most likely the issue is due to itemId:

    https://graph.microsoft.com/beta/drives/<DriveID>/items/<DocumentID>/preview
                                                            ^^^^^^^^^^^^   
    

    in your example it seems refers to a library instead of a file. Make sure the proper itemId is specified.

    For example, https://graph.microsoft.com/v1.0/me/drive/recent endpoint returns the following payload:

    {
       "value" : {
           //another properties are omitted for a clarity
           //... 
           "remoteItem": {
               "id": "01ECKZLCWSR7F76B64KZFL7I3QGZVPJELU"
               //...   
                "parentReference": {
                    "driveId": "b!79yKq-2MdkSDnQ7_1Pf3FOkRyDCajpRIvqtA7UrsEO-vu3D_qkpaT50Y6CMcSmFv",
                    "driveType": "documentLibrary",
                    "id": "01ECKZLCV6Y2GOVW7725BZO354PWSELRRZ"
                },
           }
       }
    }
    

    where

    • remoteItem.Id - corresponds to item id for a file
    • remoteItem.parentReference.driveId - corresponds to drive id

    Dont get confused with remoteItem.parentReference.Id which corresponds to item id of library