Web Parts are compiled as .NET assemblies
and can be reused many times in Windows SharePoint Services and
SharePoint Portal Server. Web Parts are usually deployed using cabinet (.CAB)
files. We will create a cabinet file project in Visual Studio .NET that
will encapsulate the assemblies and metadata for our Web Part. This
cabinet file must then be installed using the stsadm.exe utility that comes with WSS/SPS. To deploy the Web Part we will perform the following steps:
1. | Prepare the Web Part Description
|
2. | Prepare the Web Part Manifest
|
3. | Create a CAB file deployment project
|
4. | Execute the deployment
|
Preparing the Web Part Description (.DWP) File
When deploying a Web Part to SharePoint, several
files should be populated during the deployment process. One of these
files is the Web Part Description (.DWP) file.
1. | In Visual Studio .NET, right-click on the ExtensibleMCMSPageListingWebPart project and choose Add New Item.
|
2. | In the Categories pane on the left, expand Local Project Items and select Data.
|
3. | Select Web Part DWP.
|
4. | Enter the name as NavigationWebPart and click OK.
|
The .DWP file describes our Web Part,
giving the default values for properties and any assemblies that the Web
Part requires. Let’s create this file for our navigation Web Part:
<?xml version="1.0" encoding="utf-8"?>
<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" >
<Title>Extensible MCMS Page Listing Web Part</Title>
<Description>
Displays MCMS Postings and Channels within a specified channel and
transforms the display using XSLT.
</Description>
<Assembly>ExtensibleMCMSPageListingWebPart</Assembly>
<TypeName>ExtensibleMCMSPageListingWebPart.NavigationWebPart</TypeName>
</WebPart>
The values specified in this file allow Windows
SharePoint Services to generate metadata for the Web Part. The meanings
of the elements are given in the table below:
Element Name | Description |
---|
Title | Title that the user will see in the Web Part Library and the default title of the Web Part once it is added in a Web Part Zone. |
Description | Tooltip that will appear over the Web Part title in the Web Part library. |
Assembly | Assembly
name. For this sample we are only using the assembly name but it is
also possible to use the complete Web Part assembly description like so:
AssemblyName (without the .DLL extension), Version=VersionNumber, Culture=Culture, PublicKeyToken=PublicKeyToken |
TypeName | The namespace-qualified class name. |
Preparing the Web Part Manifest File
When
deploying a Web Part as a CAB file, we must create a Web Part manifest
file that gives the CAB file deployment project the details of our Web
Part and our Web Part description files.
1. | In Visual Studio. NET, right-click on the ExtensibleMCMSPageListingWebPart project and choose ‘Add New Item’.
|
2. | In the Categories pane on the left, expand Local Project Items and select Data.
|
3. | Select WPManifest.
|
4. | Enter the name as Manifest and click Open.
|
Enter the following code for this file to correspond to our Web Part:
<?xml version="1.0"?>
<WebPartManifest xmlns="http://schemas.microsoft.com/WebPart/v2/Manifest">
<Assemblies>
<Assembly FileName="ExtensibleMCMSPageListingWebPart.dll">
<ClassResources>
<ClassResource FileName="TestXml.xml"/>
<ClassResource FileName="DefaultXsl.xslt"/>
</ClassResources>
<SafeControls>
<SafeControl Namespace="ExtensibleMCMSPageListingWebPart"
TypeName="*" />
</SafeControls>
</Assembly>
</Assemblies>
<DwpFiles>
<DwpFile FileName="NavigationWebPart.dwp"/>
</DwpFiles>
</WebPartManifest>
Manifest.xml allows us to include
multiple assemblies, resources, and Web Part description files in our
deployment CAB file. The elements are explained below:
Element Name | Description |
---|
Assemblies | Allows us to include multiple assemblies in our cabinet file. |
Assembly | Used to define an assembly by setting the FileName attribute to the DLL name. |
ClassResources | Defines
the resources used by our classes. The paths to the resources must be
relative to the project files. Resources are supporting files such as
images or data files. |
SafeControls | Lists the safe controls that will be used by this Web Part (so they can be added to the Web.config of the SharePoint site on deployment). |
DwpFiles | Used to define the Web Part Description files used by this Web Part. |
Creating the Deployment Project
At this point, we have a completed Web Part
project with the necessary deployment configuration files. Now we need
to create a deployment project to encapsulate the Web Part and other
necessary files into a single cabinet file for installation.
1. | In Visual Studio .NET, right-click on the ExtensibleMCMSPageListingWebPart solution and choose ‘Add New Project’.
|
2. | In the Project Types pane, select Setup and Deployment Projects and in the templates pane, select Cab Project.
|
3. | Enter the name ExtensibleMCMSPageListingWebPartDeployment and click OK.
|
4. | Right-click on the ExtensibleMCMSPageListingWebPartDeployment project and select Add and then Project Output.
|
5. | Select ExtensibleMCMSPageListingWebPart from the project drop-down list.
|
6. | Holding down the Ctrl key, select Primary Output and Content Files and click OK.
|
7. | In the ExtensibleMCMSPageListingWebPart project, right-click on NavigationWebPart.dwp and click Properties.
|
8. | Set the Build Action attribute to Content. This will allow the deployment project to add the file to the package.
|
Your deployment project should now look like this:
Now build your deployment project by right-clicking on the project and selecting Build.
Executing the Deployment
Now that we have our cabinet file, let’s deploy it to our SharePoint server.
If you are developing on a remote workstation, copy the cabinet file to your server before following the steps below.
|
We will deploy it using stsadm.exe, which you will find under <local_drive>\Program Files\ Common Files\Microsoft Shared\Web Server Extensions\60\bin\.
Start a DOS command prompt, and enter the following commands:
cd "C:\Program Files\Common Files\Microsoft Shared\web server extensions\60\BIN\"
STSADM.EXE -o addwppack -filename "C:\Documents and Settings\Administrator\
My Documents\Visual Studio Projects\ExtensibleMCMSPageListingWebPartDeployment\
Debug\ExtensibleMCMSPageListingWebPartDeployment.cab" -globalinstall -force
The Path C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\ may need to change depending on where you have created the project.
If the Web Part was successfully deployed, you should see:
ExtensibleMCMSPageListingWebPartDeployment.cab: Deploying to http://server/.
Operation completed successfully.
When you compile the deployment project in Release mode, the command line will change to:
STSADM.EXE -o addwppack -filename C:\Documents and Settings\Administrator
\My Documents\Visual Studio Projects\ExtensibleMCMSPageListingWebPartDepl
oyment\Release\ExtensibleMCMSPageListingWebPartDeployment.cab" -globalins
tall -force
|
Here’s what happens when this command is executed:
A copy of our Web Part assembly is placed in the bin directory of the SharePoint site.
The Web Part is added to the SafeControls element of the SharePoint site’s Web.config.
A copy of the CAB file is added to the SharePoint configuration database.
Resources used by the assembly are copied into the Wpresources directory for the site. This directory can be found at c:\inetpub\wwwroot\wpresources\.
The .dwp files are copied into the wpcatalog directory of the site. This directory can be found at c:\inetpub\wwwroot\wpresources\.
As we are not installing our Web Part into the
GAC or specifying a Code Access Security Policy, we need to increase the
trust level of the SharePoint site to Full by following these steps:
1. | Open the SharePoint site’s Web.config (typically c:\inetpub\wwwroot\Web.config).
|
2. | Update the trust element’s level attribute to Full.
|
3. | Save Web.config.
|
Right-Click Deployment of Web Part CAB Files
If you are frequently redeploying Web Parts to your server, running stsadm.exe
can be a laborious task. To simplify this, we can create a batch file
and a type mapping that will let us right-click on the Web Part to
install it.
To do this:
1. | Create a batch file called C:\Program Files\Common Files\Microsoft Shared\web server extensions\60\BIN\deploy_webparts.bat.
|
2. | Edit the batch file and enter the following:
STSADM.EXE -o addwppack -filename %1 -globalinstall -force
pause
|
3. | Save and close the batch file
|
4. | Now, open Windows Explorer and choose Tools | Folder Options | File Types from the menu.
|
5. | If no entry exists for CAB files, click New and enter CAB.
|
6. | Select the entry for CAB files and click Advanced.
|
7. | Select New and enter an action name of Install Web Part.
|
8. | Under Application Used to Perform action, enter C:\Program Files\Common Files\ Microsoft Shared\web server extensions\60\BIN\deploy_webparts.bat.
|
9. | Click OK, OK again, and then click Close.
|
10. | Browse to C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\ExtensibleMCMSPageListingWebPartDeployment\Debug\, right-click on ExtensibleMCMSPageListingWebPartDeployment.cab and choose Install Web Part to deploy your Web Part.
|