Opening Microsoft Office Documents and Other Types of Files for Editing From a Web Page

Ajax WebDAV Library provides API for opening documents for editing directly from the server and saving back to the server without download and upload steps. This includes editing Microsoft Office documents, as well as any other file types and works on Windows, Mac OS X and Linux.

Opening Document for Editing

To open the document for editing call the EditDocument() function passing document URL as a parameter and optionally mounting path and an error callback function:

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript" src="ITHitWebDAVClient.js" ></script>
</head>

<body>
    <script type="text/javascript">
        function edit() {
            ITHit.WebDAV.Client.DocManager.EditDocument("https://server/folder/file.docx", "/", protocolInstallCallback);
        }
            
        // Called if protocol handler is not installed
        function protocolInstallCallback(message) {
           var installerFilePath = "/Plugins/" + ITHit.WebDAV.Client.DocManager.GetInstallFileName();
            
           if (confirm("This action requires a protocol installation. Select OK to download the protocol installer.")){
               window.open(installerFilePath);
           }
        }
    </script>
    <input type="button" value="Edit Document" onclick="edit()" />
</body>
</html>

Note that document URL always must be a full path including the domain name.

The EditDocument() call chooses a best available method for opening this type of document depending on file type. It works both with MS Office Files as well as any other types of files on Windows, Mac OS X and Linux:

  • In the case of a Microsoft Office document, the Ajax WebDAV Library is using protocol handler installed by MS Office 2010 SP2 and later to open Microsoft Office documents for editing. This approach is available on Windows and Mac OS X only if MS Office 2010 SP2 and later is installed (MS Office 2011 or later in case of OS X). If document opening failed (for example if MS Office is not installed) the davX: protocol is used.
  • In the case of a non-Microsoft Office document, the WebDAV Ajax Library is using custom davX: protocol. This approach is available on Windows, Mac OS X and Linux.

The EditDocument() function as well as other functions in DocManager are using a callback function to report if required protocol is available on the client machine. If the protocol is not installed, the callback function is executed. In this callback, you will typically request a davX: protocol installation and will redirect to the installer that is specific to a client OS. As soon as every OS requires a different installer the IT Hit WebDAV Ajax Client library provides DocManager.GetInstallFileName() function that returns installer file name depending on OS on which the client code is executed.

Important! Microsoft Office on Windows and OS X as well as Windows Shell (Web Folders / mini-redirector), requires secure SSL connection when used with Basic authentication. Microsoft Office will fail to open a document via insecure connection with Basic authentication. For a workaround please see the following articles. In case of MS Office on Windows: You cannot open Office file types directly from a server that only supports Basic Authentication over a non-SSL connection with Office applications. In case of MS Office on OS X: You cannot open Office for Mac files directly from a server that supports only Basic authentication over a non-SSL connection.

Customizing Open Document Editing

In case you wish to avoid using davX: protocol, you can use MicrosoftOfficeEditDocument() function call. This function can open MS Office documents only and is using protocol extensions installed by Microsoft Office 2010 SP2 and later. To find out if this type of document is a MS Office document call IsMicrosoftOfficeDocument() function:

var sDocumentUrl = "https://server/path/file.docx"; // this must be full path
var oNs = ITHit.WebDAV.Client.DocManager;
if (oNs.IsMicrosoftOfficeDocument(sDocumentUrl)) {
    oNs.MicrosoftOfficeEditDocument(sDocumentUrl, protocolInstallCallback);            
} else {
    oNs.DavProtocolEditDocument(sDocumentUrl, null, protocolInstallCallback);
} 

Note that this function does not support MS Office 2007 and earlier versions.

The MicrosoftOfficeEditDocument() can open for editing the following file types:

  • Microsoft Word documents: docx, doc, docm, dot, dotm, dotx
  • Microsoft Excel documents: xltx, xltm, xlt, xlsx, xlsm, xlsb, xls, xll, xlam, xla
  • Microsoft PowerPoint documents: pptx, pptm, ppt, ppsx, ppsm, pps, ppam, ppa, potx, potm, pot
  • Microsoft Access documents: accdb, mdb
  • Microsoft InfoPath documents: xsn, xsf
  • Microsoft Publisher documents: pub
  • Microsoft Visio files: vstx, vstm, vst, vssx, vssm, vssm, vss, vsl, vsdx, vsdm, vsd, vdw,
  • Microsoft Project files: mpp
  • OpenOffice Writer: odt
  • OpenOffice Calc: ods
  • OpenOffice Impress: odp

For all other types of documents use the DavProtocolEditDocument() or EditDocument() functions.

Server Requirements for Microsoft Office Documents Editing

  1. Your WebDAV Server must support Class 2. Microsoft Office will lock the item that is being opened for editing.
  2. In the case of Microsoft Office 2007 and earlier your WebDAV server must be available on the site root, it must process OPTIONS and PROPFIND requests.
  3. In case, your server is using Basic authentication your server must be accessed via HTTPS/SSL (at least with a self-signed certificate).

Using the davX: Protocol

In case you prefer using the davX: protocol, call the DavProtocolEditDocument() function.

The DavProtocolEditDocument() function is doing the following:

  1. Mounts WebDAV folder provided in the second parameter in your local file system. It will present the login dialog if required. The logic of the log-in dialog depends on OS. On Windows and Mac OS X the OS-powered login will be displayed while on Linux the protocol application will present its own login dialog. The login dialog is presented only during first document access.
  2. Executes default OS file open command.

The DavProtocolEditDocument() function accepts following parameters:

  • Document Url. Url of the file to be opened. This must be a full path including the domain name.
  • Url to be mounted. URL of the folder that will be mounted on your local filesystem. Usually, this is your WebDAV server root. In some cases, your WebDAV server root is not the same as your website root (for example your WebDAV may be located on https://webdavserver/mywebdavfolder/). In the case the second parameter is omitted, the API will pick the folder in which the file is located. For example, if your file URL is https://webdavserver/folder/document.ext it will mount https://webdavserver/folder/ in your file system. As a result, if this parameter is not specified, you may find several folders mount in your local file system after opening documents located in different folders.
  • Error callback. A callback function that will be called if the opening of the document failed. In this callback, you will typically request a protocol installation and will redirect to the installer that targets client OS.

Opening more than one document at a time

The functionality described in this paragraph is introduced in IT Hit WebDAV Ajax Library v3 Beta.

Using the DavProtocolEditDocument() function you can open more than one document simultaneously. Due to the web browser protocol limitations you can not achieve the same functionality by calling DavProtocolEditDocument() / EditDocument() functions in a cycle. Instead you must call DavProtocolEditDocument() passing a coma separated list of document URLs. All documents must reside under the common root specified in sMountUrl parameter.

ITHit.WebDAV.Client.DocManager.DavProtocolEditDocument(
    'https://server/dav/fldr1/doc1.ext,https://server/dav/fldr2/doc2.ext', // List of document URLs.
    'https://server/dav/',  // Mount URL, must be common for all docs in sDocumentUrls.
    protocolInstallMessage  // Function to call if protocol app is not installed.
);

Triggering 'Open With' dialog

The functionality described in this paragraph is introduced in IT Hit WebDAV Ajax Library v3 Beta.

Using the DavProtocolEditDocument() function you can trigger the system 'Open With' dialog, so user can select the application to use for opening the document. To show 'Open With' pass the 'OpenWith' value to the sCommand parameter:

ITHit.WebDAV.Client.DocManager.DavProtocolEditDocument(
    'https://server/dav/fldr1/doc1.ext', // Document URL(s).
    'https://server/dav/',               // Mount URL.
    protocolInstallMessage,              // Function to call if protocol app is not installed.
    null,                                     
    null,                                // Use Basic/Digest/NTLM/Kerberos, do not pass cookies.
    null,                                     
    null,                                     
    'OpenWith'                           // Command to execute.
);

The 'Open With' dialog is also automatically displayed for files with extensions that are not associated with any installed client application and for files without extensions.

The 'OpenWith' is supported on Windows and OS X only.

See Also:

 

 

Opening Photoshop Files for Editing from a Web Page | Opening Office Documents in a LibreOffice for Editing from a Web Page | Opening AutoCAD Files from a Web Page for Editing | Opening PDF File in Adobe Acrobat for Editing from a Web Page