Deploying a Document Template file in Content Type in a (Office365) Sandboxed Solution

In SharePoint 2007, we have the choice of using _layouts folder for deploying document templates that are used in content types. That option still exists in SharePoint 2010 for farm based solutions, but the SharePoint Root (14 hive) is not accessible in Sandboxed solutions and hence it is not possible to deploy the document templates in _layouts folder. You will need to include the document template within SharePoint, meaning it will be stored in the content database.

The following TechNet How-To topic discusses how to deploy document template in a sandboxed solution – What it doesn’t tell you is how to handle document template files for content types and how do we handle very special cases, such as document template files deployed to a site collections with managed path (e.g., https://webapp/sites/mysite)

 

The Problem

The Issue

Possible Workaround 1

Possible Workaround 2

The Solution

 

The Problem

I want to associate a document template file in my content type in a sandboxed solution.

How To:

Follow the TechNet How-To topic to include your document template in the solution:

image

Add a content type to your project (Parent Content Type: Document)

In the content type Elements.xml file, include the reference to your document template:

image

Deploy the solution to your SharePoint site – I am deploying to my Office365 site – https://spopg.sharepoint.com/sites/chaks

After the successful deployment, I can find the content type in my site:

image

The Issue

Now that we have our content type with the document template deployed, if you try to navigate to the Advanced Settings page for the content page and just click Ok:

image

SharePoint will throw an error: File doesn’t exist

image

ULS logs (if you are deploying to a local box) will give you detailed error:

Microsoft.SharePoint.SPException: File doesn't exist
at Microsoft.SharePoint.SPContentType.set_DocumentTemplate(String value)
at Microsoft.SharePoint.ApplicationPages.ChangeContentTypeOptionalSettingsPage.UpdateContentType()
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

It is very clear that SharePoint is not able to save the document template as it is not able to find it.

Possible Workaround 1

Prefix / for _cts/Estimate/EstimatesTemplate.xltx – We are now able to successfully update the content type (via Advanced Settings page), but when you associate the content type to a document library and create a new document, like below:

image

The Office client will throw the following error: Microsoft Excel cannot access the file

image

The reason is because prefix / refers to the / managed path, but my site is in a manage path /sites and hence this fails to load the correct template. However, if your site is in the root managed path, this will work.

So, FAIL #1!

Possible Workaround 2

Another common workaround is to add a Folder target to the content type Elements.xml:

image

This breaks in the Advanced Settings page with the following error: Unexpected error

image

So, FAIL #2!

The Solution

Enough of the workarounds and discussions already! Here is what finally worked for me. There is a slight change in the solution structure though:

Add the Templates module under your content type and delete the default files Sample.txt and Elements.xml added:

image

Now add your template file:

image

Edit the content type Elements.xml and add the Module definition just before declaring the content type:

image

Notice we have Url in the following format: _cts/<content type name>

Now we update the DocumentTemplate reference in the content type definition:

image

Notice we just reference the name of the template without the _cts/Project Estimates

It turns out that SharePoint by default looks under _cts/<content type name> folder for document templates if any.

Deploy and test your solution – it should work now - Office client will load the template without any errors!

You can download the sample here - https://bit.ly/jhJXKv – This sample is Office365 ready!

The sample also contains a web template that will create a blank team site, deploy a list definition associated with the content type (with document template file) and create a document library from the list definition.

The sample site template will be available under Sample Templates category in the New Site dialog:

image