Associating a document template with the content type

In the introduction to content types, we specified that a content type can include document templates and/or workflows. In this recipe, we will include a document template for our Project Proposal content type.

Getting ready

You should have completed the previous recipes successfully. Create a word template and name it ProjectProposal.dotx.

How to do it...

  1. If you have closed Visual Studio IDE, launch it as an administrator.
  2. Open the previously created ProjectProposal solution.
  3. Right-click on the project and select Add | SharePoint "Layouts" Mapped Folder as follows:
    How to do it...
  4. This will create a mapped folder to the Layouts folder and add a subfolder with the same name as the project underneath it. In our case it is called ProjectProposal.
  5. Add your ProjectProposal.dotx file to this folder. Your project structure should be as follows:
    How to do it...
  6. Open Feature1.EventReceiver.cs file and add code to associate the document template before the content type update method in feature activating method. Your code should look like the following:
    SPFieldLink frDepartment = new SPFieldLink(fldDepartment);
    ctProjectProposal.FieldLinks.Add(frDepartment);
    //Associate the document template created.
    ctProjectProposal.DocumentTemplate = "/_layouts/ProjectProposal/ProjectProposal.dotx";
    // Commit changes.
    ctProjectProposal.Update();
    
  7. Build and run the project. Navigate to Site Actions | Site Settings | Gallery | Site content types and select Chapter3 Project Proposal. Click on the Advanced settings link and you should see the URL for the document template attached as shown here:
How to do it...

How it works...

Visual Studio always creates a subfolder underneath the mapped folder when we add a reference to the mapped folders. We added our template file to this location. The layouts mapped folder can be referenced as _layouts and hence we provided the URL for the document template from this location.

You can associate this content type to a library as explained in the previous recipes. When you create a new document from this library, the template will be opened from the _layouts folder as shown in the next screenshot:

How it works...

There's more...

In this recipe, we used the _layouts folder to deploy the document template. This will not work with sandboxed solutions. For sandboxed solutions, since you do not have access to the SharePoint root on the file system, you have to deploy the document templates to the content databases. For my example here, I have created an excel template called BudgetProposal.xltx. The content type is called DocumentTemplateContentType. Here is the step-by-step instructions on deploying the document template in a sandboxed solution:

  1. Create a new content type project and make sure you select the Sandboxed solution in the wizard. Inherit the content type from a document content type.
  2. Visual Studio creates the ContentType1 folder and the Elements.xml file underneath it along with the feature to deploy the content type.
  3. Add a new module item to the ContentType1 folder and call it Template.
  4. Delete the default Sample.txt and the Elements.xml file that Visual Studio adds. Your project structure should look like the following:
    There's more...
  5. Edit the Elements.xml file of the content type and add the module information. The attribute's Url provides the path for clients to look for the template associated with the content type. The Type attribute specifies that the template is loaded into the content database. Your content type Elements.xml file should be as follows:
    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <Module Url="_cts/DocumentTemplateContentType" Name="Template">
    <File Url="BudgetProposal.xltx" Name="BudgetProposal.xltx" Type="Ghostable" Path="TemplateBudgetProposal.xltx"/>
    </Module>
    <!-- Parent ContentType: Document (0x0101) -->
    <ContentType ID="0x010100a3c7c0095daf42ac924b1258ab88563c"
    Name="DocumentTemplateContentType"
    Group="Custom Content Types"
    Description="My Content Type"
    Inherits="TRUE"
    Version="0">
    <FieldRefs>
    </FieldRefs>
    <DocumentTemplate TargetName="BudgetProposal.xltx"/>
    </ContentType>
    </Elements>
    

    Note

    For the list of built-in content types and their IDs refer to MSDN at: http://msdn.microsoft.com/en-us/library/ms452896.aspx.

See also

  • Associating a workflow to a content type recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.36.141