How to programmatically open a file located in OneDrive using desktop Word?

为君一笑 提交于 2019-12-25 07:48:03

问题


I have a .docx file in OneDrive. If I click on it, it opens on Word Online. Then, there is a button that says Edit in Word that opens the current file in desktop Word (you have to provide credentials, of course).

Using the OneDrive API, I'm able to open a file directly in Word Online from my PHP application (since it provides the file URL), but now I'm trying to directly open it in Word Offline (and then, Word will prompt the credentials to the user).

TL;DR: I'm trying to reproduce the Edit in Word button behavior. How can I do this?


回答1:


To open a file in desktop Word from webapp you have to use ms-word protocol:

https://msdn.microsoft.com/en-us/library/office/dn906146.aspx

It looks like: ms-word:ofe|u|<url to file>. You should be able to trigger opening file by setting above url as a href attribute of a tag or by calling window.open() JS function.

But you need direct URL(not the web url or generated access link) and as far as I'm concerned API doesn't provide such one so you will have to construct it by yourself.

Direct link for OneDrive(personal) looks like: https://d.docs.live.net/{drive-id}/{file-path}.

You can get your drive id by calling an API(I will use Microsoft Graph because I'm familiar only with this one).

Call https://graph.microsoft.com/v1.0/me/drive/ and as a response you will get a JSON which includes id property and that's your needed drive id.

If you don't want to use Graph API but OneDrive API I guess the only part that changes is graph.microsoft.com and it becomes api.onedrive.com(but I'm not sure, look here for more info - https://dev.onedrive.com/getting-started.htm).

Next you need to provide path to file in your OneDrive. If file is located in root directory the direct url to file might look like this: https://d.docs.live.net/xxxxxxxxxx/Document1.docx.

That's basically all, now you should be able to open your file in desktop Word by using ms-word:ofe|u|https://d.docs.live.net/xxxxxxxxxx/Document1.docx.



来源:https://stackoverflow.com/questions/41284652/how-to-programmatically-open-a-file-located-in-onedrive-using-desktop-word

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!