{"id":2383,"date":"2010-03-04T10:01:00","date_gmt":"2010-03-04T10:01:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/visualstudio\/2010\/03\/04\/creating-and-sharing-project-item-templates\/"},"modified":"2022-10-17T11:45:51","modified_gmt":"2022-10-17T18:45:51","slug":"creating-and-sharing-project-item-templates","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/visualstudio\/creating-and-sharing-project-item-templates\/","title":{"rendered":"Creating and Sharing Project &#038; Item Templates"},"content":{"rendered":"<p>One of the great new features we added in Visual Studio 2010 is the ability to easily find and consume online Project and Item templates without leaving the New Project\/Item dialog. What may not be so obvious is how to go from a piece of code you want to share to a template contained in a VSIX file that can be uploaded to the <a href=\"http:\/\/visualstudiogallery.msdn.microsoft.com\/en-us\/\">Visual Studio Gallery<\/a> or shared with others in your organization.<\/p>\n<p>This post describes two ways (one quick and simple, the other for more complex situations) to create a VSIX file that contains your shareable code templates.<\/p>\n<h2>Quick, (mostly) Automatic Way: Export Template as VSIX<\/h2>\n<p>In a series of posts on Visual Studio 2010 Beta 1 (<a href=\"https:\/\/learn.microsoft.com\/en-us\/archive\/blogs\/pedrosilva\/creating-an-item-template-extension-part-1\">Part 1<\/a>, <a href=\"https:\/\/learn.microsoft.com\/en-us\/archive\/blogs\/pedrosilva\/creating-an-item-template-extension-part-2\">Part 2<\/a>, <a href=\"https:\/\/learn.microsoft.com\/en-us\/archive\/blogs\/pedrosilva\/creating-an-item-template-extension-part-3\">Part 3<\/a>, <a title=\"Addendum\" href=\"https:\/\/learn.microsoft.com\/en-us\/archive\/blogs\/pedrosilva\/creating-an-item-template-extension-addendum\">Addendum<\/a>), Pedro Silva walked through the process of using the standard Export Template wizard that has shipped in Visual Studio for several releases in combination with the <a href=\"http:\/\/visualstudiogallery.msdn.microsoft.com\/en-us\/e3d76792-d1b3-4d0c-9716-1f16c38be8b3\">VSIX Explorer tool<\/a> or the <a href=\"https:\/\/learn.microsoft.com\/en-us\/archive\/blogs\/aaronmar\/what-is-the-vsix-project-template\">VSIX Project template<\/a> in the Visual Studio SDK to package up your template. While this does the job, it seemed like a few too many steps for something that should be straightforward.<\/p>\n<p>To make this process much simpler, we recently published a new <b><a href=\"http:\/\/visualstudiogallery.msdn.microsoft.com\/en-us\/57320b20-34a2-42e4-b97e-e615c71aca24\">Export Template as VSIX wizard<\/a><\/b> that will guide you through the process of turning an existing project into a VSIX file containing your template. After downloading the <a href=\"http:\/\/visualstudiogallery.msdn.microsoft.com\/en-us\/57320b20-34a2-42e4-b97e-e615c71aca24\">Export Template as VSIX wizard<\/a> from the Visual Studio Gallery website or the Extension Manager dialog, you will find a new option on the File menu in Visual Studio 2010 for \u201cExport Template as VSIX\u2026\u201d. This will bring up the Export Template as VSIX Wizard, which will allow you to configure options for both the template and the VSIX file that will contain it.<\/p>\n<h2>Longer, (mostly) Manual Way: Template Project + VSIX Project<\/h2>\n<p>While this is the ideal way to quickly share some starter code with others, it is not ideal for <i>all<\/i> situations where you\u2019re creating a template. For example, you may be building a series of templates in conjunction with a VSPackage or editor extension. What if you are making frequent changes to the template files? What if you wish to keep the template source files in a version control system? What if you need to make a customization to the templates or the VSIX container not permitted by the Export Template as VSIX wizard?<\/p>\n<p>This is where the Visual Studio SDK comes in to help. In the <a href=\"http:\/\/www.microsoft.com\/download\/en\/details.aspx?id=21835\">Visual Studio 2010 SP1 SDK<\/a>, we\u2019ve included project templates for building project and item templates. (Yes, we\u2019ve created templates for templates.) If you go to the <b>Extensibility<\/b> node under <b>Visual C#<\/b> or <b>Visual Basic<\/b> in the <b>New Project<\/b> dialog, you\u2019ll find two new templates (for either a project template or an item template). Let\u2019s walk through the process of creating and packaging a C# Project template using these new templates in the Visual Studio SDK.<\/p>\n<h3>Walkthrough: Packaging a template with the Visual Studio SDK<\/h3>\n<p>First, we&#8217;ll create a project template project\u00a0which generates a zip file for a single template. Then, we&#8217;ll create a VSIX project to contain and deploy the generated project template.<\/p>\n<h3><\/h3>\n<ol>\n<li>Open the New Project dialog to create a new \u201cC# Project Template\u201d from the Extensibility category called \u201cMyTemplate\u201d. There are a few interesting things to note about the generated project:\n<ul>\n<li>The .vstemplate file has a Build Action of \u201cVSTemplate\u201d. The .cs, .csproj, and .ico files have a Build Action of \u201cNone\u201d. Since this project is meant to build a .zip file, not a .dll or .exe file, the code files are not compiled or treated as code by the build targets.<\/li>\n<li>If you open Class1.cs or ProjectTemplate.csproj in the text editor, you\u2019ll see that there are several <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/eehb4faa.aspx\">VSTemplate parameters<\/a> ($safeprojectname$, $guid1$, etc\u2026) that will be replaced when the template is instantiated.<\/li>\n<li>The files to include in the zip file are discovered at build time by reading the .vstemplate file. Any files not specified in this file will not be included in the template zip file.<\/li>\n<\/ul>\n<\/li>\n<li>Build the project. Note that a .zip file is created for you in a subdirectory of the project output folder. (In our example, the template builds to ProjectTemplatesCSharp1033MyTemplate.zip.) The subdirectory in the output folder is determined by looking at a few things at build time:\n<ul>\n<li>The \u201cType\u201d attribute in the .vstemplate file root node. Project templates go under \u201cProjectTemplates\u201d. Item templates go under \u201cItemTemplates\u201d.<\/li>\n<li>The \u201cLanguage\u201d part of the path (\u201cCSharp\u201d in our example) is determined by the &lt;ProjectType&gt; node in the .vstemplate file.<\/li>\n<li>The next part of the sub path is optional (and null by default). This is the OutputSubPath. This can be used for placing the template in a sub-node (e.g. Windows, Web Silverlight) in the New Project dialog. Since we\u2019re building a template for targeting Windows, let\u2019s specify this additional piece of metadata in the MyTemplate.csproj file. (You can edit the file by right clicking the project node and choosing \u201cUnload Project\u201d. Then right click the project node again and choose \u201cEdit MyTemplate.csproj\u201d.)\n<div id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ecd39096-430a-4870-baa0-d332729fd46e\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px; float: none; padding: 0px;\">\n<pre style=\"background-color: #ffffff; font-family: Consolas; font-size: 12px; overflow: auto;\"><span style=\"color: #0000ff;\">&lt;<\/span><span style=\"color: #800000;\">VSTemplate <\/span><span style=\"color: #ff0000;\">Include<\/span><span style=\"color: #0000ff;\">=\"MyTemplate.vstemplate\"<\/span><span style=\"color: #0000ff;\">&gt;<\/span> <span style=\"color: #0000ff;\">&lt;<\/span><span style=\"color: #800000;\">OutputSubPath<\/span><span style=\"color: #0000ff;\">&gt;<\/span><span style=\"color: #000000;\">Windows<\/span><span style=\"color: #0000ff;\">&lt;\/<\/span><span style=\"color: #800000;\">OutputSubPath<\/span><span style=\"color: #0000ff;\">&gt;<span class=\"Apple-style-span\" style=\"color: #000000;\"><span style=\"color: #0000ff;\">&lt;\/<\/span><span style=\"color: #800000;\">VSTemplate<\/span><span style=\"color: #0000ff;\">&gt;<\/span><\/span><\/span><\/pre>\n<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http:\/\/dunnhq.com --><\/p>\n<\/div>\n<\/li>\n<li>The \u201c1033\u201d in the path is the LCID of the template. The LCID for English (US) is 1033 and this is the default. To change the LCID (for example to 1041 for Japanese), you must edit the MyTemplate.csproj to the following:\n<div id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c0fc8a43-b5f5-4195-bcd8-8e49deda6515\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px; float: none; padding: 0px;\">\n<pre style=\"background-color: #ffffff; font-family: Consolas; font-size: 12px; overflow: auto;\"><span style=\"color: #0000ff;\">&lt;<\/span><span style=\"color: #800000;\">VSTemplate <\/span><span style=\"color: #ff0000;\">Include<\/span><span style=\"color: #0000ff;\">=\"MyTemplate.vstemplate\"<\/span><span style=\"color: #0000ff;\">&gt;<\/span> <span style=\"color: #0000ff;\">&lt;<\/span><span style=\"color: #800000;\">OutputSubPath<\/span><span style=\"color: #0000ff;\">&gt;<\/span><span style=\"color: #000000;\">Windows<\/span><span style=\"color: #0000ff;\">&lt;\/<\/span><span style=\"color: #800000;\">OutputSubPath<\/span><span style=\"color: #0000ff;\">&gt;<\/span><span style=\"color: #0000ff;\">&lt;<\/span><span style=\"color: #800000;\">Culture<\/span><span style=\"color: #0000ff;\">&gt;<\/span><span style=\"color: #000000;\">1041<\/span><span style=\"color: #0000ff;\">&lt;\/<\/span><span style=\"color: #800000;\">Culture<\/span><span style=\"color: #0000ff;\">&gt;<\/span><span style=\"color: #0000ff;\">&lt;\/<\/span><span style=\"color: #800000;\">VSTemplate<\/span><span style=\"color: #0000ff;\">&gt;<\/span><\/pre>\n<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http:\/\/dunnhq.com --><\/p>\n<\/div>\n<\/li>\n<li>Finally, the name of the zip file is taken from the name of the project specified in the New Project dialog.<\/li>\n<\/ul>\n<\/li>\n<li>The project we just created only builds a zip file for you. It does not build a VSIX file, nor does it set up the template for debugging in Visual Studio. To do that, add a new VSIX Project to your solution and call it \u201cMyTemplateSetup\u201d. (You can find the VSIX Project template also on the Extensibility node in the New Project dialog.)<\/li>\n<li>When the VSIX project is created, you should see the editor for your source.extension.vsixmanifest file appear. Click the \u201cAdd Content\u201d button to specify that the template file you created in steps 1-2 should be included in the VSIX file built by this project.\n<ul>\n<li>Select \u201cProject Template\u201d for the content type<\/li>\n<li>For the source, select \u201cProject\u201d and then choose the \u201cMyTemplate\u201d project.<\/li>\n<li>Click OK<\/li>\n<\/ul>\n<\/li>\n<li>Save and close the VSIX manifest editor<\/li>\n<li>Right click the \u201cMyTemplateSetup\u201d project and select \u201cSet as StartUp Project\u201d to debug your template in the Visual Studio Experimental instance.<\/li>\n<li>Press F5 to start debugging (or CTRL + F5 to launch without the debugger).<\/li>\n<li>Show the New Project dialog in the Visual Studio and see your template listed under \u201cVisual C# Windows&#8221; (if you added the OutputSubPath from step 2).<\/li>\n<\/ol>\n<p>There are a few other interesting points about the support in the SDK for building templates:<\/p>\n<ul>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms185308.aspx\">Multi-project templates<\/a> are supported<\/li>\n<li>If you are building a collection of templates, you can include them all in one VSIX file by repeating the steps above (skipping step 3).<\/li>\n<\/ul>\n<p>While not as simple as the \u201cExport Template as VSIX\u201d wizard, this technique allows you to build templates and deliver them using the VSIX format while still keeping control over the template source files.<\/p>\n<p><b>Aaron Marten &#8211;<\/b> Developer, Visual Studio Platform<\/p>\n<p><b>Short Bio<\/b>: Aaron has been on the Visual Studio team for 6 years with a focus on extensibility. For Visual Studio 2010, Aaron worked on the Extension Manager and the Visual Studio SDK. In addition to contributing to the Visual Studio blog, he also writes about extending the IDE on his own blog at <a href=\"https:\/\/learn.microsoft.com\/en-us\/archive\/blogs\/aaronmar\/\">https:\/\/learn.microsoft.com\/en-us\/archive\/blogs\/aaronmar\/<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the great new features we added in Visual Studio 2010 is the ability to easily find and consume online Project and Item templates without leaving the New Project\/Item dialog. What may not be so obvious is how to go from a piece of code you want to share to a template contained in [&hellip;]<\/p>\n","protected":false},"author":25,"featured_media":255385,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[155],"tags":[5,9,294,185,653,13],"class_list":["post-2383","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-visual-studio","tag-csharp","tag-debug","tag-extensions","tag-node-js","tag-visual-basic","tag-visual-studio-2010"],"acf":[],"blog_post_summary":"<p>One of the great new features we added in Visual Studio 2010 is the ability to easily find and consume online Project and Item templates without leaving the New Project\/Item dialog. What may not be so obvious is how to go from a piece of code you want to share to a template contained in [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/2383","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/users\/25"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/comments?post=2383"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/2383\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/media\/255385"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/media?parent=2383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/categories?post=2383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/tags?post=2383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}