Canarys | IT Services

Blogs

Create custom project templates in Visual Studio

,
Share

Visual studio installation comes with the various predefined project templates, and we can use one of these templates (like console application, class library, ASP.NET web application, etc) to create a new project.

We can create custom project templates and have they appear in create new project dialog under the project selection templates.

Here I would show you the step by step procedure on how to build the custom solution template with multiple projects, configuring it in visual studio and have it using while creating the new project.

For the very first step, create a solution with the set of projects which you would like to create a template. Here I have created a simple solution (is a Skelton) with four projects for building web application. Now, I will show you how to create a template using this solution, so that next time same solution would be created whenever I need to build web application.

solution skelton

 

In order to create this solution as a template, we need to create .vstemplate file for each of the project in the above solution. For this, the simplest way is to use the export template wizard from the visual studio.

Go to File -> Export Template… as shown below

export template

 

As we are creating the Project templates for the given solution, select the ‘Project template’ radio option, select a project name from the dropdown and click on ‘Next’

project selection

 

Fill the next wizard as you wish and uncheck the ‘Automatically import the template into Visual Studio’ option. If you check this, the exported project template will import into visual studio and can choose the imported template while creating a new project. As we are focusing the project template for entire solution, for now we will leave it with uncheck.

Finally click on ‘Finish’ button.

proj select

 

Repeat the above export wizard step for each project in the visual studio. The exported templates are in the form of .zip file. If you go inside the any exported template, you will see a MyTemplate.vstemplate as shown below.

template

 

Extract all the exported templates into a separate folder

extract the zip

 

Now we need to create .vstemplate file by referring the all the exported templates from the solution file.

Go to, File -> New -> File…new file

 

As the .vstemplate file is in the form of XML format, choose the XML file

select xml

Copy and paste the below template content into the newly added XML file

  <VSTemplate Version="3.0.0" Type="ProjectGroup" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">

  <TemplateData>

    <Name>Canarys Web ProjectName>

    <Description>A project template for building ASP.NET application along with Business layer, Data access                      layer and Service layerDescription>

    <ProjectType>CSharpProjectType>

    <ProjectSubType>

    ProjectSubType>

    <SortOrder>1000SortOrder>

    <CreateNewFolder>trueCreateNewFolder>

    <DefaultName>CanarysWebProjectDefaultName>

    <ProvideDefaultName>trueProvideDefaultName>

    <LocationField>EnabledLocationField>

    <EnableLocationBrowseButton>trueEnableLocationBrowseButton>

    <Icon>CanarysWebProject.pngIcon>

  TemplateData>

  <TemplateContent>

    <ProjectCollection>

      <ProjectTemplateLink ProjectName="$projectname$.Business">

        Project1.BusinessMyTemplate.vstemplate

      ProjectTemplateLink>

      <ProjectTemplateLink ProjectName="$projectname$.DataAccess">

        Project1.DataAccessMyTemplate.vstemplate

      ProjectTemplateLink>

      <ProjectTemplateLink ProjectName="$projectname$.Services">

        Project1.ServicesMyTemplate.vstemplate

      ProjectTemplateLink>

      <ProjectTemplateLink ProjectName="$projectname$WebPortal">

        Project1WebPortalMyTemplate.vstemplate

      ProjectTemplateLink>

    ProjectCollection>

  TemplateContent>

VSTemplate>

Save this XML file to the same location where the templates are extracted

save template

 

saved tempalte

 

As you can observe that, the template file has the name of the icon which is used by the root template. Place the icon image in the root directory where the .vstemplate file is located.

templ icon

 

Select all the files from the root directory and compress into .zip file format

zip the files

Now the final template file is created and ready to deploy it.

temlate zip

Go to DocumentsVisual Studio VersionTemplatesProjectTemplates and create a new folder to separate your template from the default templates of visual studio.

template group name

You are almost done. your template is ready to use. Go to visual studio -> create new project dialog

You will see the newly created template as shown below. Give the name of the project and click on ‘Ok’ button.

select project

The project is created with a solution where you will see a list of projects are get created from the Skelton solution.

created solution

Thanks for reading and hope this article helps you in better….. 

Leave a Reply

Your email address will not be published. Required fields are marked *

Reach Us

With Canarys,
Let’s Plan. Grow. Strive. Succeed.