Exercise 4: Deploying Document Templates in a WSP
In this exercise you will be taking an add-in and a document template that have already been created and deploying them in a WSP. You’ll learn how to add a simple add-in to a Module element and deploy it to a SharePoint Server. Then you’ll learn how to deploy a document template to a new Document Library and update its deploy location using a feature receiver.
Task 1 – Create a new SharePoint 2010 project
In this task, you will publish the InvoiceTemplate add-in and create a new SharePoint 2010 project to deploy the add-in.
- Open Visual Studio 2010 and open the starter solution at %Office2010DeveloperTrainingKitPath%\Labs\Deployment\Source\[language]\Starter\SolutionDeployment\SolutionDeployment.sln
- Publish the InvoiceTemplate project
- Right click InvoiceTemplate in the Solution Explorer and click Properties
- In the Publish tab, verify the version is 1.0.0.0
- Right click InvoiceTemplate in the Solution Explorer and click Publish
- In the first dialog page leave the publish location as publish\ and click Next
- In the next page select From a CD-ROM or DVD-ROM and click Next
- In the last page verify the information is correct and click Finish
- Add a new SharePoint 2010 project to the solution
- Click File -> Add -> New Project
- In the Visual C#\Visual Basic -> SharePoint -> 2010 template, choose the EmptySharePointProject option
- Enter a name of InvoiceApplication and click OK
- Enter https://intranet.contoso.com/sites/Deployment as the site
- Choose Deploy as a farm solution and click Finish
Task 2 – Create the SharePoint objects needed to deploy the Invoice Document
In this task, you will create the content type and module used to deploy the add-in.
- Create a new Content Type named InvoiceDocument to reference the document template
- Right click the InvoiceApplication project in the Solution Explorer and click Add -> New Item
- Select the Content Type item and create a new item named InvoiceDocument
- In the dialog choose a base type of Document and click Finish
- In the Elements.xml file that opened for you, change the Name to InvoiceDocument
After the FieldRefs nodes, add the following DocumentTemplate node.
<DocumentTemplate TargetName="Addin/InvoiceTemplate.dotx" />
- Save the changes to the content type’s Elements.xml file
- Embed a new Module Item named Addin within the InvoiceDocument content type
- Right click the InvoiceDocument content type in the Solution Explorer and click Add -> New Item
- In the dialog select the Module template
- Name it Addin and click Add to create the new module
- Delete the Sample.txt file that was added automatically
Open the Elements.xml file in the new Addin item and modify the Module element to match the following example
<Module Name="Addin" Url="_cts/InvoiceDocument" RootWebOnly ="TRUE">
The Url attribute determines where it the site collection the files are deployed. The RootWebOnly attribute determines that this module will only be deployed at the site collection level.
- Add the published InvoiceDocument add-in files to the new Addin module
- Right click Addin and click Add -> Existing Item
- In the dialog navigate to %Office2010DeveloperTrainingKitPath%\Labs\Deployment\Source\[language]\Starter\SolutionDeployment\InvoiceTemplate\publish
- Select the InvoiceTemplate.dotx and InvoiceTemplate.vsto files and click Add
- Right click Addin and select Add -> New Folder and rename it Application Files
- Right click Application Files and select Add -> New Folder and rename it InvoiceTemplate_1_0_0_0
- Right click InvoiceTemplate_1_0_0_0 and click Add -> Existing Item
- In the dialog navigate to %Office2010DeveloperTrainingKitPath%\Labs\Deployment\ Source\[language]\Starter\SolutionDeployment\InvoiceTemplate\publish\Application Files\InvoiceTemplate_1_0_0_0
- Select all files in the folder and click Add
- Create a new list template based on the Invoice Document content type
- Right click the InvoiceApplication project and click Add -> New Item
- In the dialog select the List Definition from Content Type template
- Give it a name of InvoiceDocumentLibrary and click Add
- In the wizard enter a display name of Invoice Document Library
- Verify the content type selected is Invoice Document
- Uncheck the Add a list instance for this list definition and click Finish
- Update the list template to relate the content type to the folder containing the addin
- Open the Schema.xml file in the InvoiceDocumentLibrary item
In the List element at the top of the document, add the EnableContentTypes attribute to turn on content types
EnableContentTypes=”TRUE”
Inside the ContentType node add the following Folder element
<ContentType ID="0x01010070158d26ab254e4288119c31012da458" ...>
FakePre-0a25d4a1b2d54461b369a20180f7ea00-b0ec4d8b47fe433196b670e5d5153f61FakePre-1b1f3e23d1e142deb803e66ccd3db704-762971927c1449e0a47882fc38e221d3 <Folder TargetName="InvoiceDocument" />FakePre-187b77a747374cea862efa7fc71b14ad-b21a1fd36fc1458eb5b99e77a6cfc7be
Only add the Folder element, do not modify the ID attribute of the ContentType element
- Save your changes to the Schema.xml file
Task 3 – Create the Feature that will deploy the InvoiceDocument Add-in
In this task, you will create the feature that will deploy the SharePoint items you just created.
- Update the feature’s name and verify it contains all the necessary deployment items
- In the Solution Explorer, locate Feature1 in the Features folder and rename it to SiteFeature
- Double click SiteFeature in the Solution Explorer to open the designer
- Update the title to InvoiceApplicationSite Feature
- Change the scope of the feature to Site
- Create a feature receiver that will update the addin path stored in the template any time the feature is activated
- Right click SiteFeature in the Solution Explorer and click Add Event Receiver
- Right click InvoiceApplication and select Add Reference
- Add a reference to Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0
- Add the following using statements to the new feature receiver
using System.IO; using Microsoft.VisualStudio.Tools.Applications;
Imports System.IO Imports Microsoft.VisualStudio.Tools.Applications
- Uncomment the FeatureActivated method
Insert the following code to fine the SPFile object storing the document template
SPSite site = properties.Feature.Parent as SPSite; SPContentType contentType = site.RootWeb.ContentTypes["InvoiceDocument"]; SPFile file = site.RootWeb.GetFile(contentType.DocumentTemplateUrl);
Dim site As SPSite = TryCast(properties.Feature.Parent, SPSite) Dim contentType As SPContentType = site.RootWeb.ContentTypes("InvoiceDocument") Dim file As SPFile = site.RootWeb.GetFile(contentType.DocumentTemplateUrl)
Insert the following code to write the contents of the document template to a temporary file.
string tempFile = Path.GetTempFileName() + ".dotx"; File.WriteAllBytes(tempFile, file.OpenBinary());
Dim tempFile As String = Path.GetTempFileName() & ".dotx" File.WriteAllBytes(tempFile, file.OpenBinary())
Use ServerDocument to open the file and modify the DeploymentManifestUrl
try { using (ServerDocument document = new ServerDocument(tempFile)) { string url = site.RootWeb.Url.EndsWith("/") ? site.RootWeb.Url : site.RootWeb.Url + "/"; document.DeploymentManifestUrl = new Uri(url + contentType.ResourceFolder.Url + "/Addin/InvoiceTemplate.vsto"); document.Save(); } using (FileStream tempFileStream = File.Open(tempFile, FileMode.Open)) file.SaveBinary(tempFileStream); }
Try Using document As New ServerDocument(tempFile) Dim url As String = If(site.RootWeb.Url.EndsWith("/"), site.RootWeb.Url, site.RootWeb.Url & "/") document.DeploymentManifestUrl = New Uri(url & contentType.ResourceFolder.Url & "/Addin/InvoiceTemplate.vsto") document.Save() End Using Using tempFileStream As FileStream = File.Open(tempFile, FileMode.Open) file.SaveBinary(tempFileStream) End Using End Try
Clean up by deleting the temporary file
finally { File.Delete(tempFile); }
Finally File.Delete(tempFile) End Try
- Deploy the completed SharePoint application
- Right click InvoiceApplication in the Solution Explorer and click Deploy
Task 4 – Deploy and test the new Invoice Document template
In this task, you will finish the deployment of the new add-in by registering the certificates and adding the appropriate paths to Word’s trusted locations.
- Register the certificates used to sign the add-in manifest files
- Run a new Visual Studio Command Prompt (2010) in Programs -> Microsoft Visual Studio 2010 -> Visual Studio Tools
- Change the directory to %Office2010DeveloperTrainingKitPath%\Labs\Deployment\Source\[language]\Starter\SolutionDeployment\InvoiceTemplate
Execute the following commands to register the certificates
certmgr –add –c InvoiceTemplate_TemporaryKey.pfx –s Root certmgr –add –c InvoiceTemplate_TemporaryKey.pfx –s TrustedPublisher
- Start Word2010 and add the new site to the trusted location
- Open Office Word 2010
- Click the File tab for Backstage and then click Options
- In the options dialog, choose Trust Center and click Trust Center Settings
- Select Trusted Locations and verify Allow Trusted Locations on my network is checked
- Click Add new location
- Enter a path of https://intranet.contoso.com/sites/Deployment
- Check the Subfolders of this location are also trusted checkbox
- Click OK to close all of the dialog
- Close Word 2010
- Create a new list using the Invoice Document Library template and test the new document button
- Open Internet Explorer and navigate to https://intranet.contoso.com/sites/Deployment
- Click the All Site Content link on the Navigation bar
- Click the Create button then click the Invoice Document Library link
- Enter a name of Invoices and click Create
- In the new document library select the Documents ribbon and click New Document
- Verify Word 2010 opens and downloads the addin related to the document