none
Create a document based on specific document template using JavaScript (ECMA Script/REST API/SP Services) RRS feed

  • Question

  • Hi,

    I have requirement to create a new document based on specific document template available in document library. Able to set the specific content type but the respective document template not being applied. I am trying with ECMAScript, REST API and SPServices but no luck for now. Please help me.

    Thanks,
    JP

    Friday, May 1, 2015 10:04 AM

Answers

All replies

  • Hi JP,

    There is a method as below can be invoked to open a specific document template in Office application when create a document:

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

    More details can be found in the link below:

    http://sharepoint-edinburgh.com/2013/07/creating-documents-from-templates-with-javascript/

    However, if you want to generate a document directly using JavaScript, it won’t be achievable with the exposed SharePoint APIs at this moment.

    As a workaround, you can use Word Object Model or other Office related APIs to create documents, then upload them using SharePoint Client Object Model or REST API with JavaScript.

    About how to upload a file to SharePoint library using JavaScript:

    http://anthony-verschraegen.blogspot.com/2013/12/sharepoint-2013-js-upload-file-to.html

    A demo about how to create document with Word Object Model:

    https://msdn.microsoft.com/en-us/library/hhf98b5c.aspx

    For more information about creating a document programmatically, I suggest you open a thread in the corresponding forum, you will get more help and confirmed answers there:

    https://social.msdn.microsoft.com/Forums/office/en-US/home?category=officedev

    Thanks 

    Patrick Liang

    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Monday, May 4, 2015 5:40 AM
    Moderator
  • I think there is a work around and here are the high level steps.

    1. create a document using UI and save it to the library and name it as "new_template_doc" or something like that

    1. if you don't want users to mess with this document, secure it so that no one will delete or update it.

    2. use the REST API "/copyto(strNewUrl, bOverWrite)" method to create (copy) a new document

    /_api/Web/getfilebyserverrelativeurl('new_template_doc url')/copyto(strnewurl='destination_file_url', boverwrite=true)

    Saturday, October 7, 2017 7:50 PM