Saturday 20 November 2010

Develop SharePoint 2007 webparts using WSPBuilder 2010 Beta in Visual Studio 2010

WSPBuilder is a great tool when developing Webparts for SharePoint - developed by Carsten Keutmann. The tool has been released in a Visual Studio 2010 edition which are compatible with both SharePoint 2007 and SharePoint 2010.

At the moment I work on a project for a customer in SharePoint 2007, but develop my WebParts in Visual Studio 2010. This gave some headaches, so this post is a short How-To onDeveloping SharePoint 2007 webparts using WSPBuilder in Visual Studio 2010.

Install WSPBuilder
Go to WSPBuilder downloads page and grab the latest version of WSPBuilder Extensions 2010. Close Visual Studio and run the MSI file (the installer files contains installation options for all supported configurations)

Create a WSPBuilder project
Open Visual Studio 2010 and click File -> New... -> Project... and choose the WSPBuilder group below Visual C# in Installed Templates. In the left side choose WSPBuilder Project. In the bottom part of the dialog give the solution a name and a location.


The created solution has this structure.


Building a WSP package
To build a WSP package of the project right-click the project and choose WSPBuilder -> Build WSP.


The Output window will show something like


Since there is no webpart file in the project yet, no dll file is created during build.

The important thing in the output is the line Solution Compability: SharePoint 2010. The WSPBuilder extensions actually checks the webpart content and decide if the webpart is compatible with SharePoint 2007 and SharePoint 2010 - or SharePoint 2010 only.

Make the project SharePoint 2007 compatible
First we need to set the Target Framework to .Net 3.5. This actually has nothing to do with the compability - but to ensure the webpart dll stays managed.
Right-click the project and choose Properties. Under the Application pane set Target Framework to .NET Framework 3.5. Save the file. Notice that a web.config file is created in the project. This file has no influence on the project, so just ignore it (but do not delete it).
Next thing to do is delete the Manifest.Config file. This is a new concept in SharePoint 2010 and not supported in SharePoint 2007.
The solution is now SharePoint 2007 compatible and the output of Build WSP is Solution Compability: SharePoint 2007 and SharePoint 2010.


Adding a feature to the project
To add a feature (that is a webpart) to the project right-click the project and choose Add -> New Item.... In the dialog choose the WSPBuilder group below Visual C# in the left pane and Web Part in the right pane. In the bottom part give the webpart a name.


In the next dialog give the feature a Title and a Description. Then choose the Scope the webpart should be deployed to (I usually choose Web) and uncheck Event handler. Click Next.



In the next dialog give the web part a Title and a Description. Then uncheck Removal Code and click Finish.


A new feature is now added to the project and the structure of the project now looks like this.


The pitfall
If building the WSP package the output now shows the package is only SharePoint 2010 compatible. The newly added files elements.xml, feature.xml, the .webpart and the .cs file all seems to be SharePoint 2007 compatible - so what made the project incompatible. It took me a while to figure this out.

Right-click the project and choose Open Folder in Windows Explorer.

The Manifest.Config file is back. The Add feature wizard created the file and though it is not added to the Visual Studio project. WSP Builder finds it and uses it while building the WSP package. Delete file and build the WSP package again - and now the WSP package is both SharePoint 2007 and SharePoint 2010 compatible again.

That's it folks...

No comments: