Create New Office Documents in SharePoint using Javascript CSOM


Trying to create new Office documents in SharePoint using client-side development technologies, i.e. Javascript with JSOM or REST is a common enough scenario, though Googling it with Bing yields little to no help.

It is possible to create new Office documents in SharePoint using JSOM, but not REST, at this time at least. Also, the solution in this post doesn’t work for SharePoint Server 2013 on-premise, I haven’t looked at 2016 so I’m not sure in that case.

You have to dig deep into the innards of Core.js (around line 4737 in the debug version) to find the code which actually does this, so if you go to a Document Library, click New and then choose an Office Document format, as shown below;

createnewdoc01

Clicking “Word” for instance, calls into a function called CreateNewDefaultDocument in core.js, and that code is shown below;

createnewdoc02

It accepts 3 paremeters;

  1. j – this is the Id of the current list context, this can be null
  2. d – this is an integer enumeration of the Microsoft.SharePoint.Client.DocumentTemplateType type where Word=1, Excel=2, PowerPoint=3, OneNote=4, ExcelForm=5
  3. h – this is the server relative URL of an Office Document Template

The target library for the new document in this case, is determined by the function picking up the correct list context, from which, all it needs is the listName property.

i.e. the list guid in the form {A28BF06E-1374-4858-9D35-62C5904B8503}

Basically, if you pass a truthy value for d then a blank Office document of that type is created, whereas if you pass a non-truthy value for d and a valid server relative URL of an Office Document Template for h, the new document will be created from that template (this can be a template or regular document).

Once the document is created, you are navigated to the Office Web Application for that document so you can view/edit it.

So the 2 JSOM methods used are either;

  1. createDocumentFromTemplate(…)
  2. createDocument(…)

Both of these are methods of the SP.List object, and if you follow the link to the documentation page, you’ll notice that they aren’t listed. This doesn’t mean they’re not documented though, if you download the [MS-CSOMSPT]: SharePoint Client-Side Object Model Protocol document, they are documented in there, and a whole lot else too.

Now, the above scenario may well suit your needs, assuming you can deal with the function being able to determine the correct list context, however my needs where slightly different.

I wanted to create a new document using a template, then show the edit document properties dialog, and then finally (in my code) make a decision about whether to open the document in Office Web Apps, or do something else, the code to do just this, is show below and also available from this gist.

Published by

Phil Harding

SharePoint Consultant, Developer, Father, Husband and Climber.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s