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.
You should have completed the previous recipes successfully. Create a word template and name it ProjectProposal.dotx
.
ProjectProposal.dotx
file to this folder. Your project structure should be as follows: 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();
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:
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:
Elements.xml
file underneath it along with the feature to deploy the content type. Sample.txt
and the Elements.xml
file that Visual Studio adds. Your project structure should look like the following: 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>
For the list of built-in content types and their IDs refer to MSDN at: http://msdn.microsoft.com/en-us/library/ms452896.aspx.
3.144.36.141