Starting in Visual Studio 2010 Beta 1, you can perform additional tasks after an Office solution is installed known as a post-deployment action. For example, you can copy a customized Office document and create registry keys on the end user computer. You can compare these to Windows Installer custom actions.
The Visual Studio Tools for Office runtime supports the ability to perform post-deployment actions after an Office solution is installed. However, Visual Studio does not generate the necessary sections of the ClickOnce application and deployment manifests to perform these actions. To run these post-deployment actions, you must modify the application and deployment manifests. This process is demonstrated in the Walkthrough: Copying a Document to the End User Computer after a ClickOnce Installation topic in the MSDN Library.
The walkthrough uses an Excel workbook project, but this example uses a Word document project for demonstration purposes.
Creating a New Project
1. Create a new Word document-level project that targets the .NET Framework 3.5 named FabrikamWordDocument.
2. Add code to the FabrikamWordDocument project.
3. Build the project.
Creating a Class Library Project that Defines the Post-Deployment Action
1. Create a class library called FileCopyPDA.
2. Add references to Microsoft.VisualStudio.Tools.Applications.Runtime.v10.0 and Microsoft.VisualStudio.ToolsApplications.ServerDocument.v10.0.
3. Rename the class to FileCopyPDA.
4. Replace the FileCopyPDA class with the following code that completes the following tasks:
Copies the Word document file to the user’s desktop if the solution is installed or updated.
Changes the _AssemblyLocation property from a relative path to a fully qualified path for the deployment manifest. This is done using the AddCustomization and RemoveCustomization methods.
Deletes the file if the solution is uninstalled.
Public Class FileCopyPDA Implements IAddInPostDeploymentAction Sub Execute(ByVal args As AddInPostDeploymentActionArgs) Implements IAddInPostDeploymentAction.Execute Dim dataDirectory As String = "Data\FabrikamWordDocument.docx" Dim file As String = "FabrikamWordDocument.docx" Dim sourcePath As String = args.AddInPath Dim deploymentManifestUri As Uri = args.ManifestLocation Dim destPath As String = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) Dim sourceFile As String = System.IO.Path.Combine(sourcePath, dataDirectory) Dim destFile As String = System.IO.Path.Combine(destPath, file) Select Case args.InstallationStatus Case AddInInstallationStatus.InitialInstall, AddInInstallationStatus.Update File.Copy(sourceFile, destFile) ServerDocument.RemoveCustomization(destFile) ServerDocument.AddCustomization(destFile, deploymentManifestUri) Exit Select Case AddInInstallationStatus.Uninstall If File.Exists(destFile) Then File.Delete(destFile) End If Exit Select End Select End Sub End Class
6. Add a reference to FileCopyPDA.dll in the FabrikamWordDocument project.
7. Create a directory called “Data” in the FabrikamWordDocument project.
8. Add the FabrikamWordDocument.docx file to the Data folder.
9. In Properties window for the FabrikamWordDocument.docx file, change the Build Action property to Content, and change the Copy to Output Directory property to Copy if newer.
10. Publish the FabrikamWordDocument project. This example uses the c:\publish directory.
Modifying the Application Manifest
1. Open the C:\publish\Application Files\FabrikamWordDocument_1_0_0_0\FabrikamWordDocument.dll.manifest file in the XML Editor in Visual Studio.
2. Add the following XML code after the </vstav3:update> element to run the FileCopy post-deployment action.
<vstav3:postActions> <vstav3:postAction> <vstav3:entryPoint class="FileCopyPDA.FileCopyPDA"> <assemblyIdentity name="FileCopyPDA" version="184.108.40.206" language="neutral" processorArchitecture="msil" /> </vstav3:entryPoint> <vstav3:postActionData> </vstav3:postActionData> </vstav3:postAction> </vstav3:postActions>
Re-signing the Manifests
To prove that yes, you really meant to make these changes to the application manifest, you must re-sign the application manifest with a certificate. Then, re-sign and update the deployment manifest to point to the new application manifest.
1. Copy your certificate to the C:\publish\Application Files\FabrikamWordDocument_1_0_0_0 directory. In this example, I’m reusing the temporary certificate created by Visual Studio because I’ll be installing this to a test computer. For production deployment, we recommend that you use a certificate obtained from a certificate authority.
2. In a Visual Studio command prompt, change to the C:\publish\Application Files\FabrikamWordDocument_1_0_0_0 directory. (You may have to open the command prompt in Administrator mode to re-sign the files in the c:\publish directory.)
3. Re-sign the application manifest with the following command:
mage -sign FabrikamWordDocument.dll.manifest -certfile FabrikamWordDocument_TemporaryKey.pfx
4. Change to the c:\publish directory
5. Re-sign the deployment manifest with the following command (on one line):
mage -update FabrikamWordDocument.vsto
–appmanifest "Application Files\FabrikamWordDocument_1_0_0_0\FabrikamWordDocument.dll.manifest"
-certfile "Application Files\FabrikamWordDocument_1_0_0_0\FabrikamWordDocument_TemporaryKey.pfx"
6. Copy the c:\publish\FabrikamWordDocument.vsto file to the c:\publish\Application Files\FabrikamWordDocument_1_0_0_0 directory.
Testing the Installer
1. Copy the c:\publish directory to a test computer.
2. In the c:\publish directory, run the Setup.exe file or double-click FabrikamWordDocument.vsto.
3. Verify that FabrikamWordDocument appears in the Add/Remove Programs list in Windows XP or Programs and Features in Windows Vista.
4. Verify that the FabrikamWordDocument.docx file appears on the desktop.
5. Open the Word document file to verify that your code is running.
Feel free to leave comments and feedback about this new scenario at the bottom of this article.
Mary Lee, Programming Writer.