I'm presenting some Search results to the user using REST API, is there something specific should I include in the link to the document so the document opens in the browser?

  • I thought that depended on the settings of the underlying library where the document is stored, if the behavior is to open in the browser it will. Not certain about this and haven't tested. – Eric Alexander Jul 28 '15 at 20:36
  • Let's say that the link is in the home page.... I checked the library options and the option for open in the browser is activated – yngrdyn Jul 28 '15 at 20:36
  • what I mean is, if document X that is in library Y and presented on page Z, if user clicks the link, it should take the file handling options from library Y. That is my understanding in theory, I have not tried this in practice. – Eric Alexander Jul 28 '15 at 20:40
  • I said, I Checked the library and the options are fine... But still downloading the document with the link – yngrdyn Jul 28 '15 at 20:41

The simplest solution is to append ?web=1 at the and of file url

Ex: https://server/site/lib/filename.ext?web=1

  • You can open the document in edit mode bydefault by appending another query string parameter action=edit. So you have to append “?web=1&action=edit”to open in edit mode. – Shekar Reddy Aug 27 '18 at 18:05

You can obtain the file URL and concatenate with a constant as in the following code:

    function documentWebUrl(ctx) {
        var fileUrl = ctx.CurrentItem.FileLeafRef.FileUrl;
        ExecuteOrDelayUntilScriptLoaded(function () {
            var clientContext = new SP.ClientContext();
            var web = clientContext.get_web();
            clientContext.executeQueryAsync(function (s, a) {
                var webUrl = web.get_url();
                var DOCURL = webUrl + "/_layouts/15/WopiFrame.aspx?sourcedoc=" + encodeURIComponent(fileUrl) + "&action=default";
                // here put some code to navigate or whatever you need to DOCURL ....
        }, "SP.js");
  • In general it's a sample on client side script to obtain the url in the following form <Site URL>/_layouts/15/WopiFrame.aspx?sourcedoc=<Doc URL>&action=default. – nilsandrey Jul 28 '15 at 21:43
  • 1
    better use the static object _spPageContextInfo.webAbsoluteUrl to obtain the weburl instead of using an request – Mx. Jul 29 '15 at 8:50
  • Yes, I prefer to use _spPageContextInfo.webAbsoluteUrl – yngrdyn Jul 29 '15 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.