Skip to main content
Participant
January 14, 2014
Answered

Sharepoint 2013 PDF check out and open option

  • January 14, 2014
  • 12 replies
  • 65290 views

A few months back I made the change to my DOCICON.xml file to enable the integration with Acrobat and Sharepoint to prompt the user to "Check Out and Open" PDF files in document libraries in Sharepoint 2013. It worked fine and as expected.

After applying CU's to Sharepoint up to October 2013, this function seems to have gone away. I verified that the DOCICON.xml settings are correct.  Ex: <Mapping Key="pdf" Value="pdficon_small.png" OpenControl="AdobeAcrobat.OpenDocuments" />

I can still get the "check out..." prompt if I open the document libraries with Windows Explorer, but not when PDF's are opened directly on Sharepoint. I have a support case opened with Microsoft, but they seem to point the problem at Adobe.

I have verified that the AcroPDF.dll is on each client and is enabled in IE. The PDF files open with Acrobat, but the check out prompt is not showing up unless I open the Sharepoint library with Explorer.

Have tried different Acrobat versions (10 and 11) as well as different IE versions (9,10,11)..etc...and Windows 7 or Windows 8, but the check out function does not seem to work when opening PDF's directly from Sharepoint 2013

This topic has been closed for replies.
Correct answer EricHallgren

Microsoft support just helped me with this issue. A published fix is likely to come out soon. The following steps are just my re-write of their instructions. As with all fixes or workarounds, I recommend you test first.

Other things to be aware of before you implement this is that all PDF files will open with the "check-out" function whether or not your library settings say "open in browser" or not and I don't think any other settings will matter as the javascript file is controlling. Changes you make to the docicon.xml won't matter either.

STEPS

You need to create a javascript file, the contents are at the end of this post. Copy the contents between the “***” to notepad or other text editor and save it as “PDFFIX.js”.

Copy the file (pdffix.js) to your Sharepoint server - C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS

Run the powershell commands on your Sharepoint server below depending on what you want to fix, either a single library or site collection. You may also need to clear IE cache on your client.

Powershell commands assume admin and Sharepoint tools loaded or you are running from the management shell.

SINGLE DOC LIBRARY

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

SITE COLLECTION

$site = Get-SPSite http://”yoursite”-no quotes
$web = $site.RootWeb
$field = $web.Fields.GetFieldByInternalName("LinkFilename")
$field.JSLink = "/_layouts/15/PdfFix.js"
$field.Update($true) #push change down to all lists

To uninstall the fix use this:

$site = Get-SPSite http:// ”yoursite”-no quotes
$web = $site.RootWeb
$field = $web.Fields.GetFieldByInternalName("LinkFilename")
$field.JSLink = ""
$field.Update($true) #push change down to all lists

  

*****CONTENTS OF PDFFIX.js*****

(function () {

    if (typeof SPClientTemplates === 'undefined')

        return;

    var PdfCtx = {};

    PdfCtx.Templates = {};

    PdfCtx.Templates.Fields = { 'LinkFilename': { 'View': PdfClientLinkFilenameNoMenu } };

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(PdfCtx);

})();

function GetExtension(szHref) {

    var sz = new String(szHref);

    var re = /^.*\.([^\.]*)$/;

    return (sz.replace(re, "$1")).toLowerCase();

}

var stsOpen = null;

function IsPdfClientInstalled() {

    if (stsOpen == null) {

        if (Boolean(window.ActiveXObject)) {

            try {

                stsOpen = new ActiveXObject("PdfFile.OpenDocuments");

            }

            catch (e) {

                stsOpen = null;

            }

        }

    }

    return (stsOpen != null);   

}

function OpenPdfInClient(pdfFileUrl) {   

    var fRet = true;

    try {

        fRet = typeof stsOpen.ViewDocument2 != "undefined" && stsOpen.ViewDocument2(window, pdfFileUrl, '');

    }

    catch (e) {

        fRet = false;

    };

    if (event != null) {

        event.cancelBubble = true;

        event.returnValue = false;

    }

    return fRet;

}

function PdfNewGif(listItem, listSchema, ret) {

    if (listItem["Created_x0020_Date.ifnew"] == "1") {

        var spCommonSrc = GetThemedImageUrl("spcommon.png");

        ret.push("<span class=\"ms-newdocument-iconouter\"><img class=\"ms-newdocument-icon\" src=\"");

        ret.push(spCommonSrc);

        ret.push("\" alt=\"");

        ret.push(Strings.STS.L_SPClientNew);

        ret.push("\" title=\"");

        ret.push(Strings.STS.L_SPClientNew);

        ret.push("\" /></span>");

    }

}

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=\"");

            var appInstalled = IsPdfClientInstalled();

            var szExt = GetExtension(listItem.FileRef);

            if (appInstalled && szExt == 'pdf' && browseris.ie) {

                ret.push("return OpenPdfInClient('");

                ret.push("http://");

                ret.push(window.location.hostname);

                ret.push(listItem.FileRef);

            }

            else {

                ret.push("return DispEx(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>");

            PdfNewGif(listItem, listSchema, ret);

        }

    }

    else {

        ret.push("<nobr>");

        ret.push(listItem["FileLeafRef"]);

        ret.push("</nobr>");

    }

    return ret.join('');

}

************END CONTENTS OF PDFFIX.js***********************

12 replies

EricHallgrenAuthorCorrect answer
Participant
March 19, 2014

Microsoft support just helped me with this issue. A published fix is likely to come out soon. The following steps are just my re-write of their instructions. As with all fixes or workarounds, I recommend you test first.

Other things to be aware of before you implement this is that all PDF files will open with the "check-out" function whether or not your library settings say "open in browser" or not and I don't think any other settings will matter as the javascript file is controlling. Changes you make to the docicon.xml won't matter either.

STEPS

You need to create a javascript file, the contents are at the end of this post. Copy the contents between the “***” to notepad or other text editor and save it as “PDFFIX.js”.

Copy the file (pdffix.js) to your Sharepoint server - C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS

Run the powershell commands on your Sharepoint server below depending on what you want to fix, either a single library or site collection. You may also need to clear IE cache on your client.

Powershell commands assume admin and Sharepoint tools loaded or you are running from the management shell.

SINGLE DOC LIBRARY

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

SITE COLLECTION

$site = Get-SPSite http://”yoursite”-no quotes
$web = $site.RootWeb
$field = $web.Fields.GetFieldByInternalName("LinkFilename")
$field.JSLink = "/_layouts/15/PdfFix.js"
$field.Update($true) #push change down to all lists

To uninstall the fix use this:

$site = Get-SPSite http:// ”yoursite”-no quotes
$web = $site.RootWeb
$field = $web.Fields.GetFieldByInternalName("LinkFilename")
$field.JSLink = ""
$field.Update($true) #push change down to all lists

  

*****CONTENTS OF PDFFIX.js*****

(function () {

    if (typeof SPClientTemplates === 'undefined')

        return;

    var PdfCtx = {};

    PdfCtx.Templates = {};

    PdfCtx.Templates.Fields = { 'LinkFilename': { 'View': PdfClientLinkFilenameNoMenu } };

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(PdfCtx);

})();

function GetExtension(szHref) {

    var sz = new String(szHref);

    var re = /^.*\.([^\.]*)$/;

    return (sz.replace(re, "$1")).toLowerCase();

}

var stsOpen = null;

function IsPdfClientInstalled() {

    if (stsOpen == null) {

        if (Boolean(window.ActiveXObject)) {

            try {

                stsOpen = new ActiveXObject("PdfFile.OpenDocuments");

            }

            catch (e) {

                stsOpen = null;

            }

        }

    }

    return (stsOpen != null);   

}

function OpenPdfInClient(pdfFileUrl) {   

    var fRet = true;

    try {

        fRet = typeof stsOpen.ViewDocument2 != "undefined" && stsOpen.ViewDocument2(window, pdfFileUrl, '');

    }

    catch (e) {

        fRet = false;

    };

    if (event != null) {

        event.cancelBubble = true;

        event.returnValue = false;

    }

    return fRet;

}

function PdfNewGif(listItem, listSchema, ret) {

    if (listItem["Created_x0020_Date.ifnew"] == "1") {

        var spCommonSrc = GetThemedImageUrl("spcommon.png");

        ret.push("<span class=\"ms-newdocument-iconouter\"><img class=\"ms-newdocument-icon\" src=\"");

        ret.push(spCommonSrc);

        ret.push("\" alt=\"");

        ret.push(Strings.STS.L_SPClientNew);

        ret.push("\" title=\"");

        ret.push(Strings.STS.L_SPClientNew);

        ret.push("\" /></span>");

    }

}

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=\"");

            var appInstalled = IsPdfClientInstalled();

            var szExt = GetExtension(listItem.FileRef);

            if (appInstalled && szExt == 'pdf' && browseris.ie) {

                ret.push("return OpenPdfInClient('");

                ret.push("http://");

                ret.push(window.location.hostname);

                ret.push(listItem.FileRef);

            }

            else {

                ret.push("return DispEx(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>");

            PdfNewGif(listItem, listSchema, ret);

        }

    }

    else {

        ret.push("<nobr>");

        ret.push(listItem["FileLeafRef"]);

        ret.push("</nobr>");

    }

    return ret.join('');

}

************END CONTENTS OF PDFFIX.js***********************

January 8, 2015

@Not sure if anyone else ran into this issue, but it doesn't seem like anyone checked this code with a document library that contained folders.  When I did, it bombed out.  The fix was to completely remove the checking for a folder.  This seemed to fix it right up.  Not sure if someone forgot to code the RenderDocFolderLink and RenderListFolderLink functions or what exactly those are supposed to refer to but whatever they are, they don't work correctly and simply removing the entire checking for and individual processing of folders seems to work just fine.

Also, one other note, putting a link to the pdffix.js file in the Master Page seems to work just fine for sites that have Publishing feature active.

Participant
February 18, 2020

Thank you for posting this! It helped with my issue after migrating from SP2013 to SP2016. When the original fix was applied libraries with folders were getting a TypeError. I removed the sections you refrenced and ran the powershell script and everything worked great! Here's the code.

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

    var PdfCtx = {};
    PdfCtx.Templates = {};
    PdfCtx.Templates.Fields = { 'LinkFilename': { 'View': PdfClientLinkFilenameNoMenu } };
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(PdfCtx);
})();

function GetExtension(szHref) {
    var sz = new String(szHref);
    var re = /^.*\.([^\.]*)$/;
    return (sz.replace(re, "$1")).toLowerCase();
}

var stsOpen = null;

function IsPdfClientInstalled() {
    if (stsOpen == null) {
        if (Boolean(window.ActiveXObject)) {
            try {
                stsOpen = new ActiveXObject("PdfFile.OpenDocuments");

            }
            catch (e) {
                stsOpen = null;
            }
        }
    }
    return (stsOpen != null);    
}

function OpenPdfInClient(pdfFileUrl) {    
    var fRet = true;

    try {
        fRet = typeof stsOpen.ViewDocument2 != "undefined" && stsOpen.ViewDocument2(window, pdfFileUrl, '');
    }
    catch (e) {
        fRet = false;
    };

    if (event != null) {
        event.cancelBubble = true;
        event.returnValue = false;
    }
    return fRet;
}

function PdfNewGif(listItem, listSchema, ret) {
    if (listItem["Created_x0020_Date.ifnew"] == "1") {
        var spCommonsrc=GetThemedImageUrl("spcommon.png");

        ret.push("<span class=\"ms-newdocument-iconouter\"><img class=\"ms-newdocument-icon\" src=\"");
        ret.push(spCommonSrc);
        ret.push("\" alt=\"");
        ret.push(Strings.STS.L_SPClientNew);
        ret.push("\" title=\"");
        ret.push(Strings.STS.L_SPClientNew);
        ret.push("\" /></span>");
    }
}

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

    if (fileUrl != null && typeof fileUrl != 'undefined' && TrimSpaces(fileUrl) != "") {
      
            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=\"");

            var appInstalled = IsPdfClientInstalled();
            var szExt = GetExtension(listItem.FileRef);
            if (appInstalled && szExt == 'pdf' && browseris.ie) {
                ret.push("return OpenPdfInClient('");
				var url = window.location.href;
				var arr = url.split("/");
				var baseHostNameWithPort = arr[0] + "//" + arr[2];
                ret.push(baseHostNameWithPort);
                ret.push(listItem.FileRef);
            }
            else {
                ret.push("return DispEx(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>");

            PdfNewGif(listItem, listSchema, ret);
    }
    else {
        /*ret.push("<nobr>");
        ret.push(listItem["FileLeafRef"]);
        ret.push("</nobr>");*/
    }
    return ret.join('');
}

Hey egalyk2093, I've used your code since its the most recent and everything is working as far as getting PDF's to open with the checkout option. I am running into one issue though, hoping you might be able to help. When I go to Site Settings and click Themes, I get the following error:

 

ReferenceError: 'spCommonSrc' is undefined
ReferenceError: 'spCommonSrc' is undefinedTypeError: Unable to get property '_events' of undefined or null reference
 
Seems to be related to this function:
function PdfNewGif(listItem, listSchema, ret) {
    if (listItem["Created_x0020_Date.ifnew"] == "1") {
        var spCommonsrc=GetThemedImageUrl("spcommon.png");

        ret.push("<span class=\"ms-newdocument-iconouter\"><img class=\"ms-newdocument-icon\" src=\"");
        ret.push(spCommonSrc);
        ret.push("\" alt=\"");
        ret.push(Strings.STS.L_SPClientNew);
        ret.push("\" title=\"");
        ret.push(Strings.STS.L_SPClientNew);
        ret.push("\" /></span>");
    }
}
Any clue how to fix? Thanks.
Participant
January 23, 2014

We have the same problem.  We noticed it was no longer working after the March cummulative update for SP 2013.  Have you found a solution yet? 

Participant
January 23, 2014

We have a support case open with Microsoft and about a week ago, Microsoft confirmed a bug in the March 2013 updates. The java script is not executing a call to the active-x control on the client PC. We submitted the paperwork to get a hotfix created, don't know how this works with them, sounds like they have a committee to review requests like this, so I am hopeful we will get a fix soon, but no time frame given.

Another workaround is to search the document library and open the PDF's via the search results (assuming youi have searching enabled), then you should also get the "open" prompt.

Participant
January 23, 2014

Thank you for your response.  Do you mind keeping us posted on any further details as you hear them?