Tip/Trick: Creating Re-Usable Project and Item Templates with VS 2005

Typically when you work on projects you have a standard approach that you like to use to structure and organize your code (folder layout, default namespaces included, assembly references included by default, standard CSS stylesheets included, etc).  This is often the first thing developers setup and do when they first start a new project.

One nice feature that is new in both VS 2005 and Visual Web Developer is the ability to easily create both "Project Templates" and "Item Templates" that allow you to capture your personal coding preferences and default project items and encapsulate them in re-usable templates that you can quickly use when starting a new project or adding a new item to it.  This can both save you a lot of manual time, as well as ensure on new projects that everything is setup exactly the way you like them.

"Project Templates" allow you to specify the initial set of files (and their layout and content), assembly references, and other preferences when you create a new project (you'll see these listed in the "My Templates" section of your "Create New Project" or "Create New WebSite" dialog box).  "Item Templates" allow you to create a customized template that you add new files to a project (you'll see these listed in the "My Templates" section of your "Add New Item" dialog box):

You can name each project and item template using a descriptive name, and have any number of them that you want in VS 2005.  You can also use built-in macro replacement tokens to customize things like names, namespaces, paths, date/times, etc.  You can also easily publish them and share them with others -- allowing you to easily distribute them across a team or your own community of friends.

To learn how to-do this, check out this blog post that I wrote a year ago on how to create a re-usable Item Template for Web Site items.

Ameet Phadnis has recently published an even better article on ASPAlliance.com that goes into more detail on how you can create and customize both Item and Project Templates.  You can read his article here.

Hope this helps,

Scott

Published Monday, September 04, 2006 1:19 PM by ScottGu

Comments

# re: Tip/Trick: Creating Re-Usable Project and Item Templates with VS 2005

Tuesday, September 05, 2006 1:04 AM by Vikram
This was what I was looking for a few days and got it here thanks

# re: Tip/Trick: Creating Re-Usable Project and Item Templates with VS 2005

Tuesday, September 05, 2006 3:50 AM by JP
Hi Scott, I was looking for this just an hour ago, great timing :) However, I only need a .sql template file, which I simply put in C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\Templates\Database Project Items The new item dialog displays the new template, however the $safeitemname$ item is obviously not replaced this way. Any ideas for making this work?

# Re: Tip/Trick: Creating Re-Usable Project and Item Templates with VS 2005

Tuesday, September 05, 2006 4:04 AM by erik.zetterstrom@dotway.se (Erik Zetterström)

Thanks!

# re: Tip/Trick: Creating Re-Usable Project and Item Templates with VS 2005

Tuesday, September 05, 2006 11:21 AM by ScottGu

Hi JP,

If you want to send me email (scottgu@microsoft.com) I'll loop you in with a few people on the VS Core team who might know how this token replacement works for .sql files.

Thanks,

Scott

# re: Tip/Trick: Creating Re-Usable Project and Item Templates with VS 2005

Monday, September 11, 2006 9:17 PM by ab
Check the third part of the same article - http://aspalliance.com/1001_Speed_Up_with_Project_Templates_Using_Visual_Studio_2005__Part_3

# re: Tip/Trick: Creating Re-Usable Project and Item Templates with VS 2005

Wednesday, September 27, 2006 4:30 PM by gestald
really usefull....

# Creating Project/Item Templates in VS.NET 2005

Wednesday, October 25, 2006 2:54 AM by Kelly White

Scott Guthrie talks about how to create reusable project and item templates in VS.NET 2005 . I don't

# insert namespace in item templates

Friday, February 23, 2007 6:20 AM by Stefan Kohlmeister

hi Scott, do you know a way to include the namespace in the class name in the same way the WebForm template does?

# re: Tip/Trick: Creating Re-Usable Project and Item Templates with VS 2005

Tuesday, February 27, 2007 2:39 AM by ScottGu

Hi Stefan,

Unfortunately there isn't anyway to pick up the namespace when creating a template with a web site project.

However, if you use a web application project, you can pick up the namespace and have that automatically create the namespace in the new web form and class.

Hope this helps,

Scott

# ASP.NET 2.0 Tips, Tricks, Recipes and Gotchas - ScottGu's Blog

Pingback from  ASP.NET 2.0 Tips, Tricks, Recipes and Gotchas - ScottGu's Blog