locked
Adding project items from templates in a wizard RRS feed

  • Question

  • Hello. I've got a project template with a wizard. I need to add project items from template to created project somewhere within wizard. Those project item templates have custom parameters with values provided in wizard. Is there any way to implement it?

    I tried adding items in RunStarted method, but there are no projects yet created. If adding with ProjectItems.AddFromTemplate in ProjectFinishedGenerating then template parameters are not substituted.

    Tuesday, January 26, 2016 8:38 AM

Answers

  • Hi,

    When I implemented a ProjectWizard for my Language Service, I created one "big" project template: The .vstemplate file contained all possible <ProjectItem> definitions (contained in <TemplateContent> region). Upon finishing the Wizard, IWizard.ShouldAddProjectItem() is called for each item listed in the .vstemplate file. Here you can simply return true if you want file in the created project, or false otherwise. The main advantage is that you just have one big template containing all assets, and you can choose at runtime which elements shall be contained in the created project. There is no need to add items from other templates.

    For replacing parts of the files with custom content you can use the provided replacementsDictionary, which is supplied with the RunStarted() method.

    I hope that help,
    Regards,
    Max

    • Marked as answer by DotNet Wang Friday, February 12, 2016 7:57 AM
    Monday, February 1, 2016 12:26 PM

All replies

  • Hi KidOfTheNeon,

    You could use the Export Template Wizard extension to export project templates and item templates to the VSIX file format. Links below are for your reference.

    Working with Project Template and Wizard Extension

    How to: Use the Template Wizard Extension

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 27, 2016 2:55 AM
  • If you want to add additional items to your project using an item template, you should invoke the item template from the IWizard.ProjectFinishedGenerating function. Otherwise, as you surmise, the root project isn't quite there yet.

    I ran into a similar scenario with the VSX Template Wizard. You can address this a couple of ways, but I chose to just reuse the IWizard for both templates, and keep the pertinent info around in static variables, and add them to the replacements dictionary based on the template that was currently invoking the wizard.

    For example, review the RunStarted method in the wizard implementation here. I used the customParams[0] object to retrieve the .vstemplate file the wizard is invoked for.

    Sincerely,


    Ed Dore

    Wednesday, January 27, 2016 4:56 AM
  • Hi Ed. I tried to add project item, but wizard doesn't find item template file.

    Here is how project is added from a template in your example:

    string templateZipPath = Path.GetDirectoryName(Path.GetDirectoryName(customParams[0].ToString()));
    wizardTemplate = Path.Combine(templateZipPath, "VSXWizardTemplate.zip", "VSXWizardTemplate.vstemplate");
    sln.AddFromTemplate(wizardTemplate, wizardDestDir, wizardProjName);

    And this is how I add project item. Path (with substituted <path-to-mydocs>) is correct, and when inserting it into explorer it opens vstemplate file. Could the problem be Visual Studio 2012, while your example uses 2013?

    public void ProjectFinishedGenerating(Project project) { project.ProjectItems.AddFromTemplate(@"<path-to-mydocs>\Visual Studio 2012\Templates\ItemTemplates\Visual C#\VisualStudioItemTemplateTest.zip\MyTemplate.vstemplate", "WizardItem.cs"); }


    • Edited by KidOfTheNeon Wednesday, January 27, 2016 1:04 PM
    Wednesday, January 27, 2016 1:03 PM
  • Hi,

    When I implemented a ProjectWizard for my Language Service, I created one "big" project template: The .vstemplate file contained all possible <ProjectItem> definitions (contained in <TemplateContent> region). Upon finishing the Wizard, IWizard.ShouldAddProjectItem() is called for each item listed in the .vstemplate file. Here you can simply return true if you want file in the created project, or false otherwise. The main advantage is that you just have one big template containing all assets, and you can choose at runtime which elements shall be contained in the created project. There is no need to add items from other templates.

    For replacing parts of the files with custom content you can use the provided replacementsDictionary, which is supplied with the RunStarted() method.

    I hope that help,
    Regards,
    Max

    • Marked as answer by DotNet Wang Friday, February 12, 2016 7:57 AM
    Monday, February 1, 2016 12:26 PM