I have a document library with custom content type (template).

When I click the "new" button in the library it lets me to choose the template for new document or folder (default functionality).

Then I choose the template, it creates a new document using the template and opens it in Word Online for editing (I'm using SharePoint Online).

I am trying to somehow skip the opening of the new document.

Some time ago I had the same requirement on-prem and did this using C# code behind.

Now I'm looking for an alternative using javascript in content editor web part.

I've read lots of topics an tried lots of things, but didn't get it to work...

I have this bit of code at the moment, it creates the document on button click, but the document is somehow damaged, doesn't open at all...

var clientContext = new SP.ClientContext(siteUrl);
var library = clientContext.get_web().get_lists().getByTitle('MyLibrary');
var rootFolder = library.get_rootFolder();
var files = rootFolder.get_files();
files.addTemplateFile("/sites/MySite/MyLibrary/MyDocument.docx", 0);

So it creates new "MyDocument.docx" file in "MyLibrary", but when I try to open it I get an error "The file is corrupt and cannot be opened"

I have checked the properties of the created document, everything looks fine...

Any ideas? Do I have to pass any other properties? Thanks!

  • I am just guessing, but won't you need to call file.update or file.close or ctx.executequery? Apr 12, 2016 at 13:37
  • @Nisarg , I am using ctx.executeQuery, just didn't include the code here, sorry. I think I need to pass some more properties to .addTemplateFile() method, but I can't find this method's documentation... Or there might be another way of doing this?
    – Gintas K
    Apr 12, 2016 at 13:44

2 Answers 2


I guess creating documents based on a template is not available in JSOM, however you can create such documents within a SP Lib, by directly using Office API method baked in SP core.js.

Creating Documents via the Web App

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]');

Creating Documents via the Client’s Microsoft Office Application

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

You can create MS Word, Excel, PowerPoint & OneNote documents by above approach, following paramters are available: 'ms-word', 'ms-powerpoint', 'ms-onenote' and 'ms-excel'.

Have a look at this blog post, from where i copied all above information.

Also check this URL.

  • Thanks Muhammad. I saw all these articles and I tried using this as well, although I get an error "TypeError: Cannot read property 'listName' of null" within core.js any ideas?
    – Gintas K
    Apr 12, 2016 at 13:53
  • Yes, i guess the script will only work on the pages where there is a context of the list, try adding the same script on list's new, edit or view pages. As mentioned in the blog, the code is reading ctx information using this line ctx.rootFolder == '' ? ctx.listUrlDir : decodeURIComponent(ctx.rootFolder). May be you could try to create and pass your own context obj, and see if it works...
    – Usman
    Apr 12, 2016 at 13:59
  • I've just tried to do this in some of the list pages, I still get the same error... I will try to use my own clientContext as I did above (see my question) instead of ctx
    – Gintas K
    Apr 12, 2016 at 14:04
  • Yes.. This is what I also suggested...
    – Usman
    Apr 12, 2016 at 14:09

Try look into this

Change the 'Trust Center' Settings: Sometimes, the enabled protected view settings under 'Trust Center' prevent the Word file from opening. Mentioned below are the steps to change the settings of 'Trust Center':

  1. Open your MS Word application.

  2. Click 'File >> Options'.

  3. Select the 'Trust Center' and press button under 'Trust Center Settings'.

  4. Click 'Protected View'.

  5. Uncheck all the options available under 'Protected View' and click 'OK' to confirm.

  6. Restart MS Word and now, try to open your corrupt Word document.

  • It still doesn't open
    – Gintas K
    Apr 12, 2016 at 12:19
  • Updated my answer. Please look into it. :)
    – Hardik
    Apr 12, 2016 at 12:31
  • Please read my question again. I know how to do it in C# even easier. I need to achieve this with javascript/jQuery on client object model
    – Gintas K
    Apr 12, 2016 at 12:37
  • Sorry but have you tried look into this? social.msdn.microsoft.com/Forums/sqlserver/en-US/…
    – Hardik
    Apr 12, 2016 at 12:50
  • @Gintas K, so you are being able to use a similar code in C#, where it is working, but the JS code is not working for you? Apr 12, 2016 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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