Robert S. Robbins - Williamsport Web Developer
Home Page
Robert S. Robbins Blog
Client List
Asynchronous JavaScript and XML
Robert S. Robbins Technical Skills
aspPrint Server Component
Database Guru
FrontPage VBA
Active Server Pages Programming
ASP.NET Programming
Network Administration
PC Maintenance
Web Development
StoreFront Customization
Robert S. Robbins - Williamsport Web Developer News
click to read more!Blog
click to read more!Visual Studio 2005 - Item Templates

Visual Studio 2005 - Item Templates
Export Template

You need to customize the toolbar to add Export Template... to the File menu.



The wizard will prompt you to indicate the item to export and any assembly references to include. Adding references allows you to ensure that assemblies your template depends on are included in the project when the item is added. In addition to the assembly references, the export wizard is intelligent enough to export resource files required by the item be exported. The wizard also edits these files to add placeholders for class names and namespaces so that when they are added to a new project they can be named differently and show up in the correct code namespace.

Creates a file named HelpTopicTemplate.zip in the directory C:\Documents and Settings\rsrobbins\My Documents\Visual Studio 2005\My Exported Templates. Templates in this folder are not included in the New Item or New Project dialog boxes by default. In order to make the template available for use, it must be installed by copying it to the configured directory for templates. Checking the box to install the template simply means that the ZIP file will get copied to the folder you configured for templates in addition to the exported templates folder.



Zip file contains three files: topic-template.htm, MyTemplate.vstemplate, and __TemplateIcon.ico


New item now appears under My Templates.


Customizing Templates

The template metadata file is an XML file that describes the template and indicates the files and resources to be included in with the project or item.


<VSTemplate Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Item">

  <TemplateData>

    <DefaultName>HelpTopicTemplate.htm</DefaultName>

    <Name>HelpTopicTemplate</Name>

    <Description>Help Topic Template</Description>

    <ProjectType>CSharp</ProjectType>

    <SortOrder>10</SortOrder>

    <Icon>__TemplateIcon.ico</Icon>

  </TemplateData>

  <TemplateContent>

    <References />

    <ProjectItem SubType="" TargetFileName="$fileinputname$.htm" ReplaceParameters="true">topic-template.htm</ProjectItem>

  </TemplateContent>

</VSTemplate>

The first thing to notice is the general structure of this metadata, which includes a root VSTemplate node and two child nodes: TemplateData and TemplateContent. The VSTemplate node simply acts as a wrapper for the other two items and indicates the type of the template. Because of the XML namespace declaration in the file, any developer editing this file in Visual Studio 2005 will automatically get IntelliSense® support for the items that are allowed, based on the schema for template files.

The TemplateData element contains information that is used to provide details about the template in the New Project or New Item dialog box. The name, description, and icon elements are all displayed directly in the dialog box as you would expect. The DefaultName element is used as the root of the default name provided for the new item or project. The ProjectType element indicates the type of project that the item can be included in and affects the location in the dialog box.

Built-In Replacement Parameters
Parameters Description
itemname The user-provided name from the dialog. This value is often used for naming classes and files.
safeitemname The same as itemname but with all unsafe characters removed.
safeitemrootname The name of the root item, which can be used for multi-item templates. For example, if you have partial classes, or a code-beside or code-behind scenario, you can use this item in the secondary files when you need the safeitemname of the main item being created.
projectname The user-provided name of the project for a project template.
safeprojectname The user-provided name of the project for a project template with all unsafe characters removed.
rootnamespace The root namespace of the project for an item template, which can be used to replace the namespace in a project item source file.
guid[1-10] A GUID that can be used as a unique identifier for uses such as project GUID in the project file. You can specify up to 10 different GUID parameters using the syntax: guidx, where x is a number between 1 and 10.
time The current time in the format DD/MM/YYYY HH:MM:SS.
year The current four-digit year.
username The Windows username of the logged-in user.
userdomain The Windows domain of the logged-in user.
machinename The name of the machine where the template is being used.
clrversion The version of the common language runtime being used.
registered-organization The registered organization of the user based on the system settings.
wizarddata A single string that can be any XML or string data included in the WizardData element in a template metadata file.

You can also add custom parameters:


<TemplateContent>

  <CustomParameters>

	  <CustomParameter 

	      Name="$todaysdate$" 

	      Value="June 15, 2007" />

	</CustomParameters>

</TemplateContent>

Each item in the ZIP file that is to be included in the project is called out with a ProjectItem element. In addition to identifying the source files, the attributes of the ProjectItem element allow you to indicate whether an item has a subtype, whether parameters in the file should be replaced, and whether the target file name differs from the source file name. The subtype attribute can include values such as Form or Component, which provide Visual Studio with hints about how to display the item in the correct designer when it is opened.


<VSTemplate Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Item">

  <TemplateData>

    <DefaultName>HelpTopicTemplate.htm</DefaultName>

    <Name>HelpTopicTemplate</Name>

    <Description>Help Topic Template</Description>

    <ProjectType>CSharp</ProjectType>

    <SortOrder>10</SortOrder>

    <Icon>__TemplateIcon.ico</Icon>

  </TemplateData>

  <TemplateContent>

    <References />

    <ProjectItem SubType="" TargetFileName="$fileinputname$.htm" ReplaceParameters="true">topic-template.htm</ProjectItem>

    <ProjectItem SubType="" TargetFileName="Notes.css" ReplaceParameters="false">Notes.css</ProjectItem>

		<CustomParameters>

		  <CustomParameter 

		      Name="$author$" 

		      Value="Robert Robbins" />

		</CustomParameters>

  </TemplateContent>

</VSTemplate>

Extending the Wizard

If all the extensibility and parameter replacement is not enough to meet your needs and you really have to write some code to provide the dynamic data you need for your template, you can write a wizard extension and configure it in the template metadata. With a wizard extension, you can augment the standard New Project or New Item wizard with your own code and add dynamic custom parameters for replacement in the project files.

1. Copy DLL file to C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin and run gacutil.exe -if DynCustomParams.dll

Failure adding assembly to the cache: Attempt to install an assembly without a strong name

2. Create a strong name key file using the strong name utility:

C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin>sn -k MyKey.snk



Microsoft (R) .NET Framework Strong Name Utility Version 2.0.50727.42

Copyright (c) Microsoft Corporation. All rights reserved.



Key pair written to MyKey.snk

3. Use the strong key file to sign the assembly in the project

4. Attempt to add the assembly to the cache again

C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin>gacutil.exe -if DynCustomParams.dll

Microsoft (R) .NET Global Assembly Cache Utility. Version 2.0.50727.42

Copyright (c) Microsoft Corporation. All rights reserved.



Assembly successfully added to the cache



C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin>

5. If you get an untrusted component error make sure you did not use .dll in the Assembly tag.

<WizardExtension>

	<Assembly>DynCustomParams</Assembly>

	<FullClassName>DynCustomParams.TemplateParams</FullClassName>

</WizardExtension>

You can put the assembly in the Public Asssemblies or Private Assemblies folder inside the IDE installation folder and try that. In that case you will not need the full assembly name, just its simple name, and VS should be able to find it. Restart Visual Studio 2005 after copying the DLL file.

C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PrivateAssemblies

6. If you get a "The method of operation is not implemented" error then you did not remove the code from a method in the interface stub.


Extending the Wizard

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.VisualStudio.TemplateWizard;

using EnvDTE;



namespace DynCustomParams

{

    /// <summary>

    /// Dynamic custom parameters for replacement. To be used in Item Templates. 

    /// </summary>

    /// <remarks>Requires adding references to Microsoft.VisualStudio.TemplateWizard and EnvDTE</remarks>

    public class TemplateParams : IWizard

    {

        private DTE dteObject;



        #region IWizard Members



        public void BeforeOpeningFile(ProjectItem projectItem)

        {

        }



        public void ProjectFinishedGenerating(Project project)

        {

            this.dteObject = project.DTE;

        }



        public void ProjectItemFinishedGenerating(ProjectItem projectItem)

        {

        }



        public void RunFinished()

        {

        }



        public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)

        {

            // Replace parameters can be added dynamically in IWizard.RunStarted

            string strMonth = "";

            DateTime dtToday = DateTime.Now;

            switch (dtToday.Month)

            {

                case 1:

                    strMonth = "January";

                    break;

                case 2:

                    strMonth = "February";

                    break;

                case 3:

                    strMonth = "March";

                    break;

                case 4:

                    strMonth = "April";

                    break;

                case 5:

                    strMonth = "May";

                    break;

                case 6:

                    strMonth = "June";

                    break;

                case 7:

                    strMonth = "July";

                    break;

                case 8:

                    strMonth = "August";

                    break;

                case 9:

                    strMonth = "September";

                    break;

                case 10:

                    strMonth = "October";

                    break;

                case 11:

                    strMonth = "November";

                    break;

                case 12:

                    strMonth = "December";

                    break;

            }

            string strTodaysDate = strMonth + " " + dtToday.Day + ", " + dtToday.Year;

            replacementsDictionary.Add("$today$", strTodaysDate);

        }



        public bool ShouldAddProjectItem(string filePath)

        {

            return true;

        }



        #endregion

    }

}

MyTemplate.vstemplate

<VSTemplate Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Item">

  <TemplateData>

    <DefaultName>HelpTopicTemplate.htm</DefaultName>

    <Name>HelpTopicTemplate</Name>

    <Description>Help Topic Template</Description>

    <ProjectType>CSharp</ProjectType>

    <SortOrder>10</SortOrder>

    <Icon>__TemplateIcon.ico</Icon>

  </TemplateData>

  <TemplateContent>

    <References />

    <ProjectItem SubType="" TargetFileName="$fileinputname$.htm" ReplaceParameters="true">topic-template.htm</ProjectItem>

    <ProjectItem SubType="" TargetFileName="Notes.css" ReplaceParameters="false">Notes.css</ProjectItem>

    <CustomParameters>

		  <CustomParameter 

		      Name="$author$" 

		      Value="Robert Robbins" />

		</CustomParameters>

  </TemplateContent>

  <WizardExtension>

        <Assembly>DynCustomParams</Assembly>

        <FullClassName>

        DynCustomParams.TemplateParams

        </FullClassName>

    </WizardExtension>

</VSTemplate>
Copyright © 2007. Robert S. Robbins.  |  Privacy Policy  |  Terms Of Use  |  Contact

web design by Robert S. Robbins