Creating Stored Procedure template in VS2010? - visual-studio-2010

how can we create a stored procedure template and then import it to vs2010, so that whenever I create a new stored procedure that template is loaded and I already have some set of commands in my stored proc.
I have googled it, but haven't found much on it. can anyone tell me the procedure or provide me the link where I can learn creating stored proc templates for vs2010.
thanks in advance

First you need to create the template file, here is a simple example of what I needed
MySqlProc.sql
DELIMITER $
DROP PROCEDURE IF EXISTS `$fileinputname$`;
$
CREATE PROCEDURE `$fileinputname$`
(
)
BEGIN
END
$
As you guessed, the $fileinputname$ keyword will be replaced with the file name the user inputs.
Then you need to create an XML file with .vstemplate extension. Mine looked like this:
MySqlProc.vstemplate
<VSTemplate Type="Item" Version="2.0.0"
xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
<TemplateData>
<Name>MySql Stored Procedure</Name>
<Description>Creates SQL file with MySql stored procedure prerequisites </Description>
<Icon>mysql.ico</Icon>
<ProjectType>CSharp</ProjectType>
<DefaultName>MySqlStoredProcedure.sql</DefaultName>
</TemplateData>
<TemplateContent>
<ProjectItem ReplaceParameters="true" >MySqlProcedure.sql</ProjectItem>
</TemplateContent>
</VSTemplate>
I created my own icon, just for fun, otherwise you get an ugly generic icon by default.
Zip the 2 files you created + optionally the icon file. Copy the zip to Documents\Visual Studio 2010\Templates\ItemTemplates
Your new template should appear, no need to restart Visual Studio or anything like that.

Related

VSTemplate multi-file template not showing/working

I need to create three files several times in a project, with references to each other by a name convention. VS Item Templates seemed like an easy way to escape the hunt-and-peck copy-pasta-rename headache.
According to the docs it's possible to create a multi-file item template. The built-in VS2022 exporter only allows the selection of one file.
So I exported a single file, tried loading that up in VS2022, and it worked.
I then went on a search, found the aforementioned doc, added my other files, reloaded the template, and now it does not appear in the Add -> New Item menu.
<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Item">
<TemplateData>
<DefaultName>EfVueMantleMSC.cs</DefaultName>
<Name>EfVueMantleMSC</Name>
<Description>Create Model, Service, and Controller classes for EfVueMantle in one shot</Description>
<ProjectType>CSharp</ProjectType>
<SortOrder>10</SortOrder>
<Icon>__TemplateIcon.ico</Icon>
</TemplateData>
<TemplateContent>
<References />
<ProjectItem SubType="" TargetFileName="$fileinputname$Controller.cs" ReplaceParameters="true">ControllerTemplate.cs</ProjectItem>
<ProjectItem SubType="" TargetFileName="$fileinputname$Model.cs" ReplaceParameters="true">ModelTemplate.cs</ProjectItem>
<ProjectItem SubType="" TargetFileName="$fileinputname$Service.cs" ReplaceParameters="true">ServiceTemplate.cs</ProjectItem>
</TemplateContent>
</VSTemplate>
The .zip folder has all three class files, names matching the above. It's in the right place, I can load/unload single-file templates exported from VS2022 without issue. I've dug through a bunch of Q&A about earlier VS versions with this issue, but everything seems in order.
Is there something obviously wrong? Is there a straightforward way to debug when a template doesn't load, like some sort of log?
Are the MS Docs just lying again?

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.

Nested Files in Item Template

I am trying to create a Visual Studio Item Template that will create a WPF Window with an attached file for a view model
Like the following
VMWindow.xaml
---VMWindow.xaml.cs
---VMWindow.vm.cs
I am able to create the template with the following .vstemplate file
<VSTemplate Type="Item" Version="2.0.0"
xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
<TemplateData>
<Name>Viewmodel Dialog Box</Name>
<Description>Viewmodel Dialog Box</Description>
<Icon>Icon.ico</Icon>
<ProjectType>CSharp</ProjectType>
<DefaultName>VMDialog</DefaultName>
</TemplateData>
<TemplateContent>
<ProjectItem TargetFileName="$fileinputname$.xaml" SubType="Window">ViewModelDialogTemplate.xaml</ProjectItem>
<ProjectItem TargetFileName="$fileinputname$.xaml.cs">ViewModelDialogTemplate.xaml.cs</ProjectItem>
<ProjectItem TargetFileName="$fileinputname$.vm.cs">ViewModelDialogTemplate.vm.cs</ProjectItem>
</TemplateContent>
</VSTemplate>
I would like for the template to create itself with the .vm.cs file nested inside the main Window file when displayed in Solution Explorer.
I have found the following howto, I am having trouble following it with Visual Studio 2010 though. It was written in 2008, does this still apply?
Code Project article
It's actually very easy...
<ProjectItem TargetFileName="$fileinputname$.xaml" SubType="Window">ViewModelDialogTemplate.xaml</ProjectItem>
<ProjectItem TargetFileName="$fileinputname$.xaml/$fileinputname$.xaml.cs">ViewModelDialogTemplate.xaml.cs</ProjectItem>
<ProjectItem TargetFileName="$fileinputname$.xaml/$fileinputname$.vm.cs">ViewModelDialogTemplate.vm.cs</ProjectItem>
There's a much easier way. You can pull in the same wizard that VS uses to construct composite items. You do this by adding an element at the end of your template, after <TemplateContent>...
<WizardExtension>
<Assembly>Microsoft.VisualStudio.Web.Application, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</Assembly>
<FullClassName>Microsoft.VisualStudio.Web.Application.WATemplateWizard</FullClassName>
</WizardExtension>
Then you need to tell the wizard the extension of the parent, and the extension of the children...
<CustomParameters>
<CustomParameter Name="$ParentExtension$" Value=".xaml"/>
<CustomParameter Name="$ChildExtension$" Value=".cs"/>
</CustomParameters>
This element goes inside <TemplateContent>.
This solution is tested and working in VS2012, and you can see the version hardcoded in the call to the wizard. If you have version problems, look for the file webform.vstemplate (visual studio's .aspx template), and inspire yourself.
As it turns out the same method works for VS 2010. Required a bit of adaptation but this Code Project article covers the basic idea.
You need to implement the Microsoft.VisualStudio.TemplateWizard.IWizard interface, and write a little bit of code to remove the new item from the project and re-add it as the child of another item. Here is a working example from QueryFirst that takes any file with the extension .gen.cs and makes it a child of the same-named .sql file...
public void ProjectItemFinishedGenerating(ProjectItem
item)
{
string path = item.FileNames[0];
string parentPath = null;
if (path.EndsWith(".gen.cs"))
parentPath = path.Replace(".gen.cs", ".sql");
if (path.EndsWith("Results.cs"))
parentPath = path.Replace("Results.cs", ".sql");
if (!string.IsNullOrEmpty(parentPath))
{
ProjectItem parent = item.DTE.Solution.FindProjectItem(parentPath);
if (parent == null)
return;
item.Remove();
parent.ProjectItems.AddFromFile(path);
}
}
To attach the code to the template, you'll need something like this in your .vstemplate file...
<WizardExtension>
<Assembly>QueryFirst, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4688a60b10e39f04</Assembly>
<FullClassName>QueryFirst.WizardImplementation</FullClassName>
</WizardExtension>

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!

Visual Studio Item Template with Wizard

I am looking for a sample on how to create an item template with Wizard in Visual Studio 2010.
My requirement is when the user selects Add Item, I want to show a dialog where the user enters some input parameters. Then on pressing OK in the form we generate an XML file which we want to add to the project.
Thanks
I'd recommend you get the vsix featured in this link
http://blogs.msdn.com/b/visualstudio/archive/2010/03/04/creating-and-sharing-project-item-templates.aspx
and rename it as a zip file and open it up to start with. however the xml in the Item Templates themselves aren't all that complicated. Looking at one I just generated with the tool for a classic asp file.
<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Item">
<TemplateData>
<DefaultName>Classic ASP</DefaultName>
<Name>Classic ASP</Name>
<Description>Adds script tag import ref</Description>
<ProjectType>Web</ProjectType>
<ProjectSubType>VisualBasic</ProjectSubType>
<SortOrder>10</SortOrder>
<Icon>__TemplateIcon.ico</Icon>
</TemplateData>
<TemplateContent>
<References />
<ProjectItem TargetFileName="$fileinputname$.asp" ReplaceParameters="true">ClassicASP.asp</ProjectItem>
</TemplateContent>
</VSTemplate>

Resources