2

I require to programatically create a new document using the Document Template URL of the document Library and this requires to be done using the Client Object Model. I also require to do this without using workflow.

Could someone advise on how this can be done?

1 Answer 1

1

You can try something like this:

<script>

  function createDocument()
  {
    var clientContext = new SP.ClientContext.get_current();
    var web = clientContext.get_web();
    var docLib = web.get_lists().getByTitle("Docs");

    var rootFolder = docLib.get_rootFolder();
    clientContext.load(rootFolder);

    var files = rootFolder.get_files();
    clientContext.load(files);

    clientContext.executeQueryAsync(
      function()
      {
        files.addTemplateFile(rootFolder.get_serverRelativeUrl() + "/foo.docx", 0);
        clientContext.executeQueryAsync();
      }, 

      function(sender, args){alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());}
    );
  }    
</script>

<div><a href="javascript:void(0)" onclick="createDocument()">Create Document</a></div>
1
  • Hi @Matthias , I've just tried to implement your code, it creates the document but it is somehow damaged, It doesn't open. I am using a custom content type with custom document template. Do I have to specify any other properties in this case? any ideas? Thanks!
    – Gintas K
    Commented Apr 12, 2016 at 10:21

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.