Sharepoint - Create a link to "New Document" template link

Ok, I got the formatting right. The first reply was correct from Aboba with the link to the blog post. I just didn't have the formatting right the first time.

I ended up with <a onclick="CoreInvoke(&#39;createNewDocumentWithRedirect2&#39;,event,&#39;http:\u002f\u002fsiteurl\u002fDocumentLibrary\u002fForms\u002fSOP\u002fSOP.dotx&#39;, &#39;http:\u002f\u002fsiteurl\u002fDocumentLibrary\u002fForms\u002fSOP\u002fSOP.dotx&#39;, &#39;SharePoint.OpenDocuments&#39;, false, &#39;http://siteurl/_layouts/CreateNewDocument.aspx?id=http://siteurl/DocumentLibrary/Forms/SOP/SOP.dotx&#39;, true, 1); return false;" href="#">New SOP</a>

I put this code in a content editor web part and I get a hyperlink that directly opens the template. Sweet.

Does anybody know how I can edit this code so the hyperlink is a button instead?


Use this method:

ms-word:nft|u|TEMPLATE URL HERE|s|SAVE LOCATION HERE

^That is the URL

Note: The beginning prefix tells the browser which tool to launch. So if the template is an Excel file, you'll need to change that to 'ms-excel:nft|'. I'm not positive that the Excel method works but it works for Word as shown above.

Also, make sure not to add any spaces or it will not work.


I got the idea how to make this work from Darrell Lloyd Harvey's comments, but his code did not work in my version of SharePoint 2016 Enterprise (standalone, on-premise). I think MS changed the way the call was made and the function name to be called. Eventually I found out how to make it work, after doing a "view source" and finding "createNewDocument" there. I didn't find it the first one or few times, because I was working in Google Chrome, which doesn't always show you everything in it's debugger, but I found this "createNewDocument" when looking at View Source of the document library page after I was looking for something else (for yet another way of solving this problem). I never found another way yet to solve this problem but the code below works in my SharePoint 2016 environment. After I found "createNewDocument" I translated the hex escaped characters to get the actual javascript call to be executed which is what I will post below that is similar to what I found (taking out specifics of my site of course). And I put it inside of an anchor ref:

(In my solution I had uploaded a xlsx template file and set the library to use it as it's template. For this code to below to work, you must upload the xlsx file into the "Forms" folder of the document library as I did.):

(I put this function code and anchor ref in using SharePoint designer, under asp:Content ContentPlaceHolderId="PlaceHolderMain" and above the WebPartPages:WebPartZone and XlslListViewWebPart stuff):

<a onclick="CoreInvoke('createNewDocumentWithProgIDEx',event,                             
'https://YourDomain/YourSubsite/YourDocumentLibrary/Forms/YourTemplate.xlsx', 
                'https://YourDomain/YourSubsite/YourDocumentLibrary', 
                'SharePoint.OpenDocuments', 
                false, 'ms-excel')" href="#">New XLXS Document</a>

But there is a better way to use this function, so that you don't have to edit and embed your domain, subsite and document library names, which can and will change from project-to-project:

<script type="text/javascript">
        function newDocumentFromTemplateFile(doctype, templateFileName){
            var path = location.protocol + '//' + location.host + location.pathname
            var i = path.lastIndexOf("/") //We want to strip off the View filename at the end
            if (i > 0) { //probably no way we would get i = 0 here.
                path = path.substr(0,i+1);
                if (!path.endsWith("/")) path = path + "/" 
    //Add the back slash at the end if it is not already there. There is probably no way that this can happen though, because the backslash should always be there!
                var docLibraryPath = path.replace("Forms/",""); 
    //Our path here has "Forms/" at the end of it too remove to get the document Library name.
                CoreInvoke('createNewDocumentWithProgIDEx',event, 
                           path + templateFileName, docLibraryPath, 
                           'SharePoint.OpenDocuments', false, doctype); 
    //doctype can be: 'ms-excel' and probably other values like 'ms-word', etc.
            }
        }  
    </script>

<a onclick="newDocumentFromTemplateFile('ms-excel','YourTemplateFile.xlsx');" href="#"><img src="/_layouts/15/images/lg_icxlsx.png" alt="New"></img></a> 
<b><a onclick="newDocumentFromTemplateFile('ms-excel','YourTemplateFile.xlsx');" href="#">New XLSX Document</a></b>

This way is generic and reusuable - could put this function into a common JavaScript Library file and re-use it!

Also, by the way, I am also using css to hide the +New and Upload buttons, and the New and Upload Ribbon Action buttons. (It appears to not be easy to hide the Upload Ribbon Action button without also hiding the New Ribbon Action button.)

<style type="text/css">
    /* Hide the +New Button on the screen. */
    button#QCB1_Button1 {
        display: none;    
    }
    /* Hide the Upload button on the screen. */
    button#QCB1_Button2 {
        display: none;
    }

    /* This hides all of the "New" buttons in the Ribbon, including the New Button and the Upload Button in the Ribbon. 
       (This will probably hide all of the user defined Action buttons in the Ribbon as well.) */
    #Ribbon\.Documents\.New {display:none;}
</style>

That CSS above is to be placed just below WebPartPages:WebPartZone element in the same asp:Content section that we placed our JavaScript function, but below WebPartPages:WebPartZone and above the asp:Content end tag. I got the CSS Hide ribbon code from elsewheere, but figured out the button#QCB1_Button1 and button#QCB1_Button2 in the Google Chrome debugger.

I found the links for the Excel Icons, but if you find this section in your html/code it should show you the actual icons you used, if you used something other than MS Excel:

toolbarData['NewMenuData'] = "[{'Command':'NewDocument','CommandValueId':
'CoreInvoke\\u0028\\u0027createNewDocumentWithProgIDEx\\u0027
* * *
'Image16by16':'\\u002f_layouts\\u002f15\\u002fimages\\u002ficxlsx.png',
'Image32by32':'\\u002f_layouts\\u002f15\\u002fimages\\u002flg_icxlsx.png',

This translated to:

'Image16by16':'/_layouts/15/images/icxlsx.png',
'Image32by32':'/_layouts/15/images/lg_icxlsx.png',

Thanks everyone for giving their input on this difficult problem to solve! It helped me!