I have a document library with a DOCX template file. I want to create DOCX file in this library in a workflow but I did not succeed.

Before, I created a workflow in Sharepoint Designer to create a new item in the document library, but now I have to write this workflow in Visual Studio 2010 and it doesn't work.

Here is what I do in the workflow :

using (SPSite oSPsite = new SPSite("URL"))
    using (SPWeb oSPWeb = oSPsite.OpenWeb())
        oSPWeb.AllowUnsafeUpdates = true;

        var docLib = oSPWeb.Lists["gpReports"];

        var fileName = "Title.docx";

        Hashtable ht = new Hashtable();
        ht.Add("Title", "Title");
        ht.Add("ProjectYear", "Year");
        ht.Add("ProjectDepartment", "Department");

        docLib.RootFolder.Files.Add(fileName, new byte[] { }, ht, true);

        oSPWeb.AllowUnsafeUpdates = false;

Every method of Add need a file or a byte array, but I want that the template of the document library is used.

Using the current method, it creates an empty file in the document library.

Is there a way to do that programmatically ?

2 Answers 2


Just retrieve the template as an SPFile and use the OpenBinary() method to get the byte[]. Then copy that to your new file.

Edit. The template is not actually bound to your list but to a content type, so you need to retrieve it using its url:

byte[] f = w.GetFile(web.Lists[YourList].ContentTypes[yourContentTypeId].DocumentTemplateUrl).OpenBinary();
  • No problem, glad i could help!
    – Colin
    Commented Nov 10, 2011 at 20:17
  • What if you don't have a template associated with a content type and just want to create a new blank document?
    – Boris
    Commented Feb 5, 2013 at 15:12

Try something like that - to create your word file.

Word.Application app = new Word.Application();
Word.Document doc = app.Documents.Add(@"http://...PathToMyWordTemplate.dotx");
doc.SaveAs(FileName: @"c:\test.docx");

You can save the document in memory and pass his byteArray to the docLib.RootFolder.Files.Add... method.

Below is a codesnippet to add predefined content in the word document....

public void CreateDocument(string docName)
using (MemoryStream memStream= new MemoryStream())
// Create a Wordprocessing document.
using (WordprocessingDocument doc = WordprocessingDocument.Create(memStream,WordprocessingDocumentType.Document))
// Add a new main document part.
// Create the Document DOM.
doc.MainDocumentPart.Document =
new Document(
new Body(
new Paragraph(
new Run(
new Text(“Hello World!”)))));
// Save changes to the main document part.
//Adding Document to SharePoint
protected void AddToSharePoint(MemoryStream memStream,string fileName)
using (SPSite spSite = new SPSite(siteUrl))
//Get the document library object
SPList docLib = spSite.RootWeb.Lists["Shared Documents"];
SPFile file = docLib.RootFolder.Files.Add(fileName, memoryStream, true);
  • I had to had some ref to the calls to Add and SaveAs in order to allow the reflection to make the file compilers. But it didn't worked in my case. My workflow stops with a "Error Occured". Perhaps the problem is in my document library. Commented Nov 4, 2011 at 16:44

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.