SharePoint 2013 - Open New Document from Template

Overview

It seems that every customer I go to asks the same thing: Can we store our Document Templates in SharePoint and use them to create new instances by clicking a link?

The "SharePoint" way of doing this is, of course, is to:

  • Create a Content Type
  • Add that Content Type to a Document Library
  • Set the Template for that Content Type to the Template in that document library
  • Publish that content type
  • Add that content type to the target document library.

This solution works fine when you have a few templates. However, it does not scale well. Customers, rightly or wrongly, invariably have much more than 10 templates, and the above solution very quickly becomes

  • Onerous. Updating the templates becomes complicated
  • Creating new documents becomes onerous. You have to train users to select the document library, select the File tab, select the New drop down, then select the correct content type. Far far far too complicated.

I have tried numerous solutions (Template Hub Manager, various others) but they all seemed far to complicated, and built something that is overkill. Ideally, I wanted a solution that server a few purposes:

  • Was not complicated and worked *with* SharePoint not against it
  • Had to be low deployment. That is, it had to work with Office 365 environments. The writing is on the wall for how solutions are made for SharePoint, and they are not deploying farm based solutions
  • Clients are unwilling to pay for expensive SharePoint consultants to give solutions that take half the time in other, more traditional technologies

I was lucky enough to have the time and opportunity to explore a more scalable and useful solution for a client recently. During my investigations, I had to cobble together several disparate pieces of information to make this work. The kicker for me was that I finally had a real-world example of how to use some of the more advanced features of JavaScript and JQuery, which I have found very lacking, particularly from a SharePoint perspective.

So, I won't bore you anymore - let's get to the solution

Setting Up the Solution

Just to be clear on what we are trying to accomplish here:

  • A document library that contains all the documents and templates that users want to be able to create new documents from
  • These documents/templates are nothing special: they can be templates, or just documents. However, they must be Office documents
  • We want a categorised list of links that users will see on a page, that allows them to create new documents from existing documents(e.g. timesheet)
  • Making the default save location far more intuitive for users. That is: save to the site you clicked the link from
  • Must be able to work across site collections. (Aside: I am pretty shocked that SharePoint still does not offer a decent, scalable cross-site collection navigation solution out of the box. Managed Metadata for Navigation does not scale, and has an extremely narrow usage set.)

This is how it will look in the example I am documenting here:

image

Creating the Managed Metadata Term Sets

To be able to categorise the documents and templates, I am using a Managed Metadata Term Set. This is optional, and I am using it here because I want to give a real-world example. I could just as easily used a choice field, but I wanted to show a solution that scales across site collections, and to accomplish that, I needed to use Content Type hub. I always recommend that a content type hub be used, particularly if your site has been designed to use site collections.

  1. Navigate to your Content Type Hub (in my case this was http://spdev.deviq.local)
  2. Select Site Actions | Site Settings

clip_image001

  1. Select Term Store Management in the Site Administration section

clip_image002

  1. Create a term that will be used to categorise the documents/templates (e.g. by Department, by Process Phase, etc.). See the example below. Here, I am using 3 bits of information to help classify the content, but yours may vary. Note that the most important piece is the Document Type:
    1. Department
    2. Phases
    3. Content Types



clip_image003

Creating the Site Columns

Next, to ensure that the term store data can be used throughout the site, we are going to create some Site Columns based on those term store bits. I am going to assume that you know how to do this - image below is the result for this example.

One thing I will call out though. Whenever I create Site Columns, I always create them with short names and no spaces. This would have to be my biggest bugbear with SharePoint: Internal field names should never have spaces - it just complicates things. Once I have created the column, I go back and change the display name so that it looks OK on forms, but the internal name has no weird characters or x0020 pieces.

clip_image004

Creating the Content Type

Now that the site columns have been created, I will create a content type that contains those columns that can be used for the template document library. I will also create one for Links, as the solution I am giving also looks at a links list, and combines the links. The image below shows the Content Type creation:

clip_image005

I am going to assume that you know how to create content types and associate site columns to it. The image below shows the resulting content type definition:

clip_image006

Optional: Publishing the Content Type

If you are using a Content Type hub, this is the time to publish your content type so that it can be used throughout your site. Select the Manage Publishing for this content type and publish the content type.

Optional: Forcing the Content Type to Replicate

You will either have to wait 15 minutes or so until the content type is published, or force it by using Central Administration (Monitoring | Review Job Definitions - run both the Content Type Hub and the Content Type Subscriber for you web site)

Creating the Template Document Library

Now we can create the document library that will hold the documents and templates. Navigate to the site that will host your template library, and create a standard Document library. The same rule applies here when creating the library as with the field names: try and keep it short with no spaces or special characters. Once it has been created you can go and give it a better display name.

In this example, I am naming the document library dt, and then changing the display name to Document Templates.

clip_image007

Configuring the Document Library

Once the document library has been created, we are now going to configure it.

  1. Set the display name to something sensible (e.g. Document Templates)
  2. Change the advanced settings so that the Allow Management of Content Types is set to Yes
  3. Add the content type you created earlier and set as the default

Populating the Document Library

  1. Add documents and templates to the document library
  2. Make sure that the metadata is filled in correctly. The JavaScript below will look for either Template or External Link in the Doc Types metadata field

Optional: Create and Configure a Links List

If you want to include links to external sites in the list of links, then create a Links list and add the site columns created earlier to it

Creating a Page

Now comes the fun part :). Create a page in the site of your choice (any type - as long as you can add the web part to it). I like to use the Blank Web Part page layout in a Publishing site, but a Wiki page would work as well.

clip_image008

Adding the Snippet

Edit the page, and add the Script Editor web part to the page. Edit the snippet and add the JavaScript to the snippet.

The JavaScript Code

The JavaScript code can be downloaded from here. You will need to change some of the variables and field names if you choose to use different fields. However, it should work if you have followed this guide.

The one thing that it does show is how to use Promises with SharePoint, and is a full example of how to do successive calls.

Richard

Richard is a Director and the principal Consultant at Dev iQ Pty Ltd. He specialises in SharePoint, Team Foundation Server/Visual Studio and .NET Development.

Subscribe to richard angus

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!