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

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.

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

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

share|improve this answer
    
awesome, thank you! –  Nathan DeWitt Sep 8 '11 at 20:12
    
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 '12 at 12:20
    
@Medes check the example on MSDN msdn.microsoft.com/en-us/library/ms460224.aspx and see if it fits –  Vedran Rasol Mar 1 '12 at 23:22
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.