4

I'm looking to automatically add '?web=1' to the end of a URL for a document link in the multiple text field of a list. The user can insert the document from a doc library but I want the doc to open in the browser (file remains in SharePoint) and not download it to desktop.

It's part of a request process. Users have to add content type docs to the request list so the multiple lines of text field is perfect but not if they have to download the file when clicked.

As far as I can tell this is not related to the settings for Office Web Apps that are set in the site collection features (open in client apps is off). It's just the normal behaviour of the link within that column field. Adding '?web=1' works fine but users can't be modifying the URL each time they link to a document.

I'm using SharePoint Online. I figure JS Link might the answer but don't have enough knowledge of that as a OOTB Dev.

Thanks.

1 Answer 1

2

JS Link will work fine if all you want to do is modify the link in views.

The following code should give you a good starting place

(function() {
    // Create an object to hold the context information for the document link field
    var documentLinkContext = {};
    documentLinkContext.Templates = {};
    documentLinkContext.Templates.Fields = {
        // Define override function
        "DocumentLink": { "View": appendWebToDocLink }
    };

    //Register override function
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(documentLinkContext);
})();

function appendWebToDocLink(ctx) {
    // Get value from list
    var link = ctx.CurrentItem[ctx.CurrentFieldSchema.Name];
    // Change link and return value
    return link.replace('">', '?web=1">');  
}

If you are not sure how to add JS Link theres a good guide here

2
  • Thanks @codecraftertroy. Certainly a starting point as I can see where you are going with it. Not sure of the last line and what is being replaced. Thanks. Nov 16, 2016 at 11:52
  • The last line is replacing the end of the opening <a href="URL"> tag so it becomes <a href="URL?web=1> Nov 16, 2016 at 17:29

Your Answer

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

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