Building Visual Studio 2010 extensions in one project – Extending Visual Studio SharePoint Developer Tools tip #9
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.
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.
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.
Once unloaded you can move on to editing the project file: either in Visual Studio itself or in your favorite text editor.
In order to make the VSIX project build an assembly from your code, you have to change the value of the CopyBuildOutputToOutputDirectory to true.
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.
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.
Once again save your changes and reload and rebuild your project. If you open the VSIX package, you should see the VSIX assembly 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.
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.
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.