Walkthrough: Deploying a Document and an Assembly to Different Local Folders (2003 System)
The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.
Microsoft Office version
For more information, see Features Available by Application and Project Type.
This walkthrough demonstrates how to deploy a document-level customization so that the document and the assembly are located in different folders on the end user computer. This type of deployment is known as the local/local deployment model.
For more information about options for deploying your solution, see Deployment Models (2003 System). For more information about document-level customizations, see Architecture of Document-Level Customizations.
This walkthrough illustrates the following tasks:
Editing the embedded application manifest in a document to point to the assembly.
Granting full trust to a Visual Studio Tools for Office solution assembly that is located in a local folder.
You need the following components to complete this walkthrough:
Visual Studio Tools for Office (an optional component of Visual Studio 2008 Professional and Visual Studio Team System).
Microsoft Office Word 2003 or Microsoft Office Excel 2003.
This walkthrough assumes that you are deploying a Word solution. If you want to perform the walkthrough with an Excel solution, replace the name of the Word project with the name of your Excel project in all code examples.
Administrator privileges on the development computer, so you can set the security policy.
The first step is to create a basic Visual Studio Tools for Office solution to deploy. If you already have a solution that you want to deploy, you can skip this section and proceed to "Deploying the Solution".
To create a solution to deploy
Create a Word Document project with the name WordDeployment, using the project template for Office 2003.
In the wizard, select Create a new document. For more information, see How to: Create Visual Studio Tools for Office Projects.
In Solution Explorer, right-click the ThisDocument code file, and then click View Code.
Add the following code in the ThisDocument_Startup event handler. This code displays a message when the document opens, which will make it easy to verify whether the solution has been deployed successfully.
For more information about the Startup event, see Visual Studio Tools for Office Project Events.
Press F5 to build and run the project. Verify that the message appears.
Now you can deploy the document and assembly to different folders on the development computer.
To deploy the solution
Create folders named TestDeployDocument and TestDeployAssembly at the root of the Windows system drive (%SystemDrive%). For example, if your system drive is C, the new folders would be C:\TestDeployDocument and C:\TestDeployAssembly.
Copy the document from the build output folder (typically project folder\bin\debug or project folder\bin\release) to the %SystemDrive%\TestDeployDocument folder, and copy the assembly from the build output folder to the %SystemDrive%\TestDeployAssembly folder.
Open the document in the %SystemDrive%\TestDeployDocument folder. You will see an error message that states that the customization assembly could not be found or could not be loaded.
When you build your solution, the document assumes that the assembly is in the same folder as the document. The location of the assembly is stored in an application manifest that is embedded in the document. If you deploy the document and assembly to different folders, then you must edit the embedded application manifest to specify the new assembly path. The next section explains this step.
Close the solution in Visual Studio.
Ensure that the document is not open in an instance of Word or the project designer. If the document is open, the attempt to access the document in the next step using ServerDocument will fail.
You can edit the embedded application manifest in the document to point to the new location of the customization assembly by using the ServerDocument class. The code that uses ServerDocument to edit the embedded application manifest must be outside of the Visual Studio Tools for Office project assembly that is associated with document you are deploying.
To edit the embedded application manifest
Create a new Console Application project. For more information, see How to: Create Solutions and Projects.
Add a reference to the Microsoft.VisualStudio.Tools.Applications.Runtime.dll assembly to the new project.
Add the following using statement (C#) or Imports statement (Visual Basic) to the top of the Program.cs or Module1.vb code file.
Add the following code in the Main method to update the embedded application manifest. Replace full document path with the full path of the document in its deployment location on the development computer—for example, C:\TestDeployDocument\WordDeployment.doc. Replace full assembly path with the full path of the assembly in the folder it will be deployed to—for example, C:\TestDeployAssembly\WordDeployment.dll.
Dim sd As ServerDocument = Nothing Try sd = New ServerDocument("full document path") sd.AppManifest.Dependency.AssemblyPath = _ "full assembly path" sd.Save() Finally If Not sd Is Nothing Then sd.Close() End If End Try
Press F5 to build and run the project. The console application updates the assembly path in the embedded application manifest and then closes.
Close the Console Application project.
Open the document in the %SystemDrive%\TestDeployDocument folder. You will see an error message that states that the current .NET security policy does not permit the customization to run. This is because you have not granted full trust to the assembly yet.
All instances of Word must be closed, or the security policy changes will not take effect.
To enable the solution to run, you must grant full trust to the assembly in your .NET Framework 2.0 security policy. This walkthrough uses the Code Access Security Policy tool (Caspol.exe) to grant full trust to the assembly. For more information about using Caspol.exe, see Code Access Security Policy Tool (Caspol.exe) and Configuring Security Policy Using the Code Access Security Policy Tool (Caspol.exe).
These are basic steps for setting a security policy based on URL evidence for the purpose of completing this walkthrough. Do not use these steps to grant trust to assemblies in a real-world solution if you are not certain that the location is safe and secure. You should also base the security of a real-world solution on more evidence than the URL of the assembly. For more information, see Security Requirements to Run Office Solutions (2003 System).
To grant full trust to the assembly
At the command prompt, type the following command to create a new code group that grants full trust to the assembly.
%windir%\Microsoft.NET\Framework\v2.0.50727\caspol -u -ag All_Code -url "full assembly path" FullTrust -n "Test_Deployment"
Replace full assembly path with the full path of the assembly on the development computer—for example, C:\TestDeployAssembly\WordDeployment.dll.
The -n parameter specifies a name for the new code group. This parameter is not required, but specifying a label makes it easier to later identify and remove the new code group after you complete this walkthrough.
Type yes when prompted to confirm that you want to perform the operation, and press ENTER.
Open the document in the %SystemDrive%\TestDeployDocument folder and verify that the message appears.
You can also deploy the document and the assembly to the same local folder, or you can deploy the document and assembly to a network folder. For more information, see the following walkthroughs: