How to create a Visual Studio Item Template
Posted by zieglers on June 24, 2010
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.
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.