SharePoint - Client-side Rendering Template to Force PDF Files to Open in PDF Client

Updated on 2/26/2015

Here are the steps to implement a client side rendering template that will allow PDF file links to open in the PDF client. Note that the C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\XML\docicon.xml file will need the following PDF entry:

<Mapping Key="pdf" Value="icpdf.png" OpenControl="PdfFile.OpenDocuments"/>

And the library will need to be set to “Open in client” under advanced settings.

1) Place the following file (pdffix.js) in C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS File contents are listed below.

Basically, this will override the rendering for the Link column from the out of the box template defined in clienttemplates.js (the default template is defined in the LinkFileName function). The only change is a call to DispEx is changed to DispDocItemExWithServerRedirect.

2) Run the following PowerShell to get the JS file to load

$web = Get-SPWeb http://sharepoint:81
$list = $web.Lists["Documents"]
$field = $list.Fields.GetFieldByInternalName("LinkFilename")
$field.JSLink = "/_layouts/15/PdfFix.js"
$field.Update($true)

This will implement the workaround for this single library, to implement the workaround for every library in a site collection, change the 3rd line to:

$field = $web.Fields.GetFieldByInternalName("LinkFilename")

PdfFix.js Contents

(function () {
    if (typeof SPClientTemplates === 'undefined')
        return;

    var PdfCtx = {};

    PdfCtx.Templates = {};

    PdfCtx.Templates.Fields = {

'LinkFilename': {
            'View': PdfClientLinkFilenameNoMenu
        }

    };

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(PdfCtx);
})();

function PdfClientLinkFilenameNoMenu(param1, param2, listItem, listSchema) {
            var ret = [];
            var fileUrl = listItem.FileRef;

            if (fileUrl != null && typeof fileUrl != 'undefined' && TrimSpaces(fileUrl) != "") {
                if (listItem.FSObjType == '1') {
                    if (listSchema.IsDocLib == '1') {
                        RenderDocFolderLink(ret, listItem.FileLeafRef, listItem, listSchema);
                    }
                    else {
                        RenderListFolderLink(ret, listItem.FileLeafRef, listItem, listSchema);
                    }
                }
                else {
                    ret.push("<a class='ms-listlink' href=\"");
                    ret.push(listItem.FileRef);
                    ret.push("\" onmousedown=\"return VerifyHref(this,event,'");
                    ret.push(listSchema.DefaultItemOpen);
                    ret.push("','");
                    ret.push(listItem["HTML_x0020_File_x0020_Type.File_x0020_Type.mapcon"]);
                    ret.push("','");
                    ret.push(listItem["serverurl.progid"]);
                    ret.push("')\" onclick=\"");
                    ret.push("return DispDocItemExWithServerRedirect(this,event,'TRUE','FALSE','");
                    ret.push(listItem["File_x0020_Type.url"]);
                    ret.push("','");
                    ret.push(listItem["File_x0020_Type.progid"]);
                    ret.push("','");
                    ret.push(listSchema.DefaultItemOpen);
                    ret.push("','");
                    ret.push(listItem["HTML_x0020_File_x0020_Type.File_x0020_Type.mapcon"]);
                    ret.push("','");
                    ret.push(listItem["HTML_x0020_File_x0020_Type"]);
                    ret.push("','");
                    ret.push(listItem["serverurl.progid"]);
                    ret.push("','");
                    ret.push(Boolean(listItem["CheckoutUser"]) ? listItem["CheckoutUser"][0].id : '');
                    ret.push("','");
                    ret.push(listSchema.Userid);
                    ret.push("','");
                    ret.push(listSchema.ForceCheckout);
                    ret.push("','");
                    ret.push(listItem.IsCheckedoutToLocal);
                    ret.push("','");
                    ret.push(listItem.PermMask);
                    ret.push("')\">");
                    var fileRef = listItem["FileLeafRef"];

                    if (fileRef != null) {
                        var index = fileRef.lastIndexOf('.');

                        fileRef = index >= 0 ? fileRef.substring(0, index) : fileRef;
                    }
                    ret.push(fileRef);
                    ret.push("</a>");
                    //NewGif(listItem, listSchema, ret);
                }
            }
            else {
                ret.push("<nobr>");
                ret.push(listItem["FileLeafRef"]);
                ret.push("</nobr>");
            }
            return ret.join('');
        }

Files

PDFFix.js.rename (03.38 kB)