Building Visual Studio 2010 extensions in one project – Extending Visual Studio SharePoint Developer Tools tip #9

Creating a new VSIX project in Visual Studio 2010
When building extensions for Visual Studio 2010 SharePoint Developer Tools, VSIX project by default does only the packaging. Your extension’s code has to be included in a separate assembly. Did you know however that all you need is a simple trick to build your extension using a single project?

Visual Studio 2010 SharePoint Developer Tools – more than just tools

Visual Studio 2010 ships with the new Visual Studio 2010 SharePoint Developer Tools which simplify development on the SharePoint 2010 platform. While the tools provide some great functionality out of the box, their real power is extensibility. Whenever you feel that something is missing or doesn’t work the way you want it, you can tailor the tools to meet your specific needs. How cool is that?!

The Visual Studio 2010 SharePoint Developer Tools extensibility model is based on the Managed Extensibility Framework and uses VSIX for distributing the extensions. If you’re serious about developing on the SharePoint 2010 platform you should definitely read more about the extensibility capabilities of the new Visual Studio 2010 SharePoint Developer Tools. Microsoft did awesome job describing the extensibility capabilities of the new tools and providing us with samples making it very easy for us to write our own extensions and to work more efficiently.

The out of the box extensibility experience

The first thing that you do, when creating a new extension for the Visual Studio 2010 SharePoint Developer Tools, is to create a new VSIX project.

Creating a new VSIX project in Visual Studio 2010

Now let’s try to add a new class to the VSIX project. After you build the project, you will see that there is no assembly in the output directory.

VSIX project’s output directory after building. No assembly has been built.

By default the VSIX project only creates the VSIX package. So if you want to include some code in your VSIX package, no matter how simple it is, you would need another project. Luckily, there is a simple trick to make the VSIX project build as well the VSIX package as the assembly just as a regular Class Library project would do.

Making VSIX project build an assembly

In order to make the VSIX project build an assembly you have to modify some project properties. Since it cannot be done through the UI, you have to unload the project first.

Unloading the VSIX project in Visual Studio 2010

Once unloaded you can move on to editing the project file: either in Visual Studio itself or in your favorite text editor.

Editing the VSIX project file in Visual Studio 2010

In order to make the VSIX project build an assembly from your code, you have to change the value of the CopyBuildOutputToOutputDirectory to true.

The CopyBuildOutputToOutputDirectory project setting highlighted in Visual Studio 2010

Once done save your changes and reload the project in Visual Studio. When you build the project, you will see in the output directory not only the VSIX package but also the assembly containing your code from the VSIX project.

VSIX project’s output directory after building with the CopyBuildOutputToOutputDirectory setting enabled. Assembly has been built for the VSIX project.

Although the CopyBuildOutputToOutputDirectory setting makes the VSIX project build an assembly with your code from the VSIX project, one more thing is needed to actually include the VSIX assembly in the generated VSIX package. For this you need to enable the IncludeAssemblyInVSIXContainer project setting in the project file.

The IncludeAssemblyInVSIXContainer project setting highlighted in Visual Studio

Once again save your changes and reload and rebuild your project. If you open the VSIX package, you should see the VSIX assembly included.

VSIX package with the assembly from the VSIX project included

The last thing to do, to make the package “see” your extension, is to register the VSIX assembly as a MEF component. For this open your VSIX Manifest and include the assembly in the VSIX package’s contents.

Registering the assembly from the VSIX project with the VSIX package

When you build the project now the VSIX package will include the assembly from the VSIX project and you will be able to create your extension using a single project. If your Visual Studio 2010 SharePoint Developer Tools extension will need to communicate with the SharePoint API, you will still need a separate project for that. The reason for this is that the VSIX project is a .NET 4 project, while SharePoint 2010 API work with .NET 3.5.

Summary

Visual Studio 2010 SharePoint Developer Tools are extensible. This allows developers to tailor their work environments to their specific needs. The extensibility of Visual Studio 2010 SharePoint Developer Tools is based on MEF and uses VSIX package for the distribution of extensions. By default VSIX projects don’t build assembly and require you to create a separate project to store your extension’s code. Using a simple trick you can make the VSIX project build both assembly and VSIX package and avoid unnecessary projects in your solution.

Technorati Tags: Visual Studio 2010,SharePoint 2010

Comments

comments powered by Disqus