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.

  1. Open Visual Studio 2010 and open the starter solution at %Office2010DeveloperTrainingKitPath%\Labs\Deployment\Source\[language]\Starter\SolutionDeployment\SolutionDeployment.sln
  2. Publish the InvoiceTemplate project
    1. Right click InvoiceTemplate in the Solution Explorer and click Properties
    2. In the Publish tab, verify the version is 1.0.0.0
    3. Right click InvoiceTemplate in the Solution Explorer and click Publish
    4. In the first dialog page leave the publish location as publish\ and click Next
    5. In the next page select From a CD-ROM or DVD-ROM and click Next
    6. In the last page verify the information is correct and click Finish
  3. Add a new SharePoint 2010 project to the solution
    1. Click File -> Add -> New Project
    2. In the Visual C#\Visual Basic -> SharePoint -> 2010 template, choose the EmptySharePointProject option
    3. Enter a name of InvoiceApplication and click OK
    4. Enter https://intranet.contoso.com/sites/Deployment as the site
    5. 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.

  1. Create a new Content Type named InvoiceDocument to reference the document template
    1. Right click the InvoiceApplication project in the Solution Explorer and click Add -> New Item
    2. Select the Content Type item and create a new item named InvoiceDocument
    3. In the dialog choose a base type of Document and click Finish
    4. In the Elements.xml file that opened for you, change the Name to InvoiceDocument
    5. After the FieldRefs nodes, add the following DocumentTemplate node.

      XML

      <DocumentTemplate TargetName="Addin/InvoiceTemplate.dotx" />

    6. Save the changes to the content type’s Elements.xml file
  2. Embed a new Module Item named Addin within the InvoiceDocument content type
    1. Right click the InvoiceDocument content type in the Solution Explorer and click Add -> New Item
    2. In the dialog select the Module template
    3. Name it Addin and click Add to create the new module
    4. Delete the Sample.txt file that was added automatically
    5. Open the Elements.xml file in the new Addin item and modify the Module element to match the following example

      XML

      <Module Name="Addin" Url="_cts/InvoiceDocument" RootWebOnly ="TRUE">
      Note:
      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.

  3. Add the published InvoiceDocument add-in files to the new Addin module
    1. Right click Addin and click Add -> Existing Item
    2. In the dialog navigate to %Office2010DeveloperTrainingKitPath%\Labs\Deployment\Source\[language]\Starter\SolutionDeployment\InvoiceTemplate\publish
    3. Select the InvoiceTemplate.dotx and InvoiceTemplate.vsto files and click Add
    4. Right click Addin and select Add -> New Folder and rename it Application Files
    5. Right click Application Files and select Add -> New Folder and rename it InvoiceTemplate_1_0_0_0
    6. Right click InvoiceTemplate_1_0_0_0 and click Add -> Existing Item
    7. In the dialog navigate to %Office2010DeveloperTrainingKitPath%\Labs\Deployment\ Source\[language]\Starter\SolutionDeployment\InvoiceTemplate\publish\Application Files\InvoiceTemplate_1_0_0_0
    8. Select all files in the folder and click Add
  4. Create a new list template based on the Invoice Document content type
    1. Right click the InvoiceApplication project and click Add -> New Item
    2. In the dialog select the List Definition from Content Type template
    3. Give it a name of InvoiceDocumentLibrary and click Add
    4. In the wizard enter a display name of Invoice Document Library
    5. Verify the content type selected is Invoice Document
    6. Uncheck the Add a list instance for this list definition and click Finish
  5. Update the list template to relate the content type to the folder containing the addin
    1. Open the Schema.xml file in the InvoiceDocumentLibrary item
    2. In the List element at the top of the document, add the EnableContentTypes attribute to turn on content types

      XML

      EnableContentTypes=”TRUE”

    3. Inside the ContentType node add the following Folder element

      XML

      <ContentType ID="0x01010070158d26ab254e4288119c31012da458" ...>
      FakePre-0a25d4a1b2d54461b369a20180f7ea00-b0ec4d8b47fe433196b670e5d5153f61FakePre-1b1f3e23d1e142deb803e66ccd3db704-762971927c1449e0a47882fc38e221d3 <Folder TargetName="InvoiceDocument" />FakePre-187b77a747374cea862efa7fc71b14ad-b21a1fd36fc1458eb5b99e77a6cfc7be
      Note:
       Only add the Folder element, do not modify the ID attribute of the ContentType element

    4. 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.

  1. Update the feature’s name and verify it contains all the necessary deployment items
    1. In the Solution Explorer, locate Feature1 in the Features folder and rename it to SiteFeature
    2. Double click SiteFeature in the Solution Explorer to open the designer
    3. Update the title to InvoiceApplicationSite Feature
    4. Change the scope of the feature to Site
  2. Create a feature receiver that will update the addin path stored in the template any time the feature is activated
    1. Right click SiteFeature in the Solution Explorer and click Add Event Receiver
    2. Right click InvoiceApplication and select Add Reference
    3. Add a reference to Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0
    4. Add the following using statements to the new feature receiver

      C#

      using System.IO; using Microsoft.VisualStudio.Tools.Applications;

      Visual Basic

      Imports System.IO Imports Microsoft.VisualStudio.Tools.Applications
    5. Uncomment the FeatureActivated method
    6. Insert the following code to fine the SPFile object storing the document template

      C#

      SPSite site = properties.Feature.Parent as SPSite; SPContentType contentType = site.RootWeb.ContentTypes["InvoiceDocument"]; SPFile file = site.RootWeb.GetFile(contentType.DocumentTemplateUrl);

      Visual Basic

      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)

    7. Insert the following code to write the contents of the document template to a temporary file.

      C#

      string tempFile = Path.GetTempFileName() + ".dotx"; File.WriteAllBytes(tempFile, file.OpenBinary());

      Visual Basic

      Dim tempFile As String = Path.GetTempFileName() & ".dotx" File.WriteAllBytes(tempFile, file.OpenBinary())

    8. Use ServerDocument to open the file and modify the DeploymentManifestUrl

      C#

      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); }

      Visual Basic

      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

    9. Clean up by deleting the temporary file

      C#

      finally { File.Delete(tempFile); }

      Visual Basic

      Finally File.Delete(tempFile) End Try

  3. Deploy the completed SharePoint application
    1. 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.

  1. Register the certificates used to sign the add-in manifest files
    1. Run a new Visual Studio Command Prompt (2010) in Programs -> Microsoft Visual Studio 2010 -> Visual Studio Tools
    2. Change the directory to %Office2010DeveloperTrainingKitPath%\Labs\Deployment\Source\[language]\Starter\SolutionDeployment\InvoiceTemplate
    3. Execute the following commands to register the certificates

      CMD

      certmgr –add –c InvoiceTemplate_TemporaryKey.pfx –s Root certmgr –add –c InvoiceTemplate_TemporaryKey.pfx –s TrustedPublisher

  2. Start Word2010 and add the new site to the trusted location
    1. Open Office Word 2010
    2. Click the File tab for Backstage and then click Options
    3. In the options dialog, choose Trust Center and click Trust Center Settings
    4. Select Trusted Locations and verify Allow Trusted Locations on my network is checked
    5. Click Add new location
    6. Enter a path of https://intranet.contoso.com/sites/Deployment
    7. Check the Subfolders of this location are also trusted checkbox
    8. Click OK to close all of the dialog
    9. Close Word 2010
  3. Create a new list using the Invoice Document Library template and test the new document button
    1. Open Internet Explorer and navigate to https://intranet.contoso.com/sites/Deployment
    2. Click the All Site Content link on the Navigation bar
    3. Click the Create button then click the Invoice Document Library link
    4. Enter a name of Invoices and click Create
    5. In the new document library select the Documents ribbon and click New Document
    6. Verify Word 2010 opens and downloads the addin related to the document