How To: Package on build in your Visual Studio SharePoint 2010 solution

In Visual Studio 2010 you have cool new features allowing you to build and package and deploy your solution. In most cases, when you’re dealing with a simple set of tasks the separation between build and package and deploy is ok; in more complex scenarios you may want to build and package all at once or maybe copy your resulting WSP files to a root folder so that you can deploy them with a script.

Each SharePoint project you create will come with a project definition file that will outline some of the variables such as solution name and whether you’re in configuration or release mode. You can modify this project configuration file to perform your own build tasks when the solution is built.

Build tasks are a set of commands you want to execute during build and some of them are pretty standard. In fact Visual Studio 2010 comes with a set of SharePoint tasks that you can find here: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\SharePointTools\Microsoft.VisualStudio.SharePoint.targets. Some of those you’d have to create manually in earlier versions of SharePoint, so this new set of targets is really handy.

In this case we’ll look at how we can use out of the box CreatePackage command to create package after the build. As you see the CreatePackage is already defined in Microsoft.VisualStudio.SharePoint.targets here is how we add it as a build task.

Unload your SharePoint project from Visual Studio solution and right click on project and select Edit

The project will open in an editor; scroll down to the very bottom, you will see the SharePoint targets are loaded:

<Import Project=”$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\SharePointTools\Microsoft.VisualStudio.SharePoint.targets” />

Right below is where you define your own build commands. To package you’d call:

<PropertyGroup>
    <BuildDependsOn>$(BuildDependsOn);CreatePackage</BuildDependsOn>
</PropertyGroup>

This means that whatever build targets are already defined, add CreatePackage to the list.

You can add as many of your own or existing targets here. Here is for example how you can copy the output WSP from the bin folder to the main solution directory (where you can run scripts on it):

<PropertyGroup>
    <PostBuildEvent>copy $(TargetDir)$(TargetName).wsp 
$(SolutionDir)$(TargetName).wsp</PostBuildEvent>
  </PropertyGroup>

If your interested in automated solution deployment to production as well as generally refining your SharePoint solution deployment process – you might find chapter 1 of my new SharePoint 2010 book as a great resource.

This should get you started on creating your own extensions. Enjoy!

About the author: Yaroslav Pentsarskyy