SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

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>
share|improve this answer
    
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 Apr 12 at 10:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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