Item Templates in Visual Studio 2015

Datetime:2016-08-23 04:27:29         Topic: Visual Studio          Share        Original >>
Here to See The Original Article!!!

Now that blog posts, metadata, and comments are combined in one file, it's less work to start a new post. I figured I'd reduce friction even further by using a Visual Studio Item Template. It's best to remove every barrier to writing new posts. ;-)

I found a nice article on Visual Studio templates by Eric Sowell which answered the basic questions and another about multiple file templates that happened to get me past a stumbling block when editing a template.

Mostly everything is the same as in those posts, except the directories are different for the newer version of Visual Studio. There are a couple of caveats when doing a template for a file that doesn't end in .cs . To make it easier to see what I'm doing, I also made a companion video .

Here's the template I'm using for new posts:

title: $safeitemname$
created: $time$
published: $time$

# comments being here


To make a template, you simply create a file in Visual Studio, put whatever text you want in it, and then go to File | Export Template and follow the prompts.

The words surrounded by dollar signs are template parameters. There's a nice list of template parameters on msdn.

For whatever reason, the template paramters are not always replaced automatically. For .cs files, it worked with no further effort. For my .markdown template, I had to extract the exported template and edit the vstemplate xml file. There is an attribute of the ProjectItem nodde called ReplaceParameters . Set it's value to true.

Now, here's the stumbling block I mentioned before. You must zip up the files directly. Do not , like I did, zip the folder. It creates an extra level of nesting. There won't be any warning in visual studio. The template just won't show up.

After editing the template, I found it necessary to manually move the file from Users\{user}\Documents\Visual Studio 2015\My Exported Templates to Users\{user}\Documents\Visual Studio 2015\Templates\ItemTemplates .

Finally, I needed to restart Visual Studio. Sometimes using the suggestion of issuing devenv /installvstemplates from a Visual Studio command prompt seemed to work. Other times it didn't. Restarting Visual Studio worked consistently. Once it's done, you don't have to think about it again, so I didn't spend more time trying to nail down the fastest way to refresh the template cache.

Hopefully those tips will help you get your own templates up and running smoothly.

If anything is unclear, try watching the companion video on YouTube. Pleasecontact me with any comments, ommissions, or clarifications. Making the video was fun. I'll mix in more video content with future posts.

About List