Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
 How to: Use Wizards with Project Te...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Visual Studio 
How to: Use Wizards with Project Templates 

Visual Studio 2005 provides the IWizard interface that, when implemented, enables you to run custom code when a user creates a project from a template.

Project template customization can be used to:

  • Display custom UI that collects user input to parameterize the template.

  • Add parameter values to use in the template.

  • Add additional files to the template.

  • Perform virtually any action allowed by the Visual Studio automation object model on a project.

The IWizard interface methods are called at various times while the project is being created, starting as soon as a user clicks OK on the New Project dialog box. Each method of the interface is named to describe the point at which it is called. For example, Visual Studio calls RunStarted immediately when it starts to create the project, making it a good location to write custom code to collect user input.

Most of the code that you write for custom wizards will use the DTE object, which is the main object in the Visual Studio automation object model, to customize the project. For more information on the automation object model, see the Visual Studio Industry Partner (VSIP) program documentation.

Creating a Custom Template Wizard

This topic shows how to create a custom wizard that opens a Windows Form before the project is created. The form allows the user to add a custom parameter value that will then be added to the source code during project creation. The main steps, each of which is explained in detail, are as follows.

To create a custom template wizard

  1. Create an assembly that implements the IWizard interface.

  2. Install the assembly into the global assembly cache.

  3. Create a project and use the Export Template wizard to create a template from the project.

  4. Modify the template by adding a WizardExtension element in the .vstemplate file to link the template to the assembly that implements IWizard.

  5. Create a new project using the custom wizard.

Implementing IWizard

The first step in the process is to create an assembly that implements IWizard. This assembly uses the RunStarted method to display a Windows Form that allows the user to add a custom parameter value, which will then be used during project creation.

NoteNote

This example uses Visual C# to implement IWizard, although you could also use Visual J# or Visual Basic.

To implement IWizard

  1. Create a new class library project.

  2. Create a class that implements the IWizard interface. See the code below for a Visual C# example of a fully implemented IWizard interface.

This example contains two code files: IWizardImplementation, a class that implements the IWizard interface, and UserInputForm, the Windows Form for user input.

IWizardImplementation Class

The IWizardImplementation class contains method implementations for every member of IWizard. In this example, only the RunStarted method performs a task. All other methods either do nothing or return true.

The RunStarted method accepts four parameters:

  • An Object parameter that can be cast to the root _DTE object, to enable you to customize the project.

  • A Dictionary parameter that contains a collection of all pre-defined parameters in the template. For more information on template parameters, see Template Parameters.

  • A WizardRunKind parameter that contains information about what kind of template is being used.

  • An Object array that contains a set of parameters passed to the wizard by Visual Studio.

This example adds a parameter value from the user input form to the Dictionary parameter. Every instance of the $custommessage$ parameter in the project will be replaced with the text entered by the user.

C#
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TemplateWizard;
using System.Windows.Forms;
using EnvDTE;

namespace CustomWizard
{
    public class IWizardImplementation:IWizard
    {
        private UserInputForm inputForm;
        private string customMessage;

        // This method is called before opening any item that 
        // has the OpenInEditor attribute.
        public void BeforeOpeningFile(ProjectItem projectItem)
        {
        }

        public void ProjectFinishedGenerating(Project project)
        {
        }
        
        // This method is only called for item templates,
        // not for project templates.
        public void ProjectItemFinishedGenerating(ProjectItem 
            projectItem)
        {
        }

        // This method is called after the project is created.
        public void RunFinished()
        {
        }

        public void RunStarted(object automationObject,
            Dictionary<string, string> replacementsDictionary,
            WizardRunKind runKind, object[] customParams)
        {
            try
            {
                // Display a form to the user. The form collects 
                // input for the custom message.
                inputForm = new UserInputForm();
                inputForm.ShowDialog();

                customMessage = inputForm.get_CustomMessage();

                // Add custom parameters.
                replacementsDictionary.Add("$custommessage$", 
                    customMessage);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        // This method is only called for item templates,
        // not for project templates.
        public bool ShouldAddProjectItem(string filePath)
        {
            return true;
        }        
    }
}

User Input Form

The user input form provides a simple form for entering a custom parameter. The form contains a text box named textBox1 and a button named button1. When the button is clicked, the text from the text box is stored in the customMessage parameter.

To add a Windows Form to the solution

  1. On the Project menu, click Add New Item.

  2. Click Windows Form, name the file UserInputForm.cs, and click OK.

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CustomWizard
{
    public partial class UserInputForm : Form
    {
        private string customMessage;

        public UserInputForm()
        {
            InitializeComponent();
        }
       
        public string get_CustomMessage()
        {
            return customMessage;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            customMessage = textBox1.Text;

            this.Dispose();
        }
    }
}

Installing the Assembly into the Global Assembly Cache

The assembly that implements IWizard must be signed with a strong name and installed into the global assembly cache.

To install the assembly into the global assembly cache

  1. Sign the assembly with a strong name. For more information, see How to: Sign an Assembly with a Strong Name or How to: Sign an Assembly (Visual Studio).

  2. Install the strong-named assembly into the global assembly cache. For more information, see How to: Install an Assembly into the Global Assembly Cache.

Creating a Project to Use as a Template

In this example, the project used as the template is a console application that displays the message specified in the user input form of the custom wizard.

To create the example project

  1. Create a new Visual C# console application.

  2. In the Main method of the application, add the following line of code.

    Console.WriteLine("$custommessage$");

    The parameter $custommessage$ is replaced with the text entered in the user input form when a project is created from the template.

  3. On the File menu, click Export Template.

  4. In the Export Template wizard, click Project Template, select the correct project, and click Next.

  5. In the Export Template wizard, enter descriptive information about the template, select the Automatically import the template into Visual Studio check box, and click Finish.

    The template now appears in the New Project dialog box, but does not use the custom wizard.

The following example shows the full code file before it has been exported to a template.

C#
using System;
using System.Collections.Generic;
using System.Text;

namespace TemplateProject
{
    class WriteMessage
    {
        static void Main(string[] args)
        {
            Console.WriteLine("$custommessage$");
        }
    }
}

Modifying the Template

Now that the template is created and appears in the New Project dialog box, you must modify it so that it uses the assembly that you created in the preceding steps.

To add the custom wizard to the template

  1. Locate the .zip file that contains the template.

    1. On the Tools menu, click Options.

    2. Click Projects and Solutions.

    3. Read the Visual Studio user project templates location text box. For more information, see General, Projects and Solutions, Options Dialog Box.

    By default, this location is My Documents\Visual Studio 2005\Templates\ProjectTemplates.

  2. Extract the .zip file.

  3. Open the .vstemplate file in Visual Studio.

  4. After the TemplateContent element, add a WizardExtension Element (Visual Studio Templates) element with the strong name of your custom wizard assembly. For more information on finding the strong name of your assembly, see How to: View the Contents of the Global Assembly Cacheand How to: Reference a Strong-Named Assembly.

    The following example shows a WizardExtension element.

    <WizardExtension>
        <Assembly>CustomWizard, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=fa3902f409bb6a3b</Assembly>
        <FullClassName>CustomWizard.IWizardImplementation</FullClassName>
    </WizardExtension>

Using the Custom Wizard

Now you can create a project from your template and use the custom wizard.

To use the custom wizard

  1. On the File menu, click New Project.

  2. In the New Project dialog box, locate your template, type a name, and click OK.

    The wizard user input form opens.

  3. Type a value for the custom parameter and click the button.

    The wizard user input form closes, and a project is created from the template.

  4. In Solution Explorer, right-click the source code file and click View Code.

    Notice that $custommessage$ has been replaced with the text entered in the wizard user input form.

See Also

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Specifying the name of the assembly containing an extensions      Craig Skibo - MSFT ... MUstafa Fazlyi   |   Edit   |   Show History

When specifying the name of the assembly in the .vstemplate file, be sure to not use .dll in the <Assembly> tag. If .dll is used, it indicates that the assembly should be loaded from disk not from the probing path, and for security reasons loading will fail. Mustafa Fazlyi

Aprilgatan 9g

54330. Tibro

46000.SWEDEN

Tags What's this?: Add a tag
Flag as ContentBug
DTE and VS SDK      Craig Skibo - MSFT ... Thomas Lee   |   Edit   |   Show History
This topic states "For more information on the automation object model, see the Visual Studio Industry Partner (VSIP) program documentation." This is misleading, as it implies that the VS SDK is required to use the automation model. While you can move between the VS SDK and the DTE automation model, one is not required to use the other. Documentation of the DTE object can be found right here in the MSDN Wiki.
replacementsDictionary Dictionary      Ryan Rahlf ... Thomas Lee   |   Edit   |   Show History

The documentation here regarding the replacementsDictionary parameter to the RunStarted method seems to be in error. Stepping through the contents of the dictionary I've found that it does not contain " all pre-defined parameters in the template " but rather a subset of them. I'd recommend examining the contents of that dictionary yourself if you plan to implement IWizard.

For a list of parameters or tokens, see:

http://msdn2.microsoft.com/en-us/library/eehb4faa(VS.80).aspx

Poor mark      Tony Maresca   |   Edit   |   Show History

You get a big thumbs down rating for failing to mention what assembly references are required for the sample C# Wizard.

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker