Click here to Skip to main content
15,861,125 members
Articles / Programming Languages / C#

Customization of Project and Item Templates in Visual Studio

Rate me:
Please Sign up or sign in to vote.
4.40/5 (4 votes)
18 Apr 2011CPOL3 min read 41.2K   426   21   1
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.
  • Image 1

  • Save the Project.
  • Click Export Template in the File menu.
  • Image 2

  • In the Export Template Wizard, select Project Template and click Next.
  • Image 3

  • Enter Template Name and Description, select Automatically import the template into Visual Studio, and click Finish.
  • Image 4

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.

C#
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:

C#
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:
  • XML
    <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.
  • Image 5

  • 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)


Written By
Software Developer
India India
I have experience in more than 10 years in microsoft technologies.

Comments and Discussions

 
QuestionAbout template project modify Pin
Toshihide Higashimori27-Oct-15 17:45
Toshihide Higashimori27-Oct-15 17:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.