in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Ton Stegeman [MVP] weblog

I have moved my blog to http://www.tonstegeman.com/blog. If you have a blogpost at sharepointblogs that does not display all the content on the right hand side, please go to the new blog. It has all the posts that are on this blog as well. I you have any feedback, please send me a message through the contact form.

Document templates and SharePoint 2007 content types

One of the most powerful new features in WSSv3 (and of course MOSS 2007) are content types. One of the benefits of having content types is that we now can have multiple document templates in one single document library. Each content type can have a document template. By attaching multiple content types to a library, we can offer multiple templates to our users. Just as in the previous version of SharePoint we can still set a document template for a specific document library.

Document templates can be set in the SharePoint user interface, but it can also be done in code. This post will show some examples that will help you to get started.

Example 1 – Template for a document library

This example shows how you can upload a document to SharePoint and use that as the document template for your document library. First the code checks if a document library called ‘MyDocuments’ is available. If not it is created. If the document is not yet uploaded to the Forms folder in the library, it is uploaded. The last step in this codesample sets the DocumentTemplateUrl property of the library.

    string libraryName = "MyDocuments";
    string templateName = "MyTemplate.doc";
 
    SPSite site = new SPSite("http://office2007");
    SPWeb web = site.OpenWeb();
    SPDocumentLibrary doclib = null;
    foreach (SPList list in web.Lists)
    {
        if (list.Title == libraryName)
        {
            doclib = (SPDocumentLibrary)list;
            break;
        }
    }
    if (doclib == null)
    {
        System.Guid doclibid = web.Lists.Add(libraryName, string.Empty, web.ListTemplates["Document Library"]);
        doclib = (SPDocumentLibrary)web.Lists[doclibid];
    }
    SPFolder formsFolder = doclib.RootFolder.SubFolders["Forms"];
    SPFile template = null;
    foreach (SPFile file in formsFolder.Files)
    {
        if (file.Name == templateName)
        {
            template = file;
            break;
        }
    }
    if (template == null)
    {
        byte[] doc = System.IO.File.ReadAllBytes(string.Format(@"c:\{0}", templateName));
        template = formsFolder.Files.Add(templateName, doc);
    }
    string templateUrl = string.Format("{0}/Forms/{1}", libraryName, templateName);
    if (doclib.DocumentTemplateUrl != templateUrl)
    {
        doclib.DocumentTemplateUrl = templateUrl;
        doclib.Update();
    }

The document (MyTemplate.doc) is uploaded to the ‘Forms’ folder that is available in every document library. After running this code your Forms folder and advanced options will look like this:

    Template1

    Template2

Example 2 – Template for a content type

The second example basically does the same thing as the first example, but now for a content type. This sample requires a content type called ‘Proposal’ to be present in your site before it will run.

    string contentTypeName = "Proposal";
    string proposalTemplateName = "MyProposal.doc";
    SPContentType proposal = web.ContentTypes[contentTypeName];
    SPFolder cts = web.Folders["_cts"];
    SPFolder proposalFolder = cts.SubFolders[contentTypeName];
    SPFile proposalTemplate = null;
    foreach (SPFile file in proposalFolder.Files)
    {
        if (file.Name==proposalTemplateName)
        {
            proposalTemplate = file;
            break;
        }
    }
    if (proposalTemplate == null)
    {
        byte[] proposalDoc = System.IO.File.ReadAllBytes(string.Format(@"c:\{0}", proposalTemplateName));
        proposalTemplate = proposalFolder.Files.Add(proposalTemplateName, proposalDoc);
    }
    if (proposal.DocumentTemplate != proposalTemplateName)
    {
        proposal.DocumentTemplate = proposalTemplateName;
        proposal.Update();
    }

This code will upload a template document for the proposal content type and then set the DocumentTemplate property of the SPContentType. When you use the SharePoint interface to upload a new template document, this document is uploaded to a subfolder called ‘_cts’. All content types in a SPWeb have their own subfolder in _cts. Our ‘’MyProposal.doc’ is therefore uploaded to the folder ‘/_cts/proposal’. My content type is in the root site, so therefore this _cts folder is a subfolder of the rootsite. If you create your content type in a site deeper down in the hierarchy it will be a sub folder of this sub site. The screenshot below shows SharePoint Designer showing this folder. The screenshot below that shows the advanced settings for our content type. Please note the difference between the document library and the content type. The content type expects just the filename, the document library expects you the set the full relative url to the template.

     Template3

    Template4

 And after writing this post I should now return to finishing my article on custom policies for the Dutch .NET Magazine….

Comments

No Comments

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts