I've followed this tutorial to try to add a template to Visual Studio:
http://www.switchonthecode.com/tutorials/visual-studio-how-to-create-item-templates
The template I have created is designed to add one predefined .aspx and one predefined .aspx.cs file to the project.
The folder contains the following files:
MoosePage.aspx
MoosePage.aspx.cs
MoosePage.vstemplate
MoosePageItemTemplateIcon.ico
The .vstemplate file looks like this:
<VSTemplate Type="Item" Version="2.0.0"
xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
<TemplateData>
<Name>MoosePage</Name>
<Description>MoosePage Template</Description>
<DefaultName>NewMoosePage</DefaultName>
<ProjectType>CSharp</ProjectType>
<Icon>MoosePageItemTemplateIcon.ico</Icon>
</TemplateData>
<TemplateContent>
<ProjectItem TargetFileName="$fileinputname$.aspx" ReplaceParameters="true">
MoosePage.aspx
</ProjectItem>
<ProjectItem TargetFileName="$fileinputname$.aspx.cs" ReplaceParameters="true">
MoosePage.aspx.cs
</ProjectItem>
</TemplateContent>
</VSTemplate>
I have zipped the files up (.zip not .zipx) and placed the zip folder in My Documents\Visual Studio 2008\Templates\ItemTemplates\VisualWebDeveloper.
I have restarted Visual Studio.
When I go into my website project and choose Add New Item, I don't see my new template.
Can anyone suggest what might have gone wrong?
Thanks
David
I have found out there is a wizard to create templates in File -> Export Template.
Using this wizard, I found out that my .vstemplate file had the wrong ProjectType. I changed it to this...
<VSTemplate Type="Item" Version="2.0.0"
xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
<TemplateData>
<Name>MoosePage.aspx</Name>
<Description>MoosePage Template</Description>
<DefaultName>NewMoosePage</DefaultName>
<ProjectType>Web</ProjectType>
<ProjectSubType>CSharp</ProjectSubType>
<Icon>MoosePageItemTemplateIcon.ico</Icon>
</TemplateData>
<TemplateContent>
<ProjectItem TargetFileName="$fileinputname$.aspx" ReplaceParameters="true">
MoosePage.aspx
</ProjectItem>
<ProjectItem TargetFileName="$fileinputname$.aspx.cs" ReplaceParameters="true">
MoosePage.aspx.cs
</ProjectItem>
</TemplateContent>
</VSTemplate>
And now it works fine.
I can also confirm that the new .zipx format is NOT supported.
Now to wait two years before I can mark my own answer as correct.
Cheers
David
Related
I've been trying to make my own template in VS for C++.
I want to have a cpp and hpp file with the same name as the project.
I have already read this link: How can I set a file name using a variable in a Visual Studio project template
Howerer, when using my template, the files are not in the project that I've created.
Here is my vstemplate file:
<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Project">
<TemplateData>
<Name>My Template</Name>
<Description>A test template</Description>
<ProjectType>VC</ProjectType>
<ProjectSubType>
</ProjectSubType>
<SortOrder>1000</SortOrder>
<CreateNewFolder>true</CreateNewFolder>
<DefaultName>MyProject</DefaultName>
<ProvideDefaultName>true</ProvideDefaultName>
<LocationField>Enabled</LocationField>
<EnableLocationBrowseButton>true</EnableLocationBrowseButton>
<Icon>__TemplateIcon.png</Icon>
<PreviewImage>__PreviewImage.png</PreviewImage>
</TemplateData>
<TemplateContent>
<Project TargetFileName="MyTemplate.vcxproj" File="MyTemplate.vcxproj" ReplaceParameters="true">
<ProjectItem ReplaceParameters="true" TargetFileName="$safeprojectname$.vcxproj.filters">MyTemplate.vcxproj.filters</ProjectItem>
<ProjectItem ReplaceParameters="true" TargetFileName="$safeprojectname$.cpp">MyFile.cpp</ProjectItem>
<ProjectItem ReplaceParameters="true" TargetFileName="$safeprojectname$.hpp">MyFile.hpp</ProjectItem>
</TemplateContent>
</VSTemplate>
What am I doing wrong?
Edit: Managed to figure it out, had to replace file names in vcxproj file itself. Thanks for helping!
I ended up succeeding using the following method:
I added two files to my template, named $safeprojectname$.hpp and $safeprojectname$.cpp.
I edited my vcxproj file (using notepad++, or any other text editor for that matter), adding the following tags inside of the main Project tag:
<ItemGroup>
<ClInclude Include="%24safeprojectname%24.hpp" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="%24safeprojectname%24.cpp" />
</ItemGroup>
I had to use %24 as $ is not a valid character in this context, and 24 is the ASCII (/unicode) value of that character.
I'm using visual studio templates in order to build a custom visual studio template project. I'm trying to create a folder with a name «External Models» that has space char but the visual studio is creating a folder with the name «External%20Models» replacing the space by %20.
Follows my .vstemplate file:
<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Project">
<TemplateData>
...
</TemplateData>
<TemplateContent>
<Project TargetFileName="Product.Models.csproj" File="Product.Models.csproj" ReplaceParameters="true">
...
<Folder Name="GeneratedCode" TargetFolderName="GeneratedCode">
...
</Folder>
<Folder Name="External Models" TargetFolderName="External Models">
<Folder Name="CorePatterns" TargetFolderName="CorePatterns">
...
</Folder>
</Folder>
</Project>
</TemplateContent>
<WizardExtension>
...
</WizardExtension>
And the content of the .vsix files generated:
I changed from this:
<Folder Name="External Models" TargetFolderName="External Models">
<Folder Name="CorePatterns" TargetFolderName="CorePatterns">
...
</Folder>
</Folder>
To this, and start to work.
<Folder Name="ExternalModels" TargetFolderName="External Models">
<Folder Name="CorePatterns" TargetFolderName="CorePatterns">
...
</Folder>
</Folder>
Explanation:
It does not matter which folders are generated to create the .vsix installer. What is matter is TargetFolderName="External Models" tag name which will generate the project folder (during the Visual Studio->File->Create new project). By changing the Name="External Models" to Name="ExternalModels" and, in the template project that generate the .vsix, change the name of the folder "External Models" to "ExternalModels" everithing will work fine.
I am trying to make a project template for internal use that has all of the custom parts of our assembly versioning system.
The default project template works perfectly with our pre-build and post-build event but when I try to reference a file using the $(SolutionDir) macro it gets replaced during the building of the template and
<ItemGroup>
<Compile Include="$(SolutionDir)\CommonAssemblyInfo.cs">
<Link>Properties\CommonAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Gets turned into the following when the project template is used to generate a project in solution:
<ItemGroup>
<Compile Include="..\..\..\..\..\Users\<username>\AppData\Local\Temp\n0u03z2x.gpe\CommonAssemblyInfo.cs">
<Link>Properties\CommonAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
The template build/package process is probably replacing this.
How do I escape it ?
Visual studio project template replaces “$(SolutionDir)” macro in template on build
You should set CreateInPlace in the .vstemplate file of the exported project template to true, like <CreateInPlace>true</CreateInPlace>.
Following is my .vstemplate file:
<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Project">
<TemplateData>
<Name>TestDemo</Name>
<Description><No description available></Description>
<ProjectType>CSharp</ProjectType>
<ProjectSubType>
</ProjectSubType>
<SortOrder>1000</SortOrder>
<CreateNewFolder>true</CreateNewFolder>
<DefaultName>TestDemo</DefaultName>
<ProvideDefaultName>true</ProvideDefaultName>
<LocationField>Enabled</LocationField>
<EnableLocationBrowseButton>true</EnableLocationBrowseButton>
<Icon>__TemplateIcon.ico</Icon>
<CreateInPlace>true</CreateInPlace>
</TemplateData>
<TemplateContent>
<Project TargetFileName="TestDemo.csproj" File="TestDemo.csproj" ReplaceParameters="true">
<ProjectItem ReplaceParameters="true" TargetFileName="Class1.cs">Class1.cs</ProjectItem>
<Folder Name="Properties" TargetFolderName="Properties">
<ProjectItem ReplaceParameters="true" TargetFileName="AssemblyInfo.cs">AssemblyInfo.cs</ProjectItem>
</Folder>
</Project>
</TemplateContent>
</VSTemplate>
Detailed steps:
Export the project template without Automatically import the template into Visual Studio:
Unzip the exported file, add <CreateInPlace>true</CreateInPlace> into the MyTemplate.vstemplate file, and zip all the files.
Copy this file to the folder: C:\Users\<Username>\Documents\Visual Studio 2017\Templates\ProjectTemplates
Restart your Visual Studio, create a new project with the new custom template.
With this setting, this issue should be fix. But the weird thing is that the documentation states that the default value for CreateInPlace is true.
Hope this helps.
We are trying to create an item template which consists of 3 files in 1 project and 1 file in another project (Both projects are in the same solution)
I can successfully generate a template for the 3 files in one project and then a seperate template for the other file but i cannot seem to find a way to combine these so when i add the template it adds files to the 2 projects.
Template 1 (in first project)
<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Item">
<TemplateData>
<DefaultName>Template.ascx</DefaultName>
<Name>Template Test</Name>
<Description></Description>
<ProjectType>CSharp</ProjectType>
<SortOrder>10</SortOrder>
<Icon>__TemplateIcon.ico</Icon>
</TemplateData>
<TemplateContent>
<References />
<ProjectItem SubType="" TargetFileName="/Areas/Design/Views/Shared/EditorTemplates/$fileinputname$.ascx" ReplaceParameters="true">SampleControlDesign.ascx</ProjectItem>
<ProjectItem SubType="" TargetFileName="/Areas/Submit/Views/Shared/EditorTemplates/$fileinputname$.ascx" ReplaceParameters="true">SampleControlSubmit.ascx</ProjectItem>
<ProjectItem SubType="" TargetFileName="/Areas/Submit/Views/Render/$fileinputname$.ascx" ReplaceParameters="true">SampleControlRender.ascx</ProjectItem>
</TemplateContent>
</VSTemplate>
Template 2 (in second project)
<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Item">
<TemplateData>
<DefaultName>Template2.cs</DefaultName>
<Name>Template 2</Name>
<Description>Template for the creation of a controls domain model</Description>
<ProjectType>CSharp</ProjectType>
<SortOrder>10</SortOrder>
<Icon>__TemplateIcon.ico</Icon>
</TemplateData>
<TemplateContent>
<References />
<ProjectItem SubType="" TargetFileName="/Models/Controls/$fileinputname$.cs" ReplaceParameters="true">SampleControl.cs</ProjectItem>
</TemplateContent>
</VSTemplate>
I am creating a project that I will export as a Project Template, using this and this as guidance.
These are to be Sharepoint 2010 projects created using Visual Studio 2010.
What I would really like to have happen is that the user be prompted for several values (above and beyond just the solution name) when they start a new Project and select the Template I will create. I want to let them change the default names of the WebPart and the User Control from the outset, for example.
I am getting mixed signals, though, on whether this is really possible. The code below from here seems to indicate that the user/developer needs to add CustomParameters to the .vstemplate file, and then these values are what are read to replace those placeholder values. This is the "official" (MS example) code:
<TemplateContent>
...
<CustomParameters>
<CustomParameter Name="$MyParameter1$" Value="MyValue1"/>
<CustomParameter Name="$MyParameter2$" Value="MyValue2"/>
</CustomParameters>
</TemplateContent>
But if that's what really has to be done, I don't see any benefit over simply putting "TODOs" in the code, and having the users/developers do a global search & replace after opening the project.
So I'd like a "sanity check" from those in the know as to wheter this is what I need to do:
Is this what I have to do:
0) Create the project
1) Export it as a project template
2) Alter the .vstemplate file, adding something like the code above
?
That certainly seems to be the steps the "How To" is recommending. My main question is, will doing that prompt user developers of the Template for the replacement values (overwriting "MyValue1" and "MyValue2"), or will the user developers have to modify the *.vstemplate file to add the replacement vaues directly? If the latter, I don't see the benefit - locating and altering the *.vstemplate file prior to creating a new project based on the template seems far kludgier than adding "TODO" messages in the code to replace this and that value.
IOW, does the user/developer have to open the *.vstemplate file, replacing both the "Name"s and "Value"s with what he wants, such as changing something like this:
<CustomParameters>
<CustomParameter Name="$title$" Value="Replace this placeholder title"/>
<CustomParameter Name="$description$" Value="Replace this placeholder description"/>
</CustomParameters>
...to something like this:
<CustomParameters>
<CustomParameter Name="$title$" Value="A Connecticut Yankee in King Arthur's Court"/>
<CustomParameter Name="$description$" Value="Hammerin' Hank Morgan Le Fay Vincent Van Gogh Fly a Kite Runner"/>
</CustomParameters>
(assuming the template as saved by the original developer (me) has added replaceable params in the project named "$title$" and "$description$"?
For the curious who do not want to export a project as a template to see what such a *.vstemplate file might contain, here is a pretty generic one, in full:
<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Project">
<TemplateData>
<Name>SPWFTemplate</Name>
<Description>This is for starting projects to create WebForms based on existing PDFs</Description>
<ProjectType>CSharp</ProjectType>
<ProjectSubType>
</ProjectSubType>
<SortOrder>1000</SortOrder>
<CreateNewFolder>true</CreateNewFolder>
<DefaultName>SPWFTemplate</DefaultName>
<ProvideDefaultName>true</ProvideDefaultName>
<LocationField>Enabled</LocationField>
<EnableLocationBrowseButton>true</EnableLocationBrowseButton>
<Icon>__TemplateIcon.ico</Icon>
</TemplateData>
<TemplateContent>
<Project TargetFileName="SPWFTemplate.csproj" File="SPWFTemplate.csproj" ReplaceParameters="true">
<Folder Name="Features" TargetFolderName="Features">
<Folder Name="Feature1" TargetFolderName="Feature1">
<ProjectItem ReplaceParameters="false" TargetFileName="Feature1.feature">Feature1.feature</ProjectItem>
<ProjectItem ReplaceParameters="true" TargetFileName="Feature1.Template.xml">Feature1.Template.xml</ProjectItem>
</Folder>
</Folder>
<Folder Name="Package" TargetFolderName="Package">
<ProjectItem ReplaceParameters="false" TargetFileName="Package.package">Package.package</ProjectItem>
<ProjectItem ReplaceParameters="true" TargetFileName="Package.Template.xml">Package.Template.xml</ProjectItem>
</Folder>
<Folder Name="Properties" TargetFolderName="Properties">
<ProjectItem ReplaceParameters="true" TargetFileName="AssemblyInfo.cs">AssemblyInfo.cs</ProjectItem>
<ProjectItem ReplaceParameters="false" TargetFileName="key.snk">key.snk</ProjectItem>
</Folder>
<Folder Name="SPWFTemplateVisualWebPart" TargetFolderName="SPWFTemplateVisualWebPart">
<ProjectItem ReplaceParameters="true" TargetFileName="Elements.xml">Elements.xml</ProjectItem>
<ProjectItem ReplaceParameters="false" TargetFileName="SharePointProjectItem.spdata">SharePointProjectItem.spdata</ProjectItem>
<ProjectItem ReplaceParameters="true" TargetFileName="SPWFTemplateVisualWebPart.cs">SPWFTemplateVisualWebPart.cs</ProjectItem>
<ProjectItem ReplaceParameters="false" TargetFileName="SPWFTemplateVisualWebPart.webpart">SPWFTemplateVisualWebPart.webpart</ProjectItem>
<ProjectItem ReplaceParameters="true" TargetFileName="SPWFTemplateVisualWebPartUserControl.ascx">SPWFTemplateVisualWebPartUserControl.ascx</ProjectItem>
<ProjectItem ReplaceParameters="true" TargetFileName="SPWFTemplateVisualWebPartUserControl.ascx.cs">SPWFTemplateVisualWebPartUserControl.ascx.cs</ProjectItem>
<ProjectItem ReplaceParameters="true" TargetFileName="SPWFTemplateVisualWebPartUserControl.ascx.designer.cs">SPWFTemplateVisualWebPartUserControl.ascx.designer.cs</ProjectItem>
</Folder>
</Project>
</TemplateContent>
</VSTemplate>