Visual Studio 
Walkthrough: Creating a Visual Studio Templates and Policy Wizard 

In the previous two walkthroughs, you created a project template and associated policy files. In this walkthrough you will create a wizard that generates a Visual Studio solution based on the project template and applies policy to that solution.

Prerequisites

To complete this walkthrough you should have a good command of either Visual Basic or Visual C#. You should also have completed the previous two walkthroughs, Walkthrough: Creating a Project Template and Walkthrough: Creating Policy Files.

To Create a Visual Studio Templates and Policy Wizard

  1. Open the project you saved from the last walkthrough. If you have not yet completed them, you should first finish Walkthrough: Creating a Project Template and Walkthrough: Creating Policy Files before starting this walkthrough.

  2. From the File menu, choose Add, and then New Project.

    The Add New Project dialog box opens.

  3. Choose either Visual Basic or Visual C#, and then choose Class Library. Name the project WalkthroughWizard and click Add.

  4. In Solution Explorer, right-click WalkthroughWizard, and choose Add Reference.

    The Add Reference dialog box opens.

  5. Choose EnvDTE and EnvDTE80 and click OK.

  6. Right-click WalkthroughWizard and choose Properties. Choose the Application tab, then click Assembly Information and ensure that the Make Assembly COM-visible check box is selected.

  7. Select the Build tab and ensure that the Register For COM-Interop check box is selected.

  8. Implement EnvDTE.IDTWizard in this class, as shown below:

    ' Visual Basic
    Public Class Class1
       Implements EnvDTE.IDTWizard
    // Visual C#
    public class Class1 : EnvDTE.IDTWizard
  9. Visual C# Only: In the Code Editor, right-click EnvDTE.IDTWizard, choose Implement Interface, and then select Implement Interface. Delete the following line of code from the Execute method.

    throw new Exception("The method or operation is not implemented.");
  10. In the Execute method, add the following code:

    ' Visual Basic
    ' Get the Solution Name and path from the New Project dialog
    Dim solutionName As String = ContextParams(5).ToString
    Dim projectPath As String = ContextParams(2).ToString
    ' Get the main automation object for Visual Studio
    Dim dte As EnvDTE.DTE
    dte = CType(System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.8.0"), EnvDTE.DTE)
    ' Get a solution object representing the default solution of Visual Studio and save the solution.
    Dim soln As EnvDTE80.Solution2 = CType(dte.Solution, EnvDTE80.Solution2)
    If Not System.IO.Directory.Exists(projectPath) Then
        System.IO.Directory.CreateDirectory(projectPath)
    End If
    soln.Create(projectPath, solutionName + ".sln")
    soln.SaveAs(projectPath + ".sln")
    
    // Visual C#
    // Get the Solution Name and path from the New Project dialog
    string solutionName = ContextParams[5].ToString();
    string projectPath = ContextParams[2].ToString();
    // Get the main automation object for Visual Studio
    EnvDTE.DTE dte;
    dte = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.8.0");
    // Get a solution object representing the default solution of Visual Studio and save the solution.
    EnvDTE80.Solution2 soln = (EnvDTE80.Solution2)dte.Solution;
    if (!(System.IO.Directory.Exists(projectPath)))
        {
            System.IO.Directory.CreateDirectory(projectPath);
        }
    soln.Create(projectPath, solutionName + ".sln");
    soln.SaveAs(projectPath + ".sln"); 

    The code you added above creates the main automation object for Visual Studio. It then creates an object that represents a solution. You will use this object to add a solution folder and a project.

  11. Beneath the code you added in the previous step, add the following:

    ' Visual Basic
    ' Create a solution folder
    Dim projSolFolder As EnvDTE.Project
    projSolFolder = soln.AddSolutionFolder("WalkthroughSolutionFolder")
    projSolFolder.Globals("ElementType") = "WalkthroughSolutionFolder"
    projSolFolder.Globals.VariablePersists("ElementType") = True
    ' Create a project
    Dim templatePath As String
    Dim solnFolder As EnvDTE80.SolutionFolder = _
       CType(projSolFolder.Object, EnvDTE80.SolutionFolder)
    ' If you created the WalkthroughTemplate project in C#, then replace
    ' VisualBasic in the line below with CSharp
    templatePath = soln.GetProjectTemplate("WalkthroughTemplate", _
       "VisualBasic")
    solnFolder.AddFromTemplate(templatePath, projectPath & _
       "\WalkthroughTemplate", "WalkthroughTemplate")
    Dim projWalkthroughTemplate = _
       CType(projSolFolder.ProjectItems.Item(1).Object, EnvDTE.Project)
    projWalkthroughTemplate.Globals("ElementType") = _
       "WalkthroughTemplate"
    projWalkthroughTemplate.Globals.VariablePersists("ElementType") = _
       True)
    ' Set Policy
    projSolFolder.Properties.Item("VSPolicyExtenderProvider.TDLFileNAME").Value = _
    System.Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) & "\Microsoft Visual Studio 8\Common7\IDE\Policy\Definitions\WalkthroughPolicy.vspolicy"
    
    // Visual C#
    // Create a solution folder
    EnvDTE.Project projSolFolder;
    projSolFolder = soln.AddSolutionFolder("WalkthroughSolutionFolder");
    projSolFolder.Globals["ElementType"] = "WalkthroughSolutionFolder";
    projSolFolder.Globals.set_VariablePersists("ElementType", true);
    // Create a project
    string templatePath;
    EnvDTE80.SolutionFolder solnFolder = 
       (EnvDTE80.SolutionFolder)projSolFolder.Object;
    // If you created the WalkthroughTemplate project in Visual Basic, //then replace CSharp in the line below with VisualBasic
    templatePath = soln.GetProjectTemplate("WalkthroughTemplate", "CSharp");
    solnFolder.AddFromTemplate(templatePath, projectPath + 
       "\\WalkthroughTemplate", "WalkthroughTemplate");
    EnvDTE.Project projWalkthroughTemplate;
    projWalkthroughTemplate = 
       (EnvDTE.Project)projSolFolder.ProjectItems.Item(1).Object;
    projWalkthroughTemplate.Globals["ElementType"] = 
       "WalkthroughTemplate";
    projWalkthroughTemplate.Globals.set_VariablePersists("ElementType", true);
    // Set Policy
    projSolFolder.Properties.Item("VSPolicyExtenderProvider.TDLFileNAME").Value = 
    System.Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + "\\Microsoft Visual Studio 8\\Common7\\IDE\\Policy\\Definitions\\WalkthroughPolicy.vspolicy";

    You have now added a solution folder, a project, and set policy.

  12. From the Build menu, choose Build Solution.

Creating the .vsz and .vsdir files

You have now created a fairly simple Visual Studio wizard. In order to make this wizard accessible to users, you must create .vsz and .vsdir files and place them in the appropriate directories. For more information on .vsdir and .vsz files see Adding Wizards to the Add Item and New Project Dialog Boxes Using VSDir Files and Configuring VSZ Files to Launch Wizards.

To create .vsz and .vsdir files

  1. In Solution Explorer, select solution Walkthrough Template.

  2. From the Project menu, choose Add New Item, then select Text File. Name the file WalkthroughTemplate.vsdir.

  3. In the Code Editor, add the following text to WalkthroughTemplate.vsdir:

    WalkthroughTemplate.vsz| |Walkthrough Wizard|1|Sample Walkthrough Wizard||0|0|WalkthroughWizard
  4. From the Project menu, choose Add New Item then select Text File. Name the file WalkthroughTemplate.vsz.

  5. In the Code Editor, add the following text to WalkthroughTemplate.vsz:

    VSWIZARD 7.0
    Wizard="WalkthroughWizard.Class1"
    Param=NoLanguage
  6. From the File menu, choose Save All.

Adding Files to the Correct Directories

Now that you have created all of the ancillary files necessary for your wizard to function, you must place them in the proper directories to test it. In a subsequent walkthrough, you will create a deployment project that automatically installs the files to the correct directories.

To place files in appropriate directories

  1. In Windows Explorer, navigate to the directory that contains your solution.

  2. Select WalkthroughTemplate.vsz and WalkthroughTemplate.vsdir, right-click, and choose Copy.

  3. Navigate to the directory where Visual Studio 2005 is installed and then to the \Common7\IDE\SolutionTemplates subdirectory.

  4. Right-click and choose Paste to copy the .vsz and .vsdir files to this directory.

  5. Navigate to the directory that contains your solution.

  6. Select WalkthroughPolicy.vspolicy and WalkthroughPolicy.vspolicydef, right-click, and choose Copy.

  7. Navigate to the directory where Visual Studio 2005 is installed and then to the \Common7\IDE\Policy\Definitions subdirectory.

  8. Right-click and choose Paste to copy the policy files to this directory.

Testing Your Wizard

You wizard is now complete. All that remains is to test it.

To test your wizard

  1. Close the current instance of Visual Studio 2005 and open a new instance.

  2. From the File menu, choose New, and then choose Project.

  3. In the Project Types pane, choose Other Project Types, and then choose Visual Studio Solutions.

  4. In the Templates pane, choose WalkthroughTemplate and click OK.

    A new solution is generated and the policy file is applied.

Next Steps

The three walkthroughs you have just completed outline the basic steps involved in creating a Visual Studio Templates and Policy Wizard. Next, you can learn how to add custom help to your wizard and how to create a deployment project for your wizard in the following topics.

See Also

Tasks

How to: Create a New Template Wizard for an Enterprise Application

Other Resources

Visual Studio Templates and Policy Walkthroughs
Creating a New Enterprise Application Using Visual Studio Templates and Policy
Using Visual Studio Templates and Policy for Enterprise Applications

Tags :


Page view tracker