VS2019 Custom template not shown - visual-studio

I have created a custom template for VS2019 that contains two projects. Below is my root.vstemplate file
<VSTemplate Version="3.0.0" Type="ProjectGroup" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
<TemplateData>
<Name>Asp.Net Core Template EF Core</Name>
<Description>Asp.Net Core 3.1 Boilerplate template</Description>
<ProjectType>CSharp</ProjectType>
<ProjectSubType>
</ProjectSubType>
<SortOrder>1000</SortOrder>
<CreateNewFolder>true</CreateNewFolder>
<DefaultName>Asp.Net Core Template EF Core</DefaultName>
<ProvideDefaultName>true</ProvideDefaultName>
<LocationField>Enabled</LocationField>
<EnableLocationBrowseButton>true</EnableLocationBrowseButton>
<Icon>logo.png</Icon>
</TemplateData>
<TemplateContent>
<ProjectCollection>
<ProjectTemplateLink ProjectName="AspNet Core Ef Core">
src\AspNetCore3EfCoreTemplate\MyTemplate.vstemplate
</ProjectTemplateLink>
<ProjectTemplateLink ProjectName="AspNet Core Ef Core Web">
src\AspNetCore3EfCoreTemplate.Web\MyTemplate.vstemplate
</ProjectTemplateLink>
</ProjectCollection>
</TemplateContent>
</VSTemplate>
I followed the instructions of this link but my template is not shown when I try to create a new item in VS2019. Anyone knows why is this happening?

I followed the instructions of this link but my template is not shown
when I try to create a new item in VS2019. Anyone knows why is this
happening?
For the custom project template, the new VS2019 UI for creating projects cannot find custom project templates through filters and these templates actually exist in it.
All Langages, All Platforms and All Project Types and the search box lose the ability to filter custom project templates and this is a known issue in VS2019. But it still happens in the latest version VS2019 16.4.2. If you want to solve this annoying problem, you can report an issue to DC Forum to report this abnormal behavior for support. This might be a long process or you can try my suggestions below:
Suggestion
1) Do not use any filters in VS2019 Project UI and then drag to the bottom of all templates to find the custom template, which is usually inserted after all the default templates.
This picture shows it at the bottom of all templates.
2) change to use VS2017 which has no error about this.
Update 1
l have created with your further info and test in my side without any errors, my environment:
win10 enterprise 1903; VS2019 Community 16.4.2
My Steps:
1) l have created two projects into a same solution:
a Net Core 3.1 ClassLibrary project and a ASP.NET Core 3.1 Web Application project.
2) then click Export Template.. in the Project menu and export these projects as project template separately.
3) open the folder and unzip them into the same folder called src.
This shows the two unzip project folders in the src folder
Then, this shows the final form of the folder,and src folder contains the two project folders as the picture shows above
4) root.vstemplate is the code you provide. The project Template is called Asp.Net Core Template EF Core as you said. After that, I compress these two files into a package called multiple projects template,and then put this zip file into this path C:\Users\xxx\Documents\Visual Studio 2019\Templates\ProjectTemplates.
5) then I close VS Instance and then restart VS and clear all the filters and then pull the project templates to the bottom, and then look up at the bottom for a template called Asp.Net Core Template EF Core.
> l have found this template in my side and if you does not see this, please repair VS and then disable any third-party extensions in the Extension-->Manage Extension menu to prevent interference with VS. Besides, since your VS is 16.4.1, you could try to update it to 16.4.2.
Update 2
With your sample, l think l found the issue. The issue is that root.template cannot find the two contained projects:
You put root.template in the Src folder and the root.vstemplate file write these:
src\ClassLibrary1\MyTemplate.vstemplate
src\WebApplication3\MyTemplate.vstemplate
It uses relative paths to find these files and the current path where your root.vstemplate is located does not have src so it cannot find.
The solution is that you should put root.vstemplatein the same directory as the SRC folder and then compress these two. After that, put this Multiple.zip into C:\Users\xxx\Documents\Visual Studio 2019\Templates\ProjectTemplates.
Hope it could help you.

Visual Studio 2019 uses tags to filter projects. By default when you export a project template, the .vstemplate file doesn't contain the required tags. You need to edit the .vstemplate file and add the following tags in the TemplateData section (I.e. between <TemplateData> and </TemplateData>) and replace the values with the appropriate values for your template:
<LanguageTag>Language</LanguageTag>
<PlatformTag>Platform</PlatformTag>
<ProjectTypeTag>Type</ProjectTypeTag>
For example, here is the TemplateData for one of my custom templates:
<TemplateData>
<Name>MyCaliburnMicroTemplate</Name>
<Description>Caliburn Micro template I set up with basic localisation, logging, settings and themes implemented.</Description>
<ProjectType>CSharp</ProjectType>
<ProjectSubType>Windows</ProjectSubType>
<LanguageTag>CSharp</LanguageTag>
<PlatformTag>Windows</PlatformTag>
<ProjectTypeTag>Desktop</ProjectTypeTag>
<SortOrder>1000</SortOrder>
<CreateNewFolder>true</CreateNewFolder>
<DefaultName>MyCaliburnMicroTemplate</DefaultName>
<ProvideDefaultName>true</ProvideDefaultName>
<LocationField>Enabled</LocationField>
<EnableLocationBrowseButton>true</EnableLocationBrowseButton>
<Icon>__TemplateIcon.ico</Icon>
</TemplateData>
I also set the <ProjectSubType> to Windows, but I haven't tested to see if this makes any difference.
Here's a picture of my 'Create a new project' window with the filters set to C#, Windows and Desktop:
Notice the tags now appear below the description of my project template. I'm currently using Visual Studio Community 2019 version 16.4.3, but haven't tested this with any other version.

Maybe this will help.
https://blog.jayway.com/2015/03/13/visual-studio-how-to-create-a-solution-template-with-multiple-projects/
The original documentation usually miss a step or 2 that looks obvious after you made a lots of templates.

This is what I did to make it work:
After the zip file of the template was created, I extracted it.
Then I copied the folder to the "ProjectTamplates" folder.
I was able to create a new project based on my template this way.

I was struggling with same issue, that my project does not apper in search window of projects in VS.
In folder "ProjectTemplates" I had zip that was containing dots in name. After removing them my project appeared in projects window in VS.
I hope this will help someone.

Related

Is it possible to configure a csproj file to allow any project item type?

I'm working on a unique project in that I would like to be able to have CSHTML Razor Views, Windows Forms, and various other "non project type crossing" project item templates in one single project in visual studio.
Is there a way I can override the project type so that it compiles as a Windows Form's project but still allows me to add CSHTML files and other template types to it?
When you edit the .csproj using notepad or an xml editor you'll find that <ProjectTypeGuids> element.
You can then edit it to have an entry like below
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
Note: these are Guids from a portable class library project. You need to find correct ProjectTypeGuids for your requirements and add it.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/d9d05cdc-96a1-4044-95d8-a4f8885a660a/what-is-the-significance-of-projecttypeguids-tag-in-the-visual-studio-project-file?forum=vsx

How to define a template for adding multiple files to a project?

My new project is my first look at WPF MVVM and WCF and I like it but it seems like I am creating a lot of files, always in the same basic setup and structure.
I am wondering if anyone has a way of defining some kind of folder/project structure as an input and automatically creating the various POCOs, views, models, service classes and interfaces, perhaps with some kind of consistent prefix for the file name.
Then the developer can just go in and cut the code to get their data.
I saw this, which is sort of the right idea:
http://www.codeproject.com/Articles/16515/Creating-a-Custom-Tool-to-Generate-Multiple-Files
A colleague also suggested a batch file might be worth investigating. Open to all ideas, thanks for your help.
UPDATE //
This would take place after the project was created. So the folders and projects are already in place but you want to add the necessary files for an end-to-end service call and presentation.
You can use Visual Studio's Item Template not only to add a single file, but also to add multiple items and sort them in your project folder structure:
If you create an item template with VS, via File -> Export Template, you get a zipped folder. You can unzip it and add several files, for example until it looks like this:
You can then modify the file named 'MyTemplate'. The according file for the above example looks like this:
<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Item">
<TemplateData>
<DefaultName>Enter Module Name here</DefaultName>
<Name>INCA Module</Name>
<Description>Creates all files for a module - Add this item at project root level only!</Description>
<ProjectType>CSharp</ProjectType>
<SortOrder>10</SortOrder>
<Icon>__TemplateIcon.ico</Icon>
</TemplateData>
<TemplateContent>
<References />
<ProjectItem SubType="Code" TargetFileName="Models/$fileinputname$.cs" ReplaceParameters="true">ElementModel.cs</ProjectItem>
<ProjectItem SubType="Code" TargetFileName="ViewModels$fileinputname$ViewModel.cs" ReplaceParameters="true">ElementViewModel.cs</ProjectItem>
<ProjectItem SubType="Designer" TargetFileName="$fileinputname$View.xaml" ReplaceParameters="true">View.xaml</ProjectItem>
<ProjectItem SubType="Code" TargetFileName="$fileinputname$View.xaml.cs" ReplaceParameters="true">View.xaml.cs</ProjectItem>
<ProjectItem SubType="Designer" TargetFileName="Metadata/$fileinputname$Metadata.xaml" ReplaceParameters="true">Metadata.xaml</ProjectItem>
<ProjectItem SubType="Code" TargetFileName="Metadata/$fileinputname$Metadata.xaml.cs" ReplaceParameters="true">Metadata.xaml.cs</ProjectItem>
</TemplateContent>
</VSTemplate>
I think, what happens here is quite self explanatory.
It is very helpful to use template parameters:
MSDN on Template Parameters
Then just zip the whole thing again and place it in the folder User/Visual Studio xy/Templates. Note, that templates are exported to the folder called MyExportedTemplates.
I remember vs2010 can export your project to template, so you can encapsulate your folders or data in project and export to template.
Atfer you finished that, you can create your custom project from template.

How do I add Same Solution Project References when exporting Templates in Visual Studio?

I have a solution that contains several projects, lets call them ProjectA and ProjectB. I need ProjectA to have a reference to ProjectB, however when I "Export Template" I can't seem to keep the reference. What I am trying to do is to have it create the reference for me when I create a new project and then automatically name it to what I entered.
This is slightly out of scope for pure Visual Studio templates.
Visual Studio templates supports replacements parameters for templates, so what you could do is:
Inside your ProjectA.zip template file you will find ProjectA.csproj, which is the template for the project
In ProjectA.csproj you will find an <ItemGroup> containing <Reference> entries
Among them should be an entry like <Reference Include="ProjectB">
When you create a project from your ProjectB-template, it's name will be user-specified, so you want to replace ProjectB with a replacement parameter like $Foo$.
This is where it gets out of scope for pure templates: How do you tell visual studio to replace $Foo$? And what would the replacement be?
Your only choice here it to use a custom wizard, which either looks for the ProjectB-type project or asks the user for it.
Here's an outline for how to get the template and custom wizard to get along:
Create a VSIX package project.
Be sure to add the [ProvideBindingPath] attribute to your package. This is required to get the custom wizard registered (otherwise you would need to install it to the GAC, so VS can find it)
Create your wizard, it should implement Microsoft.VisualStudio.TemplateWizard.IWizard
Add your template to the solution and add it as a ProjectTemplate asset in your package's vsixmanifest
In the .vstemplate file of your template, add the following snippet below the <TemplateContent> block:
<WizardExtension>
<Assembly>Your.Packages.Assembly</Assembly>
<FullClassName>Your.Wizard</FullClassName>
</WizardExtension>
That will bring up your wizard when the project is created. You could, in the RunStarted method bring up a dialog which asks for the reference or you could use the automationObject parameter (which actually is a EnvDTE.DTE instance) to try and find the ProjectB project and supply it's name in the replacementsDictionary.

VSX: Custom project types

I'm trying to create a project template that works with VS2010 Standard/Pro/Ultimate, as well as Integrated Shell.
I've got the VS2010 Std part working, but for the life of me I can't get it to work w/ Int Shell - probably because my install doesn't have any CSharp projects.
After googling for what seems all day, I stumbled upon IronPython's vstemplate files, which look like this:
<VSTemplate Version="2.0.0" Type="Project" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
<TemplateData>
<Name>Python Application</Name>
<Description>A project for creating a command-line application</Description>
<Icon>__TemplateIcon.ico</Icon>
<ProjectType>Python</ProjectType>
<SortOrder>50</SortOrder>
<NumberOfParentCategoriesToRollUp>2</NumberOfParentCategoriesToRollUp>
<CreateNewFolder>false</CreateNewFolder>
<DefaultName>PythonApplication</DefaultName>
<ProvideDefaultName>true</ProvideDefaultName>
</TemplateData>
<TemplateContent>
<Project File="PythonApp.pyproj" ReplaceParameters="true">
<ProjectItem ReplaceParameters="true" OpenInEditor="true" TargetFileName="$safeprojectname$.py">Program.py</ProjectItem>
</Project>
</TemplateContent>
</VSTemplate>
These templates work with all versions and have a different ProjectType than the ones mentioned on MSDN: ProjectType = Python. How did they do this? If I use something other than CSharp/Web/VisualBasic, my project type gets sent down the drain.
Thanks;
I didn't get deep into the problem you are describing but I guess these 2 tips will help you to address the issue. In short you should care about ProjectType property for your custom ProjectNode class, and also ensure you have specified it somewhere in VSX attrubutes and in your custom .template file (I don't remember where I did that exactly). So there are those links I mentioned:
Walkthrough: Part 1 - Creating a Basic Project System (search
for 'ProjectType' and 'ProjectGuid' words onthe page)
Root Node of Custom Project Type in 'New Project' Dialog box
(also look at 'Related Topics' from the right of the page)
Hope that helps!

ASP.NET MVC3 project does not always publish all views/content

This is crazy, but I can't seem to get all my views/content/scripts published when I publish the site. This seems to happen, I believe, when the view or content is not directly referenced by my project, but used by another assembly in my project. So I might have:
ExternalAssembly.dll referenced (it gets published)
I'll need ExternalLogin.cshtml in my main project, under my views folder
ExternalLogin.cshtml doesn't get published
Right now I have a script that copies everything in the Views folder and dumps it to where I want it deployed, but VS should do this for me. What am I doing wrong?
When you click on one if these files what is the build action for it on the properties? Content....or? Set to content.
So your views files are in another project or folder outside your current project? Normally the files have to exist in the web site project, in it's views folder, not externally, and the build action should be set to Content and not to copy to the output folder. But there are some workarounds:
Duplicate them in to your site views folder and make sure they are marked content (as stated in another answer). One thing to note though is that you can add them as "Linked Files" in visual studio which actually allows them to exist in two places in the hierarchy without having to exist in two places on disk: http://support.microsoft.com/kb/306234
If you have control over the external library, you can compile them in as embedded resources or use Razor Generator or something similar and use a custom view engine to return them: How can I make ASP.NET MVC 3 use views (aspx, ascx) files from an external assembly in my website?
Manually put the copies in the .csproj build XML using the Copy task: http://msdn.microsoft.com/en-us/library/3e54c37h.aspx (Note that this will make it work in visual studio doing essentially what you are doing now, as it will then be part of the Visual Studio build if you add it to the AfterBuild target or something)

Resources