Get OneDrive Item Id for Current Word doc
Asked Answered
I

2

3

I'm looking into building an add-in for Microsoft Word that would involve creating a copy of the current document.

I've found where I can copy an item through the OneDrive API. However, this requires the item-id for the document to be copied.

I can't seem to find how to get the item-id for the current Word document through the Office JavaScript API.

I've looked on the Office.context.document object, as well as the Office.context.document.getFileAsync() and Office.context.document.getFilePropertiesAysnc() functions.

How would someone get the OneDrive Item Id for the current Word doc?

Ilex answered 21/4, 2017 at 0:23 Comment(0)
P
4

There isn't a way to do this using just Office.js, you'll need to also leverage Microsoft Graph in order to find the actual ID.

Once you have a saved file, you can get the URI using getFilePropertiesdAsync. You'll need to parse the document name and path from the URI. You can then use query filters on the Drive's Children method to isolate the specific file you're looking for.

For example, if you had a URI of https://d.docs.live.net/<id>/Documents/somedoc.docx you can determine the following:

  • This is consumer OneDrive (note the live.net URI)
  • The document name is somedoc.docx
  • It is stored in the directory Documents

To find this document you would execute the following Graph command:

https://graph.microsoft.com/v1.0/me/drive/root/children/Documents?$filter=name eq 'somedoc.docx'

All of this can be executed directly from within your add-in. Please keep in mind, users can save documents to both OneDrive and OneDrive for Business. The user must authenticate against graph using the same credentials for the drive they're saving against.

Propylaeum answered 21/4, 2017 at 12:41 Comment(9)
Thanks! That was a very helpful answer. Although, in playing with the Graph Explorer, I'm seeing that the Graph endpoint should include children after the folder, to list the documents. So, it would look like this: graph.microsoft.com/v1.0/me/drive/root/children/Documents/… eq 'somedoc.docx' Is this the behavior I should expect?Ilex
Correct. Technically this will return an array back but we're relying on the requirement that names are unique per folder, so that array should only have 1 entry in it.Propylaeum
I've also discovered this method using the relative path following a colon. graph.microsoft.com/v1.0/me/drive/root:/somedoc.docx . Are there differences or drawbacks to using this method instead of the $filter?Ilex
I can't think of any off the top of my head. As long as you know the directory, this should return the correct file each time.Propylaeum
Sorry, one last follow up question on this. What about local documents? Is there a way to copy those? I'm guessing we don't have access from an add-in to write to the local file system. But, would it be possible to take a local Word doc and save a copy to OneDrive?Ilex
Not really, no. You could upload the document using traditional web upload methods but the user experience would be pretty confusing and rife with errors (i.e. they made further edits that haven't saved so they're uploading an old rev). The best you can do is check the location and present a warning within your add-in if it isn't saved to the expected location.Propylaeum
How to do for group drive document question hereOthello
$filter must be without $, and spaces must be url encoded using encodeURI for example. Otherwise it responses 500 to me.Jarboe
A big problem here is; if the file is under e.g. 5 folders, that means you have to do this filter query 5 time to get to the actual file. Because you need folder IDs to build the path to search file within.Jarboe
J
1

The solution in accepted answer is not the best way to do this, maybe not anymore because microsoft changed the api since then.

It works well for root files; however, for files inside nested folders you have to execute same query multiple times until you have all the folder ids. Because you need those ids to build the full path that contains the file to search for.

Here is a simpler way to do this as of now. Suppose that get Office.context.document.getFilePropertiesAysnc() gives you https://d.docs.live.net/<id>/MyFolder1/MyFolder2/somedoc.docx.

The graph endpoint should be: https://graph.microsoft.com/v1.0/me/drive/root:/MyFolder1/MyFolder2/Document.docx

Jarboe answered 17/8, 2018 at 9:6 Comment(1)
I was giving the wrong path included Documents to the path, and getting a 404 error. it is working after removing the Docments from path.Danseuse

© 2022 - 2024 — McMap. All rights reserved.