11

I'm simply looking to create a link on my home page that goes directly to the button that you click to open a new instance of the template you created in your content type. If I described that wrong, the screenshot is below.

New Document

In my case, I created a new document content type, applied it to my document library. Instead of having people go to 'Add Document'> 'New Document', I want them to click on a link that immediately opens the template so they can then save it to the library.

I've found various posts on how to achieve this, but I can't seem to get the urls built right.

What's the easiest way to do this in 2010? (Is it different in 2013?)

If your suggestion involves javascript, please be specific on what I need to do to use javascript. I have no used it before, but am familiar with other programming languages.

Thanks.

  • I have done research on this and that's one of the posts I looked at. I could not get this to work (Seems like many other people couldn't either). I'm hoping to get suggestions from someone who has done this before. – spex5 Apr 28 '14 at 20:22
  • I followed these steps here: sharepoint.stackexchange.com/questions/54299/… This is the code I put on my new page under <asp:Content ContentPlaceholderID=Placeholdermain" runat="server"> code function NewSOP() { var options = SP.UI.$create_DialogOptions(); options.title = "New SOP"; options.autoSize = true; options.url = "/DocumentLibrary/Forms/SOP.aspx"; SP.UI.ModalDialog.showModalDialog(options);} </script> <a onclick="javascript:NewSOP();">New SOP</a> <div> code I just get a 404 on the link. – spex5 Apr 28 '14 at 20:41
10

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?

  • 1
    All I had to do to make this a button was change my <a> tags to <button> tags.. like this: <button onclick="window.location='example.com';">Visit Page Now</button> – spex5 May 7 '14 at 19:27
6

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.

2

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!

1

If I understand well, you want users to be able to use your Content Type as default. To do this you have to set in the library settings, in the content type section, your content type as default content type. In case you want to prevent users to use different content types, you can untick the "visible on new button" feature on all other content types.

  • No, I get that. I just want to create a hyperlink directly to the doc template. – spex5 Apr 29 '14 at 13:04
  • If you cut off all other content type options and you have only one option at "new document", then clicking the new document icon (the one on the ribbon) will open you the default template. – Marpio Apr 29 '14 at 13:36
  • My goal is to reduce the amount of clicks the user has to make. I want a hyperlink from the home page of my team site called "Create new doc" or something like that, that goes directly to the new document icon in the ribbon. Currently, if I add the doc lib web part on the home page, they have to click "add document", click the document tab in the ribbon, click New Document, then click the new document icon. – spex5 Apr 29 '14 at 13:50
  • In this case I don't think there is other solution than javascript. – Marpio Apr 29 '14 at 14:06
  • That's what I thought. Working on it. If I get it, I'll post it back here. Thanks for your responses. – spex5 Apr 29 '14 at 14:20
1

SharePoint 2010 link to new Word Doc:

I extracted the "createNewDocumentWithRedirect2" function out of the _initTRAWebPartWPQ2() function created by SharePoint. _initTRAWebPartWPQ2() may be named something else (WPQ2) may be web part relevant.

I got this by browsing to my document library, viewing page source, and searching for "createNewDocument". You will have to replace the JSON special characters i.e. \u002 with their corresponding plain text.

EDIT I just realized that this is pretty much the same answer that spex5 gave. I should have read his comment a little more thoroughly. If you leave the "CoreInvoke", you don't have to replaces the JSON special characters.

This is what I came up with (code has not been tested):

<script>
    function newDoc(){
        createNewDocumentWithRedirect2(event,
            'http://yoursite.com/site/docLibrary/Forms/template.dotx',  //URL To Template
            'http://yoursite.com/site/docLibrary', //URL to Doc Library
            'SharePoint.OpenDocuments',
            false,
            'http://yoursite.com/site/_layouts/CreateNewDocument.aspx?id=http://yoursite.com/site/docLibrary/Forms/template.dotx', //URL to Create New Doc
            true,
            0);    
    }        
</script>
<a onclick='newDoc()' href='javascript:void(0)'>New Doc</a>
-1

Just create a link with the template file.

You can find template url location from Document Library Settings => Advanced Settings ->Template URL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.