Question

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.

Thanks.

Was it helpful?

Solution

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";
          docLib.Update();
     }
}

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/">
    <Module 
        Name="MyLibrary" SetupPath="Features\MyLibrary"
        Path="" Url="_cts/MyLibrary">
        <File Url="NewTemplate.doc" />
    </Module>
</Elements>

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):

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top