Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

use this :

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

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

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.