0

I have VS Extension with Custom Item Template.
This item uses it's own wizard as I need some information to generate multiple classes.
When I add this item using Add New Item -> [My item] Custom wizard (form) is displayed.
Now I would like to have all my new Item Types available from Context menu on solution Explorer.
I added context menu, but I can not make it show my wizard.
I have tried

dte.ItemOperations.AddNewItem( ... )
dte.LaunchWizard( ... )
ProjectItems.AddFromTemplate( ... ) 

but none of these works.

This is similar problem as in here, but I don't understand solution.

Pablo notPicasso
  • 3,031
  • 3
  • 17
  • 22
  • Could you please share a small sample with us? And check if [this document](https://learn.microsoft.com/en-us/visualstudio/sharepoint/walkthrough-creating-a-custom-action-project-item-with-an-item-template-part-1?view=vs-2019) helps. – Mr Qian Mar 01 '21 at 09:59

1 Answers1

1

Finally I was able to open Wizard for my custom Item Template. I was passong wrong arguments to LaunchWizard method.
Parent can be taken for example from selection:

var parent= dte.SelectedItems.Item(1);

Running wizard:

    internal static async System.Threading.Tasks.Task AddProjectItemFromTemplateAsync(DTE dte, object parent, string templateName, string defaultName)
    {
        ProjectItems items = null;
        string fullPath = null;
        string name = null;
        if (parent is Project project)
        {
            items = project.ProjectItems;
            fullPath = project.Properties.Item("FullPath").Value?.ToString();
            name = project.Name;
        }
        else if(parent is ProjectItem projectItem)
        {
            items = projectItem.ProjectItems;
            fullPath = projectItem.Properties.Item("FullPath").Value?.ToString();
            name = projectItem.Name;
        }
        if (items != null)
        {
            string template = ((Solution2)dte.Solution).GetProjectItemTemplate(templateName, "CSharp");
            object[] parameters = new object[]{
                EnvDTE.Constants.vsWizardAddItem,
                name,
                items,
                fullPath,
                defaultName,
                "",
                false
            };
            dte.LaunchWizard(template, ref parameters);
        }
    }
Pablo notPicasso
  • 3,031
  • 3
  • 17
  • 22
  • I found this post very helpful, thanks! When I run my code, the LaunchWizard method silently adds the item correctly from the template. I cannot get Visual Studio to display the New Item dialog. The value false for 6th Boolean entry in the parameter array should mean that the dialog gets displayed. Do you have any idea why I cannot get the dialog to show? – dingdonglars Oct 15 '22 at 20:49