Click here to Skip to main content
Click here to Skip to main content

Customization of Project and Item Templates in Visual Studio

, 18 Apr 2011
Rate this:
Please Sign up or sign in to vote.
How to customize project and item templates in Visual Studio.

Introduction

This article explains how to customize the project and item templates in Visual Studio. The article comes in three parts. The first part explains how to customize project templates. The second part explains how to customize item templates, and the third part explains how to create text template generators for project items.

Though there are different project types of templates available in Visual Studio for different scenarios, a lot of initial designing and coding is common and has to be done for every project. To make work easier, Visual Studio provides features for customizing the project and item templates for different scenarios.

Using the code

First, we have to create a new project template.

I am creating a new project template for an ASP.NET Web Application, and adding custom parameters to be accepted from the user which will be updated in the project files at the time of creation of the project.

The following are the steps to create a new project template:

  • Create a new ASP.NET Web Application and name it MyWebTemplate.
  • Add a new folder named Views in the project.
  • Add a new .aspx page in the Views folder and name it ViewPage.aspx.
  • Add a custom parameter named $Title$ in the ViewPage.aspx file.
  • Save the Project.
  • Click Export Template in the File menu.
  • In the Export Template Wizard, select Project Template and click Next.
  • Enter Template Name and Description, select Automatically import the template into Visual Studio, and click Finish.

MyWebTemplate is copied as a .zip file.

Second, we have to create a new Custom Wizard as a Class Library project to accept user input at the time of project creation.

Here is the sample code for the Custom Wizard assembly. It contains a class named CustomWizard implementing the IWizard interface.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualStudio.TemplateWizard;
using EnvDTE;
public class CustomWizard : IWizard
{
   private UserInputForm inputForm;
   private string Title;

   public void BeforeOpeningFile(ProjectItem projectItem)
   {
   }

   public void ProjectFinishedGenerating(Project project)
   {
   }

   public void ProjectItemFinishedGenerating(ProjectItem
       projectItem)
   {
   }

   public void RunFinished()
   {
   }

   public void RunStarted(object automationObject,
       Dictionary<string, string> replacementsDictionary,
       WizardRunKind runKind, object[] customParams)
   {
       try
       {
           inputForm = new UserInputForm();
           inputForm.ShowDialog();
           Title = inputForm.get_Title();
           replacementsDictionary.Add("$Title$",
               Title);
       }
       catch (Exception ex)
       {
           MessageBox.Show(ex.ToString());
       }
   }

   public bool ShouldAddProjectItem(string filePath)
   {
       return true;
   }
}

The following is the sample code to accept a custom parameter from the user:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

       public UserInputForm()
       {
           InitializeComponent();
       }

       public string get_Title()
       {
           return title;
       }

       private void button1_Click(object sender, EventArgs e)
       {
           title = textBox1.Text;
           this.Dispose();
       }
   }
}

Sign the assembly with a strong name. Install the Custom Wizard assembly into the Global Assembly Cache.

Third, we have to attach the Custom Wizard assembly to MyWebTemplate manually

To do that, edit the .vstemplate file of MyWebTemplate. Following are the steps to attach the Custom Wizard assembly:

  • Extract the MyWebTemplate.zip file. To find the location of the MyWebTemplate.zip file, go to Options in the Tools menu.., select Project and Solutions.., and find the location of the .zip file in the Location textbox.
  • Add the following code in the .vstemplate file from the extracted files:
  • <WizardExtension>
        <Assembly>CustomWizard, Version=1.0.0.0, Culture=Neutral, 
                  PublicKeyToken=f84de2a5faac26c1</Assembly>
        <FullClassName>CustomWizard.CustomWizard</FullClassName>
    </WizardExtension>
  • Create a new .zip file containing the saved .vstemplate file, and delete the old MyWebTemplate.zip file.

Finally, create a new project using the MyWebTemplate project type.

Here are the steps to create a new MyWebTemplate project:

  • Create a new project by selecting the MyWebTemplate project template. Enter the project name and click OK.
  • A UserInputForm opens. Enter title name and click Submit.
  • You can verify the title that is passed as parameter to the project and saved in the ViewPage.aspx file.

Conclusion

Generally, any custom code can be run during project creation. We can create configuration settings, reference assemblies required for the project, or create project item files and folders. But you can't validate user input; you have to assume the user enters the correct values during project creation.

The next part of this article explains the creation of item templates.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

arunsuji
Software Developer
India India
I have experience in more than 10 years in microsoft technologies. Visit my blog at
http://blog.apssoft.co.in/Arunprakash/Default.aspx

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 18 Apr 2011
Article Copyright 2011 by arunsuji
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid