We have a SharePoint 2013 site with a list containing various documents, spreadsheets, or other files. From other parts of the application, we link to these attachments and expect them to open directly in the appropriate application by using some javascript and the SharePoint js function editDocumentWithProgID2
.
Example:
var docUrl = $.getUrlVar('docURL');
var fileExtension = docUrl.substr(docUrl.lastIndexOf('.') 1);
var extensionAppLocal = {
"doc": "ms-word",
"docm": "ms-word",
"docx": "ms-word",
"mpt": "ms-project",
"xlsx": "ms-excel",
"pptx": "ms-powerpoint",
"one": "onenote|UsePlain",
"vstx": "ms-visio"
};
var openApp = "";
if (extensionAppLocal[fileExtension]) {
openApp = extensionAppLocal[fileExtension];
}
if (docUrl.indexOf(".pdf") > 0) {
editDocumentWithProgID2(docUrl, '', 'AdobeAcrobat.OpenDocuments', '0', $.getUrlVar('siteURL'), '0', openApp);
}
else {
editDocumentWithProgID2(docUrl, '', 'SharePoint.OpenDocuments', '0', $.getUrlVar('siteURL'), '0', openApp);
}
This only works in IE 11, not Edge or Chrome. Is there another way to build a URL for an anchor tag or use Javascript to force a file to open in the host systems default application instead of download it or preview in browser?
CodePudding user response:
The way to do this is to set "file extension default application" on all PCs. On Windows, you can open "Settings", then select "Apps", then "Default Apps" (not sure how to do it on other OSes). I believe there must be a way to do this via script (almost certainly involves changing registry settings on Windows). Then your organization needs to schedule the script to run on all PCs.
CodePudding user response:
We ended up opening a ticket with Microsoft and their engineer gave us the answer:
The legacy experience that allows PDF files to open locally in Adobe Acrobat (or other default client applications) directly from the browser and from Microsoft Office add-ins relies on ActiveX controls that are installed to the client machine as part of Office. Internet Explorer is the only browser that allows/supports the use of ActiveX controls, so this functionality is not available outside of IE.
When you click a hyperlink on a web page, the browser and operating system work together to determine how to open the target resource. When the protocol scheme for the link target is http, https, or other known web protocols, the OS and the browser know to use the browser to open the requested resource. For Word and Excel documents, modern versions of Office provide additional protocol handlers (e.g. ms-word: and ms-excel:) that facilitate opening files in the local client. SharePoint 2013 and newer will render links to Office documents using those protocols. Clicking one of these links in a SharePoint page will tell the OS to use the corresponding Office application to open the file from the target location. You would see the same behavior in any other non-SharePoint HTML page if the protocol scheme is ms-word or ms-excel on a machine that has Office installed.
Adobe has not provided a similar protocol handler for PDF files with Acrobat, so there is no off-the-shelf way to open files directly in the Acrobat client directly from a SharePoint page (or Office add-in) when using a modern browser. Instead, your developers can create a custom protocol handler and render links to PDF files such that they open in a preferred application. At a high level, it would require the following...
Write a protocol handler that can: a) receive the file's location as an argument b) find the locally installed executable file for the client application (i.e. Acrobat Reader) c) issue a command to launch the client application and supply an argument specifying the file to open (i.e. the file URL in SharePoint)
Register the protocol handler on the client machine for each user who requires this type of interaction. o It may be possible to update the installer for your add-in to check for the protocol handler during the first run, and register the handler if needed. This could eliminate the need to install/register the protocol handler separately.
Update the add-in code to detect PDF files and write the hyperlinks to use the protocol handler scheme instead of http/s.
If you want to have this behavior in the SharePoint UI as well, you can use JavaScript to detect links to PDF files and rewrite HREF values to use your protocol handler scheme instead of http/s. The script can be referenced from the master page or included directly in the master page. For SharePoint Online, this change could be implemented as a field customizer, or as a list view customizer extension ( which could be limited to document library list types, if desired).
Overview
Example