Saturday, June 06, 2015

Create a shortcut link to the "New Document" ribbon menu item to create a new document from a document template

The ribbon in SharePoint 2013 is useful but not user friendly if one is looking to create a document based on a pre-defined document template that is associated with a content type. Find below the code snippet that can be used to add a custom JQuery button or override existing script on button(s) to easily open a document based on a pre-defined Word template that is associated with the document library or content type:

Primary function

// Ensure that you replace the siteURL, docTemplateURL and docLibraryURL with your own values
// e.g. siteURL = http://intranet/
// e.g. docTemplateURL = http://intranet/doc%20ibrary/forms/template.docx
// e.g. docLibraryURL = http://intranet/doc%20ibrary
function createNewDocumentInstance(siteURL, docTemplateURL, docLibraryURL){

  var createDocumentURL = siteURL + "/_layouts/CreateNewDocument.aspx?id=" + docTemplateURL;

  // Invoke the internal SharePoint function to create a new document based on the document template
  createNewDocumentWithRedirect2(event, docTemplateURL, docLibraryURL, 'SharePoint.OpenDocuments', false, createDocumentURL, true, 0);

}

Possible Use (EXAMPLE Only)

function replaceAnchorTagOnClickEventAndHref(aTag, siteURL, docTemplateURL, docLibraryURL)
{
  aTag.click(function() {
   // Handle home page hyperlink update to open new document instance based on content type
    createNewDocumentInstance(siteURL, docTemplateURL, docLibraryURL);
    return false;
  }); // End onclick event for aTag to open document in New Window
 
  aTag.attr("href", "#");
}
$(document).ready(function() {

  // Find the anchor tag that needs to be set with the onclick event
  // e.g. if the anchor text is set to 'My New Document Hyperlink'
  var aTag = $(".link-item a:contains('My New Document Hyperlink')");

  if (aTag.length > 0) {
    replaceAnchorTagOnClickEventAndHref(aTag,
               "http://intranet/",
             "http://intranet/MyDocLibrary/Forms/TestContentType/TestContentTypeTemplate.docx",
            "http://intranet/MyDocLibrary/");
}