These days there are a lot of cool ways for .NET developers to be more productive. From code generation to refactoring, there is never a shortage of plug-ins out there to help us in this goal. But let's not forget about the great tools and features already built into Visual Studio that we can use. One feature I have started using recently is the item template.
Whether you know what an item template is or not, you use them in your every day development. Item templates are those pre-written files you add to your project whenever you need a new ASP.NET Web Form, Visual Basic class, or Web Configuration file. Can you imagine how much it would slow developers down if we had to start with a blank sheet of paper every time we wanted to add a new item to our project? Lucky for us we have all those wonderful templates to help us along. But where did all that pre-written code come from in the first place?
There are tons of item templates pre-installed and packaged with Visual Studio out of the box. But I'm sure you, like me, have often overlooked the 'My Templates' section just below the Visual Studio installed templates. However, when you get to the point that the pre-installed templates do not serve your specific needs, you might start to wonder how you can add your own templates. Lucky for us, doing so is super easy and takes just a couple of minutes.
Simply create a new source file or use a file in a current solution and give it the basic code and structure you wish all files of this type to have. In my case, I'm creating an NUnit test class. Once you have set up the source file, select File | Export Template...
Click the Item template radio button and select the project that contains the file you just created.
Select the source file.
All references from the selected project will be displayed to allow you to choose which should be automatically included in any project which uses this template. Only select those references that you specifically need for the template. Note that Visual Studio warns you if you reference a library that is not pre-installed with Visual Studio. In this case, in order for the template to work the machine will have to have the NUnit library installed.
Finally, select an icon to give your template, give the template a name and a description, ensure the 'Automatically import the template into Visual Studio' box is checked, and click Finish.
Voila! You have created your first template. But where exactly was the template installed? Visual Studio zips up the generated template files and places the new template in the default user template directory (unless you have specified another template directory). In order to have my new template only show up in the Visual C# | Code node when adding a new item, I moved the zip file to the Visual C#\Code subdirectory.
No when I go to add a new item to my Visual Studio project, I get the shiny new template for faster and easier code.
Now that you have your template created, you might be wondering how you can install it on another machine so that your fellow developers can take advantage of all your hard work. Well, believe it or not copying and installing templates is even easier than creating them. Simply take the zip file generated by Visual Studio when you created your new template, and copy it to another machine. Then, tell Visual Studio where to look for the template. Open up Visual Studio options (Tools | Options) and navigate to Projects and Solutions. From there you can set the location for Visual Studio to search for user item templates. If it finds any, they will automatically appear for you when you go to add a new item. Easy, right?
As you can see from the image above, Visual Studio also checks for project templates in Visual Studio. Project templates give you the ability to pre-define the content and structure of entire software projects. However, I will leave the creation of project templates as an exercise for you, dear reader. If you'd like more information, check out this article by Matt Milner in MSDN magazine.
Subscribe to be the first to get our expert-written articles and tutorials for developers!