Automating Solution Package Creation for Windows SharePoint Services by Using MSBuild

Summary:  Learn how to use MSBuild to automatically generate solution package files for Windows SharePoint Services.

Applies to:  Windows SharePoint Services 3.0, Microsoft Office SharePoint Server 2007, Microsoft Visual Studio 2005

  Andrew Connell, Critical Path Training, LLC

April 2008

Windows SharePoint Services 3.0 includes a new method of deploying custom code and functionality to SharePoint sites that acts as the internal installer framework for Windows SharePoint Services. A Windows SharePoint Services solution package (*.wsp) is a CAB file that includes all custom code and files that should be deployed to a SharePoint site.

Creating Windows SharePoint Services solution package files is typically a manual task. To create a solution package file, you must use the Makecab.exe console application that is included in the Microsoft Cabinet Software Development Kit (SDK). Makecab.exe requires a parameter that specifies the Diamond Directive File (.ddf) that contains a list of all the files to include in the package.

After creating and compiling any custom code or files that will be included in the solution package file, you must create the .ddf file and execute Makecab.exe to create the solution package file. Although you can automate this by using a custom post-build event in Microsoft Visual Studio or by using a DOS batch file, another approach is to automate the solution package creation process by using MSBuild.

Configuring Visual Studio projects by using MSBuild to automatically generate Windows SharePoint Services solution package files on each project build action involves two steps. not including the creation of the .ddf file, which is the same as creating a solution package file manually by using Makecab.exe.

Creating the MSBuild Targets File

First, you must create a custom targets file that calls Makecab.exe. The following targets file (BuildSharePointPackage.Targets) creates a new variable named MakeCabPath, which is used in a new MSBuild target BuildSharePointPackage. The target BuildSharePointPackage calls Makecab.exe and passes in the path to the .ddf file (relative to the root of the project directory), the name of the solution package file to generate (which is set to be the same name as the project), and the location to save the new solution package file (a subdirectory in the root of the Visual Studio project /wsp that includes the current configuration, such as /wsp/debug).

Configuring Visual Studio to Call the Target BuildSharePointPackage on Each Build

The next step is to configure the Visual Studio project file to call the custom MSBuild target BuildSharePointPackage each time the project is built. This step involves editing the project file (*.csproj or *.vbproj), which you can do in Microsoft Notepad or in Visual Studio.

To edit the project file in Visual Studio

  1. Right-click the project in Solution Explorer, and then select Unload Project.

    If the solution is not displayed in Solution Explorer:

    1. On the Tools menu, click Options.

    2. Select the Projects and Solutions node, and then select the Always show solution check box.

  2. With the project file unloaded, right-click the project again in Solution Explorer, and then click Edit project name.csproj.

  3. Scroll down to the <Import> section just before a commented block. Add an <Import> node to include the custom targets file to the project. Modify the target AfterBuild to call the BuildSharePointPackage target defined in the BuildSharePointPackage.targets file as shown in the following code.

  4. Reload the project by right-clicking it in Solution Explorer and clicking Reload Project

From now on, when the project is built, it automatically calls the BuildSharePointPackage target that creates the Windows SharePoint Services solution package file automatically.

Windows SharePoint Services solution packages are the preferred way to deploy your custom code and files to SharePoint sites. This deployment method is preferred over manually copying files to SharePoint servers for several reasons. First, in a load-balanced environment where the server farm contains multiple front-end Web servers, Windows SharePoint Services automatically deploys the solution package contents to each front-end Web server in the farm. If the deployed files ever have to be modified, you should update them in the solution package, use the Stsadm.exe operation UPGRADESOLUTION to replace the solution package, and, if previously deployed, redeploy the files. In addition, when new front-end Web servers are added to the farm, all deployed solutions are deployed to the new front-end Web server automatically.

Watch the Video

Length: 12:32 | Size: 11.7 MB | Type: WMV