A few days ago I presented a session on Visual Studio Extensibility. In this post you will find a short walkthrough for this session.
During the course of the following walkthrough you will learn to setup a basic project extension, explorer node type and deployment step.
Get the download for this walkthrough here.
Solution Setup
You begin with creating a new VSIX project. VSIX is a new packaging model for Visual Studio extensions. Note that you need to target the .NET 4.0 Framework.
I named my project VirtualConferencePackage
Delete the code file since you'll not be coding in this project.
Next, add a new project to the solution. Target the .NET 4.0 Framework. I named this project VirtualConferenceExtensions since it will contain the main extension code.
For the VirtualConferenceExtension project, add references to the following items. You can find them all on the .NET tab. All these assemblies are part of the public APIs of the SharePoint project model.
- Microsoft.VisualStudio.SharePoint
- Microsoft.VisualStudio.SharePoint.Explorer.Extensions
- Microsoft.VisualStudio.SharePoint.Utilities
Next, add a reference to yet another DLL on the .NET tab. This time you are including the Managed Extensibility Framework, or MEF. This is basically a new .NET plugin system which allows easy application composition.
- System.ComponentModel.Composition.
There is one more project to add to the solution. This project will contain the code that integrates with SharePoint. You need a separate assembly because SharePoint is .NET 3.5 / 64 bit, and Visual Studio hosts .NET 4.0 / 32 bit.
Be sure to set the .NET Framework to version 3.5. I named this project VirtualConferenceCommands since the integration with SharePoint is command-based.
Add references to the following assembly belonging to the project model.
- Microsoft.VisualStudio.SharePoint.Commands
Also add a reference to the Microsoft.SharePoint DLL. Currently you'll still need to use the Browse tab to locate it in the SharePoint installation folder, but that'll probably be resolved before RTM! (I hope)
Next, I removed the code files, and the references I don't need, like System.Data. This leaves the following shell. At this point in time you may want to right-click the VSIX project you added first and set it as the startup project.
Configuring packaging
Open the source.extension.vsixmanifest file and behold the manifest editor. Change some of the metadata to your liking.
You will need to include both the extension and the command assembly. Hit Add Content to add the extension DLL. The extension DLL needs to be packaged as a MEF component since it will use MEF for extending the SharePoint project system.
Next do the same for the command assembly. This assembly is unknown to Visual Studio, and hence is a Custom Extension Type. Use the identifier SharePoint.Commands.v4 so that the SharePoint project model can find it.
A simple project extension
The first demo for the day is a project extension. Add a new code file 'ProjectExtension' to the extensions project. Make this class implement the ISharePointProjectExtension interface. Export the class using the MEF Export attribute. This allows the SharePoint project model to load it.
[Export(typeof(ISharePointProjectExtension))]
class ProjectExtension
: ISharePointProjectExtension
{
public void Initialize(ISharePointProjectService projectService)
{
}
}
The ISharePointProjectService you receive can be seen as a reference to the entire Visual Studio solution. Meaning you get events such as ProjectAdded, and you can enumerate over all projects. You can also hook into events that have a particular project as the context such as ProjectMenuItemsRequested and ProjectPropertiesRequested. The first is called to show menu items on the project context menu in the Solution Explorer. The second is called to get objects to show in the Properties window. First hook into the event.
projectService.ProjectMenuItemsRequested += projectService_ProjectMenuItemsRequested;
Then you implement this method. You get to add menu items to the 'Action' area and the 'Add' area of the context menu. Super easy to do. Add the menu item using only text, and hook the click event. As a demo, here's one that calls IIS Reset.
void projectService_ProjectMenuItemsRequested(
object sender, SharePointProjectMenuItemsRequestedEventArgs e)
{
IMenuItem item = e.ActionMenuItems.Add("Reset IIS");
item.Click +=
delegate
{
string path = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.System),
"iisreset.exe");
Process.Start(
new ProcessStartInfo
{
FileName = path
});
};
}
Now say that you want to provide the option to either do an IIS Reset or an application pool recycle. You could expose a custom setting on the Properties window. To do this, hook into the ProjectPropertiesRequested event and go for it!
projectService.ProjectPropertiesRequested += projectService_ProjectPropertiesRequested;
You will first need to have a custom 'Properties' class to define the project properties you want to expose. This can be an internal class. Here's a simple one that contains a custom ResetMode property. This is an two-value enumeration with the values Recylce or Reset. As you can see you will need to use a backing store that survives quiting VIsual Studio. In this case I will use the user properties file.
class ProjectProperties
{
public IDictionary<string, string> Properties { get; internal set; }
[Category("Virtual Conference")]
public ResetMode ResetMode
{
get
{
return Properties.ContainsKey("ResetMode") ?
(ResetMode)Enum.Parse(typeof(ResetMode), Properties["ResetMode"]) :
ResetMode.Recycle;
}
set { Properties["ResetMode"] = value.ToString(); }
}
}
And now in the method hooked to the event, you add an instance of this object to the collection of items to show in the Properties window.
void projectService_ProjectPropertiesRequested(
object sender, SharePointProjectPropertiesRequestedEventArgs e)
{
e.PropertySources.Add(new ProjectProperties
{
Properties = e.Project.ProjectUserFileData
});
}
You should now be able to press F5 and see the results of your efforts. Create a new SharePoint project and check out the menu item and custom property.
That's it for now. Have fun building more extensions.
Again: get the download for this walkthrough here.
Hope it helps!