Creating a Visual Studio solution item template

Recently I've been using the excellent Cake for Visual Studio extension. It makes working with Cake build scripts inside Visual Studio that much easier with task runner integration, item templates and (some) syntax highlighting.

While the item template for adding a basic Cake build script is great, it only works when you use the Add New Item functionality on a C# project. However, the majority of our build scripts sit at the solution level (inside the Solution Items folder) so we couldn't take advantage of this template when adding a new item to the solution itself.

Luckily the extension is open source so we can see how the current item template works:

<?xml version="1.0" encoding="utf-8"?>
<VSTemplate Version="3.0.0" Type="Item" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" xmlns:sdk="http://schemas.microsoft.com/developer/vstemplate-sdkextension/2010">
  <TemplateData>
    <Name>Cake Build Script</Name>
    <Description>Basic Cake build script with a sample task</Description>
    <Icon>ItemTemplate.ico</Icon>
    <TemplateID>7d181506-47e9-4f9d-8ac3-5ac65f6b8354</TemplateID>
    <ProjectType>CSharp</ProjectType>
    <NumberOfParentCategoriesToRollUp>1</NumberOfParentCategoriesToRollUp>
    <DefaultName>build.cake</DefaultName>
  </TemplateData>
  <TemplateContent>
    <ProjectItem ReplaceParameters="false">build.cake</ProjectItem>
  </TemplateContent>
</VSTemplate>

The <ProjectType> element is set to CSharp which explains why it appears for new items in C# projects but not for solution items.

According to the Visual Studio template schema the ProjectType element must contain one of the following values:

  • CSharp: Specifies that the template creates a Visual C# project or item.
  • VisualBasic: Specifies that the template creates a Visual Basic project or item.
  • Web: Specifies that the template creates a Web project or item.

None of which sound like they will work for a Solution item.

It turns out this mustn't be a common use case, as checking out the Online Solution Item templates revealed only one other template (although it does have over 8,000 downloads).

Having a look at Runsettings' .vstemplate file revealed an undocumented ProjectType of General to get an Item Template to appear in the Add New Item - Solution Items dialog - exactly what we needed!

So the last step was to create a new item template for Cake, with <ProjectType> of General and submit a pull request to have it included in the addin. Now we can add a new Cake build script at the solution level with ease!