Opening Office Templates (.dotx) from a SharePoint Document Library

I ran across this problem while doing some work for a client where they had added a bunch of Word templates to a document library. The issue was that users would click on these documents and Word opens them fine but instead of it creating a new version of the document users are instead editing the template. This created confusion for the users, when they tried to edit the template they would be prompted for credentials since they didn’t have contribute access to the library. The screenshot below highlights the default behaviour – in this case we are editing the template, rather than a new document as one would expect.

 

This is the standard method that SharePoint uses to handle documents. I assume the suggested approach would be to associate a template with a content type. This is fine if you want to save the new document back to SharePoint but what if you would like to store the document elsewhere or simply print it.

I dug up a solution on an old blog post titled .dot templates in SharePoint Document Library, one of the comments by a user named Tom posted a relatively simple solution – especially if you already have jQuery available on the page.

The following code can be added to a list view or any other page in SharePoint that contains links to documents.

This JavaScript code picks up any links to templates (.dot, .dotx, .xlt, .xltx) and removes the existing onclick event. It then binds a built in SharePoint function, createNewDocumentWithProgID, to open the template in the client application. This is the same function that is called if you have a content type associated with a library and you select New from the ribbon. I have tested this in SharePoint 2010 but the code also apparently works for 2013.

Leave a Reply

17 Comments

  1. Peter

    Hi, how do you add it to the list view sorry? SharePoint Newbie!

    • Tom O'Connor

      Hi Peter,

      If you open up the list view that you have this functionality on you should have an option to edit the page. You can then add an Content Editor web part and then link this script on the page using that.

      Cheers,

      Tom

  2. Damian

    I liked this script snippet however I have a document library that is grouped by category and so running the script in this way does not work.

    I was able to adapt the script using the JQuery .on() feature.

    First I used the “mouseenter” event to remove the existing “onclick” event from the link.

    Then the “click” event opens the document template in the same way as the snippet in the article.

    Personally I don’t like using the “mouseenter” event as it will fire each time the mouse scrolls over the link when it only needs to be done once. However there is no noticeable slowness as a result of doing it this way.

    This script works in a document library in SharePoint 2013 On-Premise envornment.

    $(document).ready(function () {
    $(“table.ms-listviewtable”).on(“mouseenter”, ” a[href$=’.dot’], a[href$=’.dotx’], a[href$=’.xlt’], a[href$=’.xltx’]” ,function(event){
    $(this).attr(“onclick”, “”);
    });

    $(“table.ms-listviewtable”).on(“click”,” a[href$=’.dot’], a[href$=’.dotx’], a[href$=’.xlt’], a[href$=’.xltx’]”, function () {
    var saveLocation = jQuery(this).attr(“href”).split(“/”).slice(0, -2).join(“/”);
    createNewDocumentWithProgID(window.location.protocol + ‘//’ + window.location.host + jQuery(this).attr(“href”), window.location.protocol + ‘//’ + window.location.host + saveLocation, ‘SharePoint.OpenDocuments’, false);
    return false
    });
    });

    • Tom O'Connor

      Thanks Damian, that makes sense with the grouping. Cheers for posting your code.

    • Jeff

      Where would you enter this code in?

      • Tom O'Connor

        This can be included anywhere on the page as long as it resides within some JavaScript script tags and you have jQuery included on the page.

  3. It only works for me in 2 of 3 scenarios:
    – manual link : works *
    – library web : works *
    – content query : doe not work

    * a window is displayed with the following… however Word does open it up as a new document

    The document could not be created.
    The required application may not be installed properly, or the template
    for this document library cannot be opened.

    Please try the following:
    1. Checek the General Settings for this document library for the name of
    the template, and install the application necessary for opening the template.
    If the application was set to install on first use, run the
    application and then try creating a new document again.

    2. If you have permissions to modify this doucment library, go to
    General Settings for the library and configure a new template.

  4. Niels

    Thanks Peter,

    This is exactly what I was looking for. Do you (or anybody else) have any idea if this works for SharePoint Online?

    • Niels

      Sorry Tom, misread the author’s name..

    • Peter

      Hi Niels

      The script from Tom worked perfectly for us on our Office 365 SharePoint-Account. But since a few days, the script gives an error message. It seems, that href part of the link has changed. It’s no longer possible to extract the path to the template.

      Has anybody an idea, were I can get the path to the template from?

      • Tom O'Connor

        Hi Peter,

        The above solution will be dependent on the structure of the page remaining relatively similar. I’m guessing with the new doc library experience that the way these documents are opened has likely changed.

        If I find some time I’ll try to do some further digging but let me know if you come across a solution.

        Cheers
        Tom

  5. Arno

    Thanks Tom and Damian!
    This seems to be what I am looking for, but I have a similar problem as Damian.
    On some pages I use document library webparts that have too many documents to be shown in one page and you have to click the “next” button in the Webpart.
    The provided code works for links in the first webpart page but not for links on the next pages. Why are the links that are dynamicaly loaded after user click not changed? The click or mouseenter only fires for links that were present on inital page load. Do you have any idea how to improve this?

    • Tom O'Connor

      Hi Arno,

      The solution above would only run against content that is first loaded on the page. When you click next it would do a query and grab the next page of documents from the server. You would have to tie the script above to also run after you click the next button. JQuery allows you to bind to click events of particular objects – I’d suggest investigating this.

      Cheers,
      Tom

  6. Joe

    Hi Tom & Damian,

    This is exactly the solution I am looking for but can not get the script to work using Script Editor in Sharepoint 2013, any idea’s?
    -JQueryNoob:P

    (function($j) {
    $j(document).ready(function () {
    $(“table.ms-listviewtable”).on(“mouseenter”, ” a[href$j=’.dot’], a[href$j=’.dotx’], a[href$j=’.xlt’], a[href$j=’.xltx’]” ,function(event){
    $j(this).attr(“onclick”, “”);
    });

    $j(“table.ms-listviewtable”).on(“click”,” a[href$j=’.dot’], a[href$j=’.dotx’], a[href$j=’.xlt’], a[href$j=’.xltx’]”, function () {
    var saveLocation = jQuery(this).attr(“href”).split(“/”).slice(0, -2).join(“/”);
    createNewDocumentWithProgID(window.location.protocol + ‘//’ + window.location.host + jQuery(this).attr(“href”), window.location.protocol + ‘//’ + window.location.host + saveLocation, ‘SharePoint.OpenDocuments’, false);
    return false
    });
    });
    })(JQuery);

  7. Joe

    Hi Tom,

    Sorry for the double post but I got the script to work using your method but am receiving a compatibility error.

    If I use the standard method Sharepoint recommends by creating a content type and associating it with a specific template, the “New Document’ function works but when using your initial script, I receive

    ‘New Document’ requires a Microsoft SharePoint Foundation-compatible application and web browser. To add a document to this document library, click the ‘Upload Document’ button.”

    Any suggestions?

    • Tom O'Connor

      Hi Joe,

      Sorry for the delay in getting back to you. To my knowledge it should be calling the same function – it might be worth having a look to see if you can see which function is being called by the New Document menu. It has been a while since I’ve looked at this so it’s highly possible that this has changed if, for example, you are using SP2016 or Office 365.

      The script above calls createNewDocumentWithProgID, it would be interesting to see if this is still the same function that is being called.

      Cheers
      Tom

  8. Curzio

    Hello,
    any idea why the original script opens me the SharePoint Designer instead of the Office application?

Next ArticleReflections on the Metadata MOOC