How to: Deploy a Document Template in a Sandboxed Solution
For a practical example of the deployment of files and documents to the sandbox environment, see the Sandbox Proxy Reference Implementation.
Summary of Steps
This how-to topic includes the following steps:
- Step 1: Create and Configure a Module. In this step, you create a feature manifest file that contains a Module element. This specifies which files to deploy and where to deploy them.
- Step 2: Add the Module to a Feature. In this step, you create a feature and add your feature manifest file to the feature. This enables you to deploy the module to the SharePoint environment.
|This how-to topic assumes that you have used the Microsoft Visual Studio® 2010 development system and one of the SharePoint 2010 templates to create a project.|
Step 1: Create and Configure a Module
This procedure creates a feature manifest that contains a Module element. The module is configured to add files to the content database, which enables you to deploy it within a sandboxed solution.
To create a Module element
- In Solution Explorer, right-click the project node, point to Add, and then click New Item.
- In the Add New Item dialog box, in the Installed Templates pane, expand SharePoint, and then click 2010.
- Click Module, type a name for the module in the Name text box, and then click Add. This example uses the name Templates for the module.
In Solution Explorer, notice that Visual Studio has added a Templates node to represent your module.
- Expand the Templates node, and then open the feature manifest file (Elements.xml). By default, the feature manifest includes a Module element with a placeholder File child element, as shown in the following XML.
- In the Elements.xml file, delete the placeholder File element. You can also delete the Sample.txt file from Solution Explorer.
- In Solution Explorer, right-click the Templates node, point to Add, and then click Existing Item.
- Browse to the file or files you want to deploy, and then click Add.
- Notice that Visual Studio adds a File element for each file that you add, as shown in the following XML. This example adds an Excel template named Estimate.xltx.
- In the Module element, add a Url attribute to specify the destination for the files. In this example, a document template for a content type is being deployed, so a value of _cts/Estimate is specified.
Note: In SharePoint 2010, like in earlier versions of SharePoint, content types and document templates are stored as subfolders in the _cts virtual directory.
- Make the following changes to the File element:
- Leave the Path attribute value as Templates\estimate.xltx. This tells the feature where to find the file in your Visual Studio project.
- Change the Url attribute value to estimate.xltx. This specifies the virtual path to the file, within the virtual directory specified by the Module element.
- Add a Type="Ghostable" attribute value. This indicates that the file will be stored in the content database.
Note: Visual Studio 2010 does not always automatically pick up the feature manifest schema. If you see schema errors or you lack IntelliSense support when you edit a feature manifest, check the properties of the XML document in the Properties window. The Schemas property should be set to 14\TEMPLATE\XML\wss.xsd.
- The feature manifest should resemble the following.
<Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <Module Name="Templates" Url="_cts/Estimate"> <File Path="Templates\estimate.xltx" Url="estimate.xltx" Type="Ghostable" /> </Module> </Elements>
Note: For sandboxed deployments, you can use Type attribute values of Ghostable or GhostableInLibrary. Use GhostableInLibrary if your deployment target is a document library and you want SharePoint to create a parent list item for your file.
Step 2: Add the Module to a Feature
This procedure adds the module to a feature, which provides the mechanism to deploy your files to the SharePoint environment.
To add a module to a feature
- In Solution Explorer, right-click the Features node, and then click Add Feature.
Note: Visual Studio 2010 may have already added an empty feature when you added other SharePoint components. In this case, you can either rename the empty feature or delete it and create a new one.
- In Solution Explorer, right-click the new feature node, and then click Rename. This example uses the name TemplatesFeature for the feature.
- If the Feature Designer is not already open, double-click the TemplatesFeature node to open the designer.
- In the Feature Designer, select an appropriate scope. You can use a feature scope value of Web or Site within a sandboxed solution.
- In the Items in the Solution pane, click the Templates module.
- Click the right arrow button to add the module to the feature. This moves the Templates module to the Items in the Feature pane.
- To deploy the feature, right-click the project node in Solution Explorer, and then click Deploy.
Note: This example deploys the document template to the _cts virtual directory. This location is not directly accessible to end users. You can use steps 8–11 to verify that the file was deployed successfully.
- Open your site in SharePoint Designer.
- In the Site Objects pane, click All Files.
- In the main window, click _cts.
- Click the Estimate folder, and verify that the folder contains the file Estimate.xltx.