Search code examples
microsoft-graph-apionedriveoffice365api

Accessing OneDrive files in API, how do I link them to open in Office Online?


I am using the OneDrive API as file storage in a CRM and I have been asked if clicking on a file in the CRM can open it directly in Office Online to be edited, rather than people having to download the file, edit on their desktop, the re-upload.

I can't find anything in the documentation either way so maybe my Google-fu is low at the moment


Solution

  • The DriveItem property you're looking for is called webUrl.For Office documents (Word, Excel, PowerPoint, etc.), webUrl contains the complete link to opening the document in Office Online:

    {
        "id": "{driveItem-id}",
        "name": "{file-name}",
        "file": {
            "hashes": {
                "quickXorHash": "YY1FIiSDCS9hcAptSPs7prNdf5A="
            },
            "mimeType": "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        },
        "size": 22750,
        "webUrl": "https://{domain}/{path}/_layouts/WopiFrame.aspx?sourcedoc{id}&file={file-name}&action=default",
    }
    

    If the user also has a native Office client installed (Office for Windows, Mac, iPad, etc.), they will also be presented with the option to open the document in the native app (which will also automatically handle saving changes directly back to OneDrive).

    You can try this for yourself using this sample in Graph Explorer.