Walkthrough: Create a Simple Project Feature that Triggers when Projects Load
You can create a project feature to perform custom actions when project-related actions occur. In this walkthrough, you create a project feature named SampleProjectFeature that displays a dialog box when a database project is loaded. You could modify this sample to perform actions when the database project is loaded.
In this walkthrough, you will accomplish the following major tasks:
Create a Visual Studio Package
Define a Project Feature
Test your Project Feature
Prerequisites
You need the following components to complete this walkthrough:
You must have installed Visual Studio 2010 Professional, Visual Studio 2010 Premium, or Visual Studio 2010 Ultimate.
You must have a database project.
You must also have the Visual Studio 2010 SDK installed on your computer. To download this kit, see this page on the Microsoft Web site: Visual Studio 2010 SDK.
Note
This walkthrough is intended for users who are already familiar with the database features of Visual Studio. You are also expected to be familiar with basic Visual Studio concepts, such as how to create a class library and how to use the code editor to add code to a class.
Create a Visual Studio Package
To start the Visual Studio Integration Package Wizard
On the File menu, point to New, and then click Project.
In the list of Installed Templates, expand the Other Project Types node, and click the Extensibility node.
In the details pane, click Visual Studio Package.
Important
If you did not install the Visual Studio 2010 SDK, the Visual Studio Integration Package project type is not available.
In Name, type SampleProjectFeature.
Click OK.
The Visual Studio Integration Package Wizard appears.
Next, you use the wizard to configure the project that you want to create.
To use the Visual Studio Integration Package Wizard to create your project
Click Next.
In Choose Language, click Visual C#.
Note
You could also create the package by using Visual Basic or Visual C++. This walkthrough only shows the sample code for Visual Basic and Visual C#.
In Obtain keys for assembly signing, click Generate a new key file to sign the assembly.
Click Next.
In Company name, type MyCompany.
In VSPackage name, type SampleProjectFeature.
(Optional) You can provide a specific version number.
(Optional) You can customize the icon used by your package.
(Optional) You can provide additional information about your package in Detailed Information.
Click Next.
Select the Menu Command check box.
Verify that the Tool Window and Custom Editor check boxes are cleared.
Click Next.
In Command Name, type Toggle Project Loaded Dialogs.
In Command ID, type cmdidEnableDialogs.
Click Next.
Clear the Integration Test Project and Unit Test Project check boxes.
Note
If you were creating a real package for use by your organization, you should consider creating tests for your package to verify that it functions correctly.
Click Finish.
A Visual Studio Integration Package project is created and appears in Solution Explorer.
Next, you will customize the code in the package to define your database project feature.
Define a Project Feature
To modify the package definition to define a database project feature, you must update the package definition and then you must add the SampleProjectFeature class.
To update the package definition
In Solution Explorer, double-click SampleProjectFeaturePackage.cs to open it in the code editor.
In the code editor, find the MenuItemCallback method.
Replace the method definition with the following code:
private void MenuItemCallback(object sender, EventArgs e) { SampleProjectFeature.EnableDialogs = !SampleProjectFeature.EnableDialogs; }
Note
An error appears in the Error List because you have not yet defined the SampleProjectFeature class and the EnableDialogs method.
On the File menu, click Save SampleProjectFeaturePackage.cs.
Next, you define the project feature class.
To define the SampleProjectFeature class
In Solution Explorer, right-click the SampleProjectFeature project, point to Add, and then click Class.
The Add New Item dialog box appears, with the class template already highlighted.
In Name, type SampleProjectFeature.cs.
Click Add.
The new class is added to your project and the code editor appears and displays the class definition.
In Solution Explorer, right-click the project and then click Add Reference.
The Add Reference dialog box appears.
Click the .NET tab.
In the list of components, highlight the following references and click OK:
Microsoft.Data.Schema
Microsoft.VisualStudio.Data.Schema.Package
In the code editor, add the following using statements to the class definition:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Data.Schema.Extensibility; using Microsoft.Data.Schema; using Microsoft.VisualStudio.Data.Schema.Package.Project; using Microsoft.VisualStudio.Shell.Interop; using System.Globalization;
Modify the class definition to specify the base class and to provide the attribute that specifies with which database schema providers this feature is compatible. For this walkthrough, the example is compatible with all database schema providers.
[DatabaseSchemaProviderCompatibility(typeof(DatabaseSchemaProvider))] class SampleProjectFeature : IDatabaseProjectFeature { }
Key interfaces, types, and methods are DatabaseSchemaProviderCompatibilityAttribute, DatabaseSchemaProvider, and IDatabaseProjectFeature.
Add a pair of properties to your class:
public static bool EnableDialogs { get; set; } private IDatabaseProjectNode ProjectNode { get; set; }
The first property is used by the command that can enable or disable this project feature. Noteworthy interface is IDatabaseProjectNode.
Add the following constructor to your class:
static SampleProjectFeature() { EnableDialogs = true; }
The constructor initializes the EnableDialogs property.
Add the Initialize method to your class:
public void Initialize(IDatabaseProjectNode projectNode) { ProjectNode = projectNode; // Hook up to a few events ProjectNode.ProjectLoaded += delegate { ShowDialog("In ProjectLoaded Event"); }; }
The Initialize method identifies the project to which the feature is being applied, and then prepares to watch for the ProjectLoaded event.
Last, add the ShowDialog method that will display a dialog box when a ProjectLoaded event occurs, if the feature is enabled:
void ShowDialog(string msg) { if (!SampleProjectFeature.EnableDialogs) return; // Show a Message Box to indicate that we were here IVsUIShell uiShell = (IVsUIShell)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SVsUIShell)); Guid clsid = Guid.Empty; int result; Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(uiShell.ShowMessageBox( 0, ref clsid, "Simple Project Feature", msg, string.Empty, 0, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST, OLEMSGICON.OLEMSGICON_INFO, 0, // false out result)); }
If the user has toggled the feature off by using the menu command, the method returns. Otherwise, this method displays a dialog box. In your own project feature, instead of displaying a dialog box, you might perform additional processing.
On the File menu, click Save SampleProjectFeature.cs.
Next, you will build the project.
To build the project
- On the Build menu, click Build Solution.
Next, you will gather assembly information generated in the project, including the version, culture, and PublicKeyToken.
To gather assembly information
Open the Start menu, point to Microsoft Visual Studio 2010, point to Visual Studio Tools, and click Visual Studio Command Prompt (2010).
Navigate to the folder that contains your built assembly (SampleProjectFeature.dll).
Type the following command line:
SN.EXE -T SampleProjectFeature.dll
Make note of the public key token. It will be used in the next procedure.
Next, you will create an XML file by using the assembly information that you gathered in the previous procedure.
To create the XML file
In Solution Explorer, select the SampleProjectFeature project.
On the Project menu, select Add New Item.
In the Templates pane, locate and select the XML File item.
In the Name text box, type SampleProjectFeature.Extensions.xml and click the Add button.
The SampleProjectFeature.Extensions.xml file is added to the project in Solution Explorer.
Open the SampleProjectFeature.Extensions.xml file and update it to match the following XML. Replace the PublicKeyToken that you retrieved in the previous procedure.
<?xml version="1.0" encoding="utf-8"?> <extensions assembly="" version="1" xmlns="urn:Microsoft.Data.Schema.Extensions" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:Microsoft.Data.Schema.Extensions Microsoft.Data.Schema.Extensions.xsd"> <extension type=" MyCompany.SampleProjectFeature.SampleProjectFeature" assembly="SampleProjectFeature, Version=1.0.0.0, Culture=neutral, PublicKeyToken=nnnnnnnnnnnnnnnn" enabled="true"/> </extensions>
On the File menu, click Save.
Next, you modify the properties of the Extensions.xml file to include it in the vsixmanifest.
To include SampleProductFeature.Extensions.xml in the vsixmanifest
In Solution Explorer, click SampleProjectFeature.Extensions.xml.
In the Properties window, change the Build Action property to Content.
Change the Include in VSIX property to True.
On the File menu, click Save All.
On the Build menu, click Build SampleProjectFeature.
The vsixmanifest file is updated. Next you are ready to test your project feature.
Test your Project Feature
To test your project feature, you must first build it, and then you can press F5 to start the experimental build of Visual Studio.
To build and test your database project feature
On the Project menu, click Build.
Your project should build successfully.
Press F5 to run your package in the experimental build of Visual Studio.
An experimental instance of Visual Studio appears. On the Tools menu, the Toggle Project Loaded Dialogs command appears.
On the File menu, point to Open, and click Project/Solution.
Browse to the database project (.dbproj) that you want to open, and then click Open.
As your database project is loaded, a dialog box appears with the message "Simple Project Feature In ProjectLoaded Event".
At this point, you can customize your project feature to add more capabilities.
Next Steps
If you copy the feature that you created in this walkthrough as a starting point for your own work, you must:
Give your extension a unique name
Give your extension a unique GUID
When you create a new package by using the Visual Studio Integration Package Wizard, a new GUID is created for you.
Before the package is available in regular instances of Visual Studio, you must register it. For more information about VSPackages, see:
See Also
Tasks
How to: Troubleshoot VSPackages
Concepts
Create Custom Features for Database Projects