How to construct/get Office Web App URL for sharep

2019-06-08 02:00发布

问题:

I am trying to get the right redirection URL for my sharepoint documents which then I can use to open documents in WebView of iOS. Currently I am giving the absolute URL for the document where the doc is rendered inside WebView as PDF(Image/Readonly). Whereas I want to redirect to office webapp. Now my issue is I dont know if the URL for office web app is something which I can construct like appending /_layouts/15/WopiFrame.aspx?sourcedoc= or is the URL custom based on installations and we need to call some Sharepoint API which will let us know what is the base URL for Wopi service.

Currently I am passing URL like - https://.sharepoint.com/Shared%20Documents/demo/demo.docx Whereas I want to pass URL like - https://.sharepoint.com/_layouts/15/WopiFrame.aspx?sourcedoc=/Shared%20Documents/demo/demo.docx

Looking forward for help. Thanks in advance, Vishwesh

回答1:

File f = clientContext.Web.GetFileByServerRelativeUrl("/sites/  /Shared%20Documents/Title.docx");
clientContext.Load(f);
clientContext.ExecuteQuery();

ClientResult<String> result = f.ListItemAllFields.GetWOPIFrameUrl(SPWOPIFrameAction.Edit);

clientContext.Load(f.ListItemAllFields);
clientContext.ExecuteQuery();

result.Value contains a URL, something like this: http://sharep.xxx:8080/sites/zxxx/_layouts/15/WopiFrame.aspx?sourcedoc=%2Fsites%2Fzxxx%2FShared%20Documents%2FTitle%2Edocx&action=edit

Also you can extract the extract Office Web Apps URL from the above page, if you don't want to hit the sharepoint at all.



回答2:

using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Utilities;

// Assume we have these variables:
// ctx: A valid client context
// serverRelativeUrl: the URL of the document

File f = ctx.Web.GetFileByServerRelativeUrl (serverRelativeUrl);
result = f.ListItemAllFields.GetWOPIFrameUrl(SPWOPIFrameAction.Edit);

ctx.Load(f.ListItemAllFields);
ctx.ExecuteQuery();

This builds on the answer from @thebitlic which was the silver bullet for sure! However he or she is doing two calls to the server. Through the wonders of CSOM batching, it's possible to do it in one round trip, and no need to bring back the File object at all.