Zieglers

Just little about C#, .NET, SQL Server, SharePoint and SAP

How to create a Visual Studio Item Template

Posted by zieglers on June 24, 2010

BACKGROUND:A number of predefined project and project item templates are installed when you install Visual Studio. You can use one of the many project templates to create the basic project container and a preliminary set of items for your application, class, control, or library. You can also use one of the many project item templates to create, for example, a Windows Forms application or a Web Forms page to customize as you develop your application.
 

In addition to the installed templates that are available in the New Project and Add New Item dialog boxes, you can access templates that you have created.

MOTIVATION:Developers are lazy. I don’t mean that in a bad way, but in the same way that a smart horse is a lazy horse. Developers just don’t stand for repeating the same tasks without finding a way to automate them. I count myself among the lazy developers. I’m quite willing to spend time figuring out how to automate those repetitive tasks that I find myself doing often.

Visual Studio provides enough extensibility points that I’ve made a new rule for myself: If I do the same task twice, I figure out how to automate it.

In this post, I’ll try to show you ‘How to create an Item Template‘ for your Visual Studio project.

This will allow you to take advantage of C#’s Item Templates to automate tasks that you find yourself having to perform on a regular basis.

DEMO:

Now, let’s assume we have an ‘Application Pages’ project. We’ve been creating some app pages for a while and now we’d like to automate this process.

Since creating an app page requires a number of steps, we don’t want to perform those steps again and again, so it’s time for an app page template for our project.

Before creating your item template, get your code in a position where it can be used as a template once it’s been generated by template. This means include all the tasks you repeat and only leave page specific code / logic. For that part of the code, simply you can use a comment saying: // YOUR CODE GOES HERE.

Ok, say you have your project item (you’ll create your template from) ready.

First, we start Export Template wizard from File menu.

Then, we choose Template Type. In our case, it’s ‘Item Template‘.

Next, we select the item we’d like to export as an item template.

Then, we select item references to include with our item.

Finally we name our item template, provide a custom icon for it if you want and a description.

Once you click finish, your item template will be generated. Solution and Item templates in Visual Studio are in ZIP format.

Now, let’s create an application page by using item template we’ve just created. For this, simply go to your project, right click where you want to add a new app page and select new item.

Select item template just created.

Provide a name for your app page and click Add. That’s all! Now a new app page has been created using your Item Template.

This concludes our post on ‘How to create an Item Template‘. Remember to give a second thought while coding, in case you find yourselves doing same thing more than once or twice, it might be a good candidate for an item template.

Zieglers

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: