Creating a visual studio package to add templates (vspackague visual studio sdk)

1

I am trying to create a visual studio package, specifically I want to create a new menu option in the items context menu. As this one:

http://www.diaryofaninja.com/blog/2014/02/18/who-said-building-visual-studio-extensions-was-hard

I already got the menu option display:

Custom menu option

But now I want to configure the callback to create a file based on a template (custom visual studio template) like when we we click in Add > Class But instead of use the class template, use one that I got created in my custom list. Avoiding the time to search the template in the list.

In the example that I follow to create the 'Add new service' buttom, in the first example is showed how to create a popup with:

IVsUIShell uiShell = (IVsUIShell)GetService(typeof(SVsUIShell));
Guid clsid = Guid.Empty;
int result;

Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(uiShell.ShowMessageBox(
                   0,
                   ref clsid,
                   "NewService",
                   string.Format(CultureInfo.CurrentCulture, "Inside {0}.MenuItemCallback()", this.ToString()),
                   string.Empty,
                   0,
                   OLEMSGBUTTON.OLEMSGBUTTON_OK,
                   OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST,
                   OLEMSGICON.OLEMSGICON_INFO,
                   0,        // false
                   out result));

I guess that there should be more services to use with this VSPackages, but I don't found a method like that in the referece: https://msdn.microsoft.com/en-us/library/bb166217.aspx

Can you tell me from where I can found a method to perform this operation. Or how to archive my goal.

Update:

I am trying with this:

var dte = (DTE)GetService(typeof(DTE));
dte.ItemOperations.NewFile(@"General\Text File", "file.txt","7651A701-06E5-11D1-8EBD-00A0C90F26EA");

But i am getting this exception:

An exception of type 'System.Runtime.InteropServices.COMException' occurred in NewService.dll but was not handled in user code

Additional information: Invalid class string Exception from HRESULT: 0x800401F3 (CO_E_CLASSSTRING)

c#
visual-studio
visual-studio-2013
vspackage
visual-studio-sdk
asked on Stack Overflow Feb 21, 2015 by juan25d • edited Jun 20, 2020 by Community

1 Answer

1

Solved!

I used the following piece of code:

 private void MenuItemCallback(object sender, EventArgs e)
 {       
        var dte = (DTE)GetService(typeof(DTE)) as EnvDTE80.DTE2;

        var template = @"C:\Users\JuanAntonio\Documents\Visual Studio 2013\Templates\ItemTemplates\MyTemplate.vstemplate";

        dte.Solution.Projects.Item(1).ProjectItems.AddFromTemplate(template, "Template.cs");     
 }
answered on Stack Overflow Feb 21, 2015 by juan25d

User contributions licensed under CC BY-SA 3.0