Make Custom Class Item Templates for Windows Store Apps Visible in VS 2012

Make the Item Visible

One very helpful feature of Visual Studio is the ability to create custom item or project templates. I’m using this feature for years to get a pre-formatted C# class, containing several regions.

In VS 2012, one can set the directory where these custom item templates are located via “Tools / Options / Projects and Solutions” dialog. This folder contains several subfolders for some categories, i.e. JavaScript, Visual Basic, or Visual C#. These were the places where to put the appropriate custom item templates.

The first thing that has changed in VS 2012 (at least Update 3, can’t recall if this has changed before) compared to VS 2010 is that you have to create a “My Templates” folder below the corresponding one. Means, if I have a custom C# class item template, it has to be put into “C:\Users\nnn\Documents\Visual Studio 2012\Templates\ItemTemplates\Visual C#\My Templates” to be shown in the “My Templates” section of the “Add New Item” dialog.

But this did not work for all kinds of project types. At least, it does not work for Windows Store app projects. Even though the item template is located at the right place, it is not listed by the “Add New Item” dialog. Of course it is listed in a Windows console app project.

For whatever reason, the Windows Store app project requires an additional setting in the vstemplate file, which is part of the item template zip file. This setting is the element <TemplateID>Microsoft.CSharp.Class</TemplateID> inside the <TemplateData> section. Putting the updated vstemplate into the class item zip, the template is also listed in the “My Templates” section of the “Add New Item” dialog in Windows Store app projects. The TemplateID is not generated when exporting the template by VS.

You might notice too, that from the moment on when the TemplateID is added, the item template will be listed twice for other project types, i.e. Windows console application.

After making these changes, I had some issues with the item template cache (retrieving an error when selection one of the duplicates in the list). I just deleted the directory ItemTemplatesCache in “C:\Users\nnn\AppData\Roaming\Microsoft\VisualStudio\11.0”.

How to Create a Custom Item Template

In case you hadn’t created a custom item template yet, have a look at How to: Create Item Templates.

On my VS 2012 installation, the “Export Template…” menu item was not part of the File menu. So I had to add it using the “Tools / Customize” dialog. Switched to the “Commands” tab, selected “Menu bar”, “Add Command”, selected “File” from the categories, and already found the missing item.

Exporting the template, it will written to “C:\Users\nnn\Documents\Visual Studio 2012\My Exported Templates” by default. Selecting the “Automatically import the template into Visual Studio” option copies the file to the directory configured in the VS 2012 settings (see above). The default is “C:\Users\nnn\Documents\Visual Studio 2012\Templates\ItemTemplates”. You have to move it into the appropriate subdirectory yourself!

Links

I have to confess that I did not found out all of it by myself. Here are the links to the postings in MS forums where I found most of the informations:

  • Can’t see My Templates (VS 2012 Update 3)
  • Where to put Custom Item Templates for Windows Store Apps (XAML/C#) in VS2012 Update 3?