moopoo.net
Fishing, technology and anything else

Package class library as wsp for SharePoint Deployment

December 15th, 2010 by Matt

Some Visual Studio solutions I’ve come across have class library projects in them. These assemblies have to subsequently be manually copied into the GAC of each SharePoint server. This goes against the standard of using wsp files to deploy assemblies and files into a SharePoint environment. Single servers are easier to manage manually copying assemblies but it’s not a very pleasant way of doing things, move into a multi server environment and you’re in a minefield.
Fortunately there is an easy way to get these assemblies, plus any others you may wish to deploy at the same time, into a wsp. It takes two files and a post build event. Read on..

First up, we need a manifest file. This tells the environment what we would like it to do with the assembly.
Create a new xml file in the root of your project called manifest.xml and add the following code:

<?xml version="1.0" encoding="utf-8" ?>
<Solution xmlns="http://schemas.microsoft.com/sharepoint/"
  SolutionId="<new guid>">
  <Assemblies>
    <Assembly Location="<assembly>.dll" DeploymentTarget="GlobalAssemblyCache"></Assembly>
  </Assemblies>
</Solution>

Replace <new guid> with a generated GUID and <assembly> with the name of your dll file.
The DeploymentTarget attribute above specifies that it will be deployed to the GAC, you can use WebApplication if you want it to be deployed to the bin directory of your Webb Application.

You can also specify that the relevant line can be added to the SafeControls section of your web.config file by adding the following inside the Assembly tag above.

<SafeControls>
        <SafeControl Assembly="<assembly>, Version=1.0.0.0, Culture=neutral, PublicKeyToken=<public key token>" Namespace="<namespace>" TypeName="*" Safe="True" />
      </SafeControls>

Next up, create a ddf file in the root of your project. I tend to name mine wsp.ddf for consistency. This will contain the instructions on what to package into the wsp file. This file should look like the following.

;Define the output directory and CAB file name (with a wsp extension)
.Set DiskDirectory1="bin\Debug"
.Set CabinetNameTemplate="MySolutionName.wsp"
 
;Include the following files in the CAB Root
manifest.xml
bin\Debug\<assembly>.dll

Now all we have left to do is add a post build event to build the wsp for us.
Go to you project properties (right click project)
Under the Build Events tab under Post-build event command line, enter the following:

:: Change directory to the root of the project
cd "$(ProjectDir)"
 
:: Create a WSP CAB
MakeCAB /f "WSP.DDF"

You can change when the post build event runs, to your liking; normally ‘On successful build’.

That’s it when you have a successful build the wsp file will be generated ready for you to deploy, no more manual copying or editing web.config.

Posted in SharePoint

One Response

  1. ganesh

    Very nice article.. finally i got .wsp file at the end.

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.