I'm cross-posting this question from Stack Overflow.

I am creating a Document Library from an event receiver. I would like to change the Document Library to a custom Word document. In the user interface, this is simply done by changing the Template URL value in the Document Template property under Document Library Advanced Settings. I'm not sure how to do this via code.

I would also be open to creating a list template with my document template already setup, but I'd prefer to go the other route so I retain the flexibility of setting up each document library with a different template.


1 Answer 1


Here is simple example how to setup Document Library template via code:

using (SPSite site = new SPSite("http://mycoolsite"))
     using (SPWeb web = site.OpenWeb())
          SPList list = web.Lists["MyLibrary"];
          SPDocumentLibrary docLib = (SPDocumentLibrary) list;
          docLib.DocumentTemplateUrl = "/mylibrary/Forms/NewTemplate.doc";

However, if template is outside Document Library Forms folder you will receive error:

Invalid template URL.

The template must exist in the Forms directory of this document library. Create the template in the Forms directory, and then re-type the Web address. Note that you cannot move or copy a template into the Forms directory.

You can provide document template to forms folder of Document Library via feature using module element. Your feature element.xml would look something like this:

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
        Name="MyLibrary" SetupPath="Features\MyLibrary"
        Path="" Url="_cts/MyLibrary">
        <File Url="NewTemplate.doc" />

IMO this is not right solution for you. I suggest that you deploy custom content type already associated to custom template and when you create new library you just add content type to it. There are plenty articles on web covering this. Here are few (found by simple google search) to get you started:

Or you can use custom document templates (as you pointed in your question):

  • hi, thanks for your answer, I have a similar problem. Is it possible to create a content type (programmaticall) which has a excel default file as template and when you add this content type to a list, then it uses this template (excel file) when you create new document within the list??
    – Medes
    Mar 1, 2012 at 12:20
  • @Medes check the example on MSDN msdn.microsoft.com/en-us/library/ms460224.aspx and see if it fits Mar 1, 2012 at 23:22

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.