This documentation is archived and is not being maintained.

Walkthrough: Create a Simple Project Feature that Triggers when Projects Load

Visual Studio 2010

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:

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.

NoteNote

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.

To start the Visual Studio Integration Package Wizard

  1. On the File menu, point to New, and then click Project.

  2. In the list of Installed Templates, expand the Other Project Types node, and click the Extensibility node.

  3. In the details pane, click Visual Studio Package.

    Important noteImportant

    If you did not install the Visual Studio 2010 SDK, the Visual Studio Integration Package project type is not available.

  4. In Name, type SampleProjectFeature.

  5. 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

  1. Click Next.

  2. In Choose Language, click Visual C#.

    NoteNote

    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#.

  3. In Obtain keys for assembly signing, click Generate a new key file to sign the assembly.

  4. Click Next.

  5. In Company name, type MyCompany.

  6. In VSPackage name, type SampleProjectFeature.

  7. (Optional) You can provide a specific version number.

  8. (Optional) You can customize the icon used by your package.

  9. (Optional) You can provide additional information about your package in Detailed Information.

  10. Click Next.

  11. Select the Menu Command check box.

  12. Verify that the Tool Window and Custom Editor check boxes are cleared.

  13. Click Next.

  14. In Command Name, type Toggle Project Loaded Dialogs.

  15. In Command ID, type cmdidEnableDialogs.

  16. Click Next.

  17. Clear the Integration Test Project and Unit Test Project check boxes.

    NoteNote

    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.

  18. 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.

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

  1. In Solution Explorer, double-click SampleProjectFeaturePackage.cs to open it in the code editor.

  2. In the code editor, find the MenuItemCallback method.

  3. Replace the method definition with the following code:

            private void MenuItemCallback(object sender, EventArgs e)
            {
                SampleProjectFeature.EnableDialogs = !SampleProjectFeature.EnableDialogs;
            }
    
    NoteNote

    An error appears in the Error List because you have not yet defined the SampleProjectFeature class and the EnableDialogs method.

  4. On the File menu, click Save SampleProjectFeaturePackage.cs.

    Next, you define the project feature class.

To define the SampleProjectFeature class

  1. 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.

  2. In Name, type SampleProjectFeature.cs.

  3. Click Add.

    The new class is added to your project and the code editor appears and displays the class definition.

  4. In Solution Explorer, right-click the project and then click Add Reference.

    The Add Reference dialog box appears.

  5. Click the .NET tab.

  6. In the list of components, highlight the following references and click OK:

    • Microsoft.Data.Schema

    • Microsoft.VisualStudio.Data.Schema.Package

  7. 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;
    
    
  8. 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.

  9. 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.

  10. Add the following constructor to your class:

            static SampleProjectFeature()
            {
                EnableDialogs = true;
            }
    

    The constructor initializes the EnableDialogs property.

  11. 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.

  12. 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.

  13. 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

  1. Open the Start menu, point to Microsoft Visual Studio 2010, point to Visual Studio Tools, and click Visual Studio Command Prompt (2010).

  2. Navigate to the folder that contains your built assembly (SampleProjectFeature.dll).

  3. Type the following command line:

    SN.EXE -T SampleProjectFeature.dll

  4. 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

  1. In Solution Explorer, select the SampleProjectFeature project.

  2. On the Project menu, select Add New Item.

  3. In the Templates pane, locate and select the XML File item.

  4. 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.

  5. 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>
    
    

  6. 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

  1. In Solution Explorer, click SampleProjectFeature.Extensions.xml.

  2. In the Properties window, change the Build Action property to Content.

  3. Change the Include in VSIX property to True.

  4. On the File menu, click Save All.

  5. On the Build menu, click Build SampleProjectFeature.

    The vsixmanifest file is updated. Next you are ready to 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

  1. On the Project menu, click Build.

    Your project should build successfully.

  2. 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.

  3. On the File menu, point to Open, and click Project/Solution.

  4. 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.

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:

Show: