12

I would like to create a custom item template that includes a wizard (IWizard interface).

On http://msdn.microsoft.com/en-us/library/vstudio/ms171411%28v=vs.100%29.aspx and some other places it is always described that the assembly containing the wizard has to be installed in GAC. I would like to distribute my template in my organisation, and not everybody has right to install anything into GAC.

So I'm wondering, is there no way to have the assembly containing the wizard code inside the template zip, or vsix file?

It seems to me that this should be a common problem?

Any help how to distribute a custom wizard and item template without any GAC installations?

Achim
  • 828
  • 6
  • 21

1 Answers1

19

After a lot of research, I found a way to do that. With this method it is possible to include the assembly with the IWizard implementation into the VSIX file, even without signing it, and having it available at runtime. No need to fiddle with the GAC.

You need 3 projects in your solution

  1. the vsix project (we call it MyExtension)
  2. an item template project (we call it MyTemplate)
  3. a regular class library project for the wizard code (we call it MyWizardImpl)

Inside MyWizardImpl you need to implement the IWizard interface, in an arbitray class. (we call it MyWizardImpl.ItemTemplateWizard1). When compiling this, you get as output

<solutiondir>\MyWizardImpl\bin\Debug\MyWizardImpl.dll

Now, in MyTemplate\MyTemplate.vstemplate, you need to reference it

<WizardExtension>
    <Assembly>MyWizardImpl</Assembly>
    <FullClassName>MyWizardImpl.ItemTemplateWizard1</FullClassName>
</WizardExtension>

The tricky part comes last: Including the assembly inside the vsix in a way that it can be loaded when the ItemTemplate is applied.

  • Create a folder MyExtension\Assemblies
  • In solution explorer, Right-click that folder and choose Add -> Existing Item...
  • In the "Add existing Item" dialog, navigate to the created MyWizardImpl.dll. On the Add button, click on the little arrow and choose "Add As Link" (to prevent that VS makes a copy of the dll)
  • In the properties for this link, choose BuildAction=Content, IncludeInVSIX=True
  • Open the vsixmanifest in a XML editor (or notepad), and add an Assembly line to the section

Example:

<Content>
   <Assembly AssemblyName="MyWizardImpl">Assemblies\MyWizardImpl.dll</Assembly>
</Content>

Build and test the solution, the wizard code should run now when using the template.

If you need more than one template in your vsix, you simply need to add additional ItemTemplate projects. All the wizards can go into the same MyWizardImpl assembly.

Achim
  • 828
  • 6
  • 21
  • 8
    Including the output of one project as an item in another project is generally brittle, and there is a better way to handle it in this case. You can add the class library assembly as an asset to the VSIX project: open source.extension.vsixmanifest and select the "Assets" tab, then click "New", select type "Assembly", source "A project in current solution", and finally pick the project. There is a field in that dialog that is labeled "Embed in this folder": just put "Assemblies" there. The end result will be exactly the same, but it won't throw off MSBuild dependency tracking etc. – Pavel Minaev Jul 19 '13 at 01:13
  • 1
    I think there is no "Assets" tab in VS2010 – aaron Aug 22 '13 at 15:13
  • @aaron It's called "Content" in VS2010 – just.another.programmer May 05 '14 at 15:07
  • Why is adding the assembly as an "asset" or "content" better than adding it as an item to the project? It seems almost the same. – Nick Feb 16 '15 at 06:03
  • Not sure how it is with VS2013 nowadays, but at least with 2010 this was not possible, the actual assembly was not available then during the run of the wizard. – Achim Mar 23 '15 at 12:34
  • after adding tag it is giving an error `Could not find project TemplateWizard referenced from source.extension.vsixmanifest. Please add a ProjectReference to TemplateWizard or correct project name in source.extension.vsixmanifest` – Neo Apr 03 '15 at 10:14
  • This is documented in the docs: https://learn.microsoft.com/en-us/visualstudio/extensibility/how-to-use-wizards-with-project-templates?view=vs-2019 – SteveB Apr 29 '21 at 16:59
  • @SteveB Great, sometimes things are getting easier over the years... – Achim Apr 30 '21 at 09:24