One of the best concepts in SharePoint are them hierarchical definition of content types that has been first introduces in SharePoint 2007. New content types could be defined by small variations made easily by developer or end users. All document based templates could be assigned with a specific document template that would be used in libraries. If someone has predefined Document Templates that could be assigned with the content type, they could be uploaded or deployed.
Xml Definition of content type with document template
The solution to deploy document templates and content type together in a solution consists of two parts:
- The content type definition
- A module for the template file
The first part of this solution is just a xml content type definition with the parent content type set to “Document”. A DocumentTemplate inside the content type definition was specified, which points to the defined template file. The content type looks as follows:
<!-- ContentType: Document (0x0101) --> <ContentType ID="0x01010064db413f63314bbbb9e4cd59d461beff" Name="MyNewContentType" Group="Developent Content Type" Description="My Content Type" Inherits="TRUE" Version="0"> <DocumentTemplate TargetName="MyNewContentTypeTemplate.docx"> <DocumentTemplate> <FieldRefs> </FieldRefs> <ContentType>
In SharePoint every content type has its own folder where templates will be stored, no matter if the content type is deployed through a solution or a template file has been uploaded via web the browser. These folders are located in the root of the site collection beneath the _cts folder. This folder can be found in the SharePoint Designer. Once the root of the site collection has been opened in SharePoint Designer and on the left side of the navigation “All Files” has been selected the _cts folder gets visible.
The second part of the solution is a module that has word document which will be deployed to the folder of the content type.
<Module Name="MyNewContentType"> <File Path="MyNewContentType\MyNewContentTypeTemplate.docx" Url="_cts/MyNewContentType/MyNewContentTypeTemplate.docx" Type="GhostableInLibrary" /> </Module>
On solution deployment, first of all the content type is created that reference the template file. Later the module will make sure that the template file could be located in the correct content type folder and make the template file accessible.
After the deployment the content type and the template can be accessed through SharePoint and used in any desired document library.
SharePoint Farm Solution, Sandboxed or SharePoint Online aka Office 365
This solution works in all the common deployment scenarios no matter where it should be deployed. This is because no custom code is required. The complete code for this only uses a couple of lines xml definition and looks like this:
<Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <!-- Parent ContentType: Document (0x0101) --> <ContentType ID="0x01010064db413f63314bbbb9e4cd59d461beff" Name="MyNewContentType" Group="Developent Content Type" Description="My Content Type" Inherits="TRUE" Version="0"> <DocumentTemplate TargetName="MyNewContentTypeTemplate.docx"> </DocumentTemplate> <FieldRefs> </FieldRefs> </ContentType> <Module Name="MyNewContentType"> <File Path="MyNewContentType\MyNewContentTypeTemplate.docx" Url="_cts/MyNewContentType/MyNewContentTypeTemplate.docx" Type="GhostableInLibrary" /> </Module> </Elements>
For those who like to try the solution can download the VS Studio Solution or the WSP File.
Thanks for the vs studio solution!
Not true, this does not work with Sandbox solution
I worked under SharePoint 2010 and even in Sandboxed Solutions. I used it multiple time back in the old days. Anyway Sandbox Solution nowadays should be avoided.