1

I want to create a Visual Studio Item Template that generates an Item (for eg. custom .aspx page) in the solution by executing T4 Template while adding it to the solution. I read the following post : T4 Templates and Visual Studio Item Templates

Is it possible to implement this without using GAX. I want to start by using a standard project item template and T4 Templates only, which doesn’t require GAX.

Please suggest,

Thanks,

Mayur

Community
  • 1
  • 1
Mayur
  • 53
  • 1
  • 10

1 Answers1

2

Yes, this is quite possible. You need two pieces to make it work.

Firstly you need to create a template that uses an IWizard implementation to enable you to run custom code. Here's the documentation for that: http://msdn.microsoft.com/en-us/library/ms185301(v=vs.100).aspx

Secondly, in your custom IWizard, you need to use T4 via its STextTemplating service. Here's the documentation for that. http://msdn.microsoft.com/en-us/library/gg586947.aspx

Hope this gets you started.

GarethJ
  • 6,496
  • 32
  • 42
  • Thanks GarethJ, Your answer is very useful to me. I have implemented the IWizard. Now I wanted to invoke T4 Templates through VS Extension. But while doing this I am not getting "STextTemplating" and "ITextTemplating" interface in my code (i.e. ITextTemplating t4 = serviceProvider.GetService(typeof(STextTemplating)) as ITextTemplating;). For this to work, I have added reference to "Microsoft.VisualStudio.TextTemplating.VSHost" , "Microsoft.VisualStudio.TextTemplating" and "Microsoft.VisualStudio.TextTemplating.Modeling.10.0" (plz see nxt comment as can't accommodate all in sngle cment) – Mayur Apr 04 '12 at 09:25
  • (to be continued from previous comment...) But still I am not getting this interface. Actually this interface is not in present in "Microsoft.VisualStudio.TextTemplating.VSHost". Bt after searching I got only one answer that "ITextTemplating" is in the dll "Microsoft.VisualStudio.TextTemplating.VSHost". I am writting the code in RunStarted() method of IWizard. Please suggest a solution to solve this. Am I doing something wrong. Please reply. – Mayur Apr 04 '12 at 09:26
  • Can you please tell me how to get Service Provider which is mentioned in this link : http://msdn.microsoft.com/en-us/library/gg586947.aspx. I tried to implement this as : EnvDTE.DTE dte = (EnvDTE.DTE)Package.GetGlobalService(typeof(EnvDTE.DTE)); // Get a service provider – how you do this depends on the context: IServiceProvider serviceProvider = new Microsoft.VisualStudio.Shell.ServiceProvider(dte as Microsoft.VisualStudio.OLE.Interop.IServiceProvider); But it's giving me some Value cannot be null exception. Am I doing it correct? Please suggest a way to do this. – Mayur Apr 04 '12 at 11:42
  • 1
    The interface is in Microsoft.VisualStudio.TextTemplating.Interfaces.10.0.dll, assumign your'e usign Vissual Studio 2010. You dont' need the modeling assembly at all. – GarethJ Apr 04 '12 at 18:51
  • You shoudl just be able to call package.GetGlobalService directly with STextTemplating and skip the DTE step altogether. – GarethJ Apr 04 '12 at 18:51