0

I have created a custom page

It makes an ajax call and then results are shown in the page

$.ajax({
        url: _spPageContextInfo.webAbsoluteUrl+"/_api/web/lists/getbytitle('Document Library Name')/items?$top=1000&$select=Title,FieldValuesAsText/FileRef,FieldValuesAsText/FileRef/Title&$expand=FieldValuesAsText",
        headers: { "Accept": "application/json; odata=verbose" },
        cache: false,
        success: function (data) {
            //some code
        },
        error: function (xhr) {
            console.log(xhr);
        }
    });

Using the results, I create an anchor tag to open files

"<a target='_blank' href='{0}' class=''><i class='fa fa-eye'></i> View Document</a>".format(row.FieldValuesAsText.FileRef);

Here the problem is, when I have a xlsx file, it gets downloaded instead of opening in browser using office webapps

Can we add a query string to the url so that it opens up the file instead of downloading it?

1 Answer 1

1

You can append ?web=1 to the URL of the file and then it will open it in the browser.

Modify your code somewhat as:

"<a target='_blank' href='{0}' class=''><i class='fa fa-eye'></i> 
View Document</a>".format(row.FieldValuesAsText.FileRef + "?web=1");

In case of Office documents, you can also use the LinkingUrl property as below, wont work for PDF documents but for that you can use null handling as below :

use the endpoint as:

/_api/web/lists/getbytitle('Document Library Name')/items/items?
$top=1000&$select=Title,FieldValuesAsText/FileRef,FieldValuesAsText/FileRef/Title,
File/LinkingUrl&$expand=FieldValuesAsText,File

After that in your code use it somewhat as:

var fileUrl = "";
if(row.LinkingUrl.length > 0){
    fileUrl = row.LinkingUrl;
}else{
    fileUrl = row.FieldValuesAsText.FileRef + "?web=1";
}

"<a target='_blank' href='{0}' class=''><i class='fa fa-eye'></i>
 View Document</a>".format(fileUrl);

Reference - Making a link to a document in sharepoint open in browser

Link to “open in browser” with Office Web Apps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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