4

As in the title, I need to programmatically create a new document based on a content type template. Does anyone know the best way to do this? Normally, to add files you use Web.Files.Add(), but all those overloads take a byte stream.

1 Answer 1

3

use this :

var tplurl = doclib.ContentTypes["MyContentType"].DocumentTemplateUrl;
byte[] stream = web.GetFile(tplurl).OpenBinary();

SPFile file = myFolder.Files.Add("myFileName", stream, true);
2
  • The above code give problems for the .docx files as file is could not be opened? any ideas.
    – user16674
    Commented Apr 28, 2013 at 23:55
  • I forgot to mention that I am using a docx as template for my content type not a dotx.
    – must19
    Commented May 4, 2013 at 18:16

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.