Creating Documents from Templates with JavaScript

We can decide whether documents within a document library should open in the Web App (within the browser) or in the client’s local Microsoft Office application in Library Settings -> Advanced Settings.OpenInBrowser

Whilst SharePoint obeys this setting when opening existing documents, it ignores it when creating new ones.  If the client’s Microsoft Office application is available, SharePoint will use it to create documents, regardless of the library or site collection settings.


Creating Documents via the Web App

Fortunately, we can use the JavaScript in SharePoint’s core.js to create documents within the Web App, regardless of the client’s local Office installation…

CoreInvoke('createNewDocumentWithRedirect2', event, '[template URL]', 'http://[server]' + (ctx.rootFolder == '' ? ctx.listUrlDir : decodeURIComponent(ctx.rootFolder)), 'SharePoint.OpenDocuments', false, 'http://[server]/[subsite]/_layouts/15/CreateNewDocument.aspx?id=[escaped template URL]', false, 1, '[web app]');

For example, to open the standard document library template in the Shared Documents document library on server SharePointDev01…

CoreInvoke('createNewDocumentWithRedirect2', event, 'http://SharePointDev01/Lists/Shared%20Documents/Forms/template.dotx', 'http://SharePointDev01' + (ctx.rootFolder == '' ? ctx.listUrlDir : decodeURIComponent(ctx.rootFolder)), 'SharePoint.OpenDocuments', false, 'http://SharePointDev01/_layouts/15/CreateNewDocument.aspx?id=http%3a%2f%2fSharePointDev01%2fLists%2fShared%20Documents%2fForms%template.dotx', false, 1, 'ms-word');


Creating Documents via the Client’s Microsoft Office Application

Similarly, we can just as easily create documents within the client’s Office application, assuming it’s available…

CoreInvoke('createNewDocumentWithProgID', '[template URL]', 'http://[server]' + (ctx.rootFolder == '' ? ctx.listUrlDir : decodeURIComponent(ctx.rootFolder)), 'SharePoint.OpenDocuments', false, '[web app]');

To give the same example as above…

CoreInvoke('createNewDocumentWithProgID', 'http://SharePointDev01/Lists/Shared%20Documents/Forms/template.dotx', 'http://SharePointDev01' + (ctx.rootFolder == '' ? ctx.listUrlDir : decodeURIComponent(ctx.rootFolder)), 'SharePoint.OpenDocuments', false, 'ms-word');


Additional Points

The available values for the application parameter are 'ms-word', 'ms-powerpoint', 'ms-onenote' and 'ms-excel'.

The following JavaScript evaluates to be the site-relative URL of the document library (or subfolder if applicable) in the current context.

ctx.rootFolder == '' ? ctx.listUrlDir : decodeURIComponent(ctx.rootFolder)

That contextual reference is great for use within a web part on a document library’s list view page.


Grouping List By Content Type

Simple Task
Create a custom view on a SharePoint list grouping the items by Content Type

You just select Content Type from the Group By field list right? Eh, where is the Content Type field?  I’ve always been confused as to why this isn’t supported out the box by SharePoint but I guess there must be a good reason!?

Salvation is here though fellow SharePointers, you can achieve grouping by Content Type by using SharePoint Designer.  The simplest approach is to create your custom view in the SharePoint web interface and group on one of the available columns (Title, Modified, Created etc) and then save the custom view.  Open SharePoint Designer and navigate to your list, double-click your custom view from the Views list (see below).

Once the view is open in the editor, search for “<GroupBy>” which will take you to the GroupBy filter fields, change the current fieldname in the Name parameter of the FieldRef element to “ContentType” and click save

That’s it, a SharePoint list grouped by Content Type!  Easy when you know how.

NOTE: If you edit the view again via the web interface the customisation will be lost as there is no option in the Group By field list for ContentType.