Visual Studio Development Bookmark and Share   
 index > Visual Studio Extensibility > Display Add New Item dialog with specific item selected
 

Display Add New Item dialog with specific item selected

I would like to be able to provide a menu item via my addin so that users can easily add my custom project item to their projects. What I ideally would like to do is to show the Add New Item dialog and have my custom item pre-selected for the user, much in the same way as calling DTE.ExecuteCommand("Project.AddClass", String.Empty) does. I've tried calling Project.AddNewItem with various arguments and all result in E_FAIL. Does anyone know how I can achieve this?
neilco  Thursday, August 28, 2008 11:15 AM
so there's got to be some way.
Yes, I believe so. And I suggest that you can submit the document issue of second parameter of Project.AddNewItem to the following site in the formal way: http://connect.microsoft.com/VisualStudio/

After you submit it, we will appreciate it if you can share the feedback address so that it can be found and checked out by other community members having similar issues.

Thanks for your valuable feedback!


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Feng Chen  Thursday, September 04, 2008 2:46 AM
I've submitted feedbackhere.
neilco  Thursday, September 04, 2008 10:39 AM
I don't know exactly what services you have available from an add-in. You can try the follwoing code; it worked from inside a VsPackage:

IVsAddProjectItemDlg addItemDialog;
string strFilter = String.Empty;
int iDontShowAgain;
IVsProject3 project = <SomehowGetThisInterface>;
addItemDialog = this.GetService(typeof(IVsAddProjectItemDlg)) as IVsAddProjectItemDlg;
uint uiFlags = (uint)(__VSADDITEMFLAGS.VSADDITEM_AddNewItems | __VSADDITEMFLAGS.VSADDITEM_SuggestTemplateName | __VSADDITEMFLAGS.VSADDITEM_AllowHiddenTreeView);
addItemDialog.AddProjectItemDlg(<selectedNodeHierarchyId>, ref "<YourProjectGuid>", project, uiFlags, "<YourProjectTypeAsDefinedInVSTemplateFile>"), ("<ProjectItemNameAsDefinedInVSTemplateFile_ThatYouWantSelected>", ref "<LocalDirectoryNameForYourProject>", ref strFilter, out iDontShowAgain);
  • Marked As Answer byneilco Friday, September 05, 2008 1:46 PM
  • Proposed As Answer byConstantin Boscenco Thursday, September 04, 2008 4:58 PM
  •  
Constantin Boscenco  Thursday, September 04, 2008 4:57 PM
What's the version of Visual Studio you're using?

I've tried calling Project.AddNewItem with various arguments and all result in E_FAIL.
Could you please post here the source code and detailed steps can let me reproduce this issue locally?

Thanks.

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Feng Chen  Friday, August 29, 2008 5:02 AM
Example code:

publicvoidExec(stringCmdName,vsCommandExecOptionExecuteOption,refobjectVariantIn,refobjectVariantOut,refboolHandled)
{
Handled=false;
if(ExecuteOption==vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if(CmdName=="MyAddin.Connect.AddCustomItem")
{
try
{
applicationObject.ExecuteCommand("Project.AddNewItem",@"General\TextFile");
}
catch(COMExceptione)
{
Debug.WriteLine(e);
}
Handled=true;
return;
}
}
}

That's pretty much it. ExecuteCommand will throw a COMException with an error code of 0x80004005. I've tried several different arguments for Project.AddNewItem (including @"Visual C# Items\General\Text File", "Text File", @"Visual C# Items\Text File") and the only one that works is String.Empty.

How does Project.AddClass achieve this?



neilco  Friday, August 29, 2008 6:22 PM
What's the version of Visual Studio you're using?

Thanks.

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Feng Chen  Monday, September 01, 2008 12:42 AM
Visual Studio 2008 Team Suite without SP1.
neilco  Tuesday, September 02, 2008 9:28 AM
I can reproduce this issue and I'm not aware of the implementation of "Project.AddClass". However, the document of the second parameter of this command is not available.

As a work around, we can use the IVsProject.AddItem Method in our Addin, for more information please refer to the following threads:
Adding an item to a project when the path is known
Custom Add Item

Hope this helps.

Thanks.

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Unproposed As Answer byneilco Wednesday, September 03, 2008 1:59 PM
  • Proposed As Answer byFeng ChenModeratorWednesday, September 03, 2008 8:14 AM
  •  
Feng Chen  Wednesday, September 03, 2008 8:13 AM
Thanks for the info. Unfortunately, IVsProject.AddItem does not meet my requirements as it does not display the Add New Item dialog. I do not want to have to go create my own UI so that my users can specify their own filename for my project item template. That's what the Add New Item dialog is for. Project.AddClass, Project.AddComponent, etc. manage to do it, so there's got to be some way.
neilco  Wednesday, September 03, 2008 2:12 PM
You can try execute the code below in your implementation of IOleCommandTarget.Exec:

EnvDTE80.DTE2 dte = Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(EnvDTE.DTE)) as EnvDTE80.DTE2;
if (dte != null)
{
EnvDTE80.Solution2 solution = dte.Solution as EnvDTE80.Solution2;
string templatePath = null;;
if (solution != null)
{
templatePath = solution.GetProjectItemTemplate("<ProjectItemNameAsDefinedInVSTemplateFile>", "<YourProjectTypeAsDefinedInVSTemplateFile>");
}
}

yourProjectNode.RunWizard(parentHierarchyNode, "<ProjectItemNameAsDefinedInVSTemplateFile>", templatePath, IntPtr.Zero);

  • Unproposed As Answer byneilco Thursday, September 04, 2008 10:39 AM
  • Proposed As Answer byConstantin Boscenco Wednesday, September 03, 2008 11:38 PM
  •  
Constantin Boscenco  Wednesday, September 03, 2008 11:38 PM
so there's got to be some way.
Yes, I believe so. And I suggest that you can submit the document issue of second parameter of Project.AddNewItem to the following site in the formal way: http://connect.microsoft.com/VisualStudio/

After you submit it, we will appreciate it if you can share the feedback address so that it can be found and checked out by other community members having similar issues.

Thanks for your valuable feedback!


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Feng Chen  Thursday, September 04, 2008 2:46 AM
Constantin: Your solution does exactly the same as Feng's suggestion (i.e. adds an item to a project). If I were simply trying to do that, both suggestions would have been great. However, adding an item without showing the Add New Item dialog is notwhat I am attempting to do. I mustshow the dialog with my item selected. Thus far I am lead to believe there is no known method of doing this even though Visual Studio manages to do it somehow.Thanks all the same.
neilco  Thursday, September 04, 2008 9:40 AM
I've submitted feedbackhere.
neilco  Thursday, September 04, 2008 10:39 AM
I don't know exactly what services you have available from an add-in. You can try the follwoing code; it worked from inside a VsPackage:

IVsAddProjectItemDlg addItemDialog;
string strFilter = String.Empty;
int iDontShowAgain;
IVsProject3 project = <SomehowGetThisInterface>;
addItemDialog = this.GetService(typeof(IVsAddProjectItemDlg)) as IVsAddProjectItemDlg;
uint uiFlags = (uint)(__VSADDITEMFLAGS.VSADDITEM_AddNewItems | __VSADDITEMFLAGS.VSADDITEM_SuggestTemplateName | __VSADDITEMFLAGS.VSADDITEM_AllowHiddenTreeView);
addItemDialog.AddProjectItemDlg(<selectedNodeHierarchyId>, ref "<YourProjectGuid>", project, uiFlags, "<YourProjectTypeAsDefinedInVSTemplateFile>"), ("<ProjectItemNameAsDefinedInVSTemplateFile_ThatYouWantSelected>", ref "<LocalDirectoryNameForYourProject>", ref strFilter, out iDontShowAgain);
  • Marked As Answer byneilco Friday, September 05, 2008 1:46 PM
  • Proposed As Answer byConstantin Boscenco Thursday, September 04, 2008 4:58 PM
  •  
Constantin Boscenco  Thursday, September 04, 2008 4:57 PM
Superb! Does exactly what I need it to do. Thank you.
neilco  Friday, September 05, 2008 1:46 PM

I also, wanted to do the same thing using "AddProjectItemDlg".

It's opening the "Add new item" dialog box but its selecting first template always and when I try to create instance using template, it does not create anything.

What should be the reason for this?

+Dushantha

Dushantha  Friday, October 16, 2009 6:43 AM

You can use google to search for other answers

Custom Search

More Threads

• Do all custom properties have GUIDs?
• Visual Studio 2008 Standard, where are my missing Project Types?
• Visual Studio 2008 webconfig connectionstring
• Multiple selections in solution explorer & MPFProj
• Support for users with Team Edition products by small ISVs
• Macros & the Output Pane
• Project Properties Dialog - C/C++ Compiler | Commandline
• Can i add Build Failed to the Event Toaster?
• CodeModelEvents questions
• How to create my own custom tool for use by C# project ? And other queries...