locked
VS 2010 MPF: Templates when adding new item to custom project RRS feed

  • Question

  • Hi everyone,

    I've created a custom language package, which extends ProjectPackage.  I can create the new project correctly, and a file is correctly created and added to the project when the project is made.  I would like to be able to add new source files to the project by using the Add->New Item... (Ctrl+Shift+A) menu item.  However, when I click this at the moment the list of available templates is empty.  I would like to add my own custom template to the menu of available templates for this project type.  Is there some documentation for accomplishing this?  The only mentions I have seen have been registry hacks, but there has to be a way to do the programmatically I would think.

    Is there a particular method I can override to populate the list?  Do I really need to make a template, or can I just show the 'template name', 'icon' and provide the correct file extension (the files should be empty when created, so I think a template is largely wasted on what I want to do).

    Thanks,

    Giawa

    Tuesday, July 26, 2011 8:12 PM

Answers

  • After a lot of reading and work, here's my answer:

    Use vstemplate files for your project and items, and compile them as vstemplate (do not copy to output, do not include with VSIX, contrary to what the Microsoft Walkthrough suggests).  The compiler will package each item and project template into their own zip file.  Now, you need to tell the vsixmanifest where those files are.  The default directories for projects and items are ProjectTemplates and ItemTemplates, respectively.  Add them to your vsixmanifest like so:

       <Content>
        <VsPackage>|%CurrentProject%;PkgdefProjectOutputGroup|</VsPackage>
        <ProjectTemplate>ProjectTemplates</ProjectTemplate>
        <ItemTemplate>ItemTemplates</ItemTemplate>
       </Content>
    

    Now, you must also add some properties to your .csproj file that your vsix package is built in.  These properties can probably be added via some convoluted method, but the easiest is just to drop them directly in the .csproj file.

       <PropertyGroup>
        <GetVsixSourceItemsDependsOn>$(GetVsixSourceItemsDependsOn);GetVsixTemplateItems</GetVsixSourceItemsDependsOn>
       </PropertyGroup>
       <Target Name="GetVsixTemplateItems" DependsOnTargets="ZipProjects;ZipItems">
        <ItemGroup>
         <VSIXSourceItem Include="@(IntermediateZipItem)">
          <VSIXSubPath>ItemTemplates\%(IntermediateZipItem.Language)\%(IntermediateZipItem.OutputSubPath)\%(IntermediateZipItem.Culture)</VSIXSubPath>
         </VSIXSourceItem>
         <VSIXSourceItem Include="@(IntermediateZipProject)">
          <VSIXSubPath>ProjectTemplates\%(IntermediateZipProject.Language)\%(IntermediateZipProject.OutputSubPath)\%(IntermediateZipProject.Culture)</VSIXSubPath>
         </VSIXSourceItem>
        </ItemGroup>
       </Target>
    

    This will now include your zip files into your project, and with any luck, you'll now have projects and items that are linked correctly together.

    Luckily, there is quite a bit of documentation out there for item/project templates.  It just took me a while to realize that is the correct way to go.  Good luck,

    Giawa
    • Marked as answer by Giawa Wednesday, July 27, 2011 6:57 AM
    Wednesday, July 27, 2011 6:56 AM

All replies

  • After a lot of reading and work, here's my answer:

    Use vstemplate files for your project and items, and compile them as vstemplate (do not copy to output, do not include with VSIX, contrary to what the Microsoft Walkthrough suggests).  The compiler will package each item and project template into their own zip file.  Now, you need to tell the vsixmanifest where those files are.  The default directories for projects and items are ProjectTemplates and ItemTemplates, respectively.  Add them to your vsixmanifest like so:

       <Content>
        <VsPackage>|%CurrentProject%;PkgdefProjectOutputGroup|</VsPackage>
        <ProjectTemplate>ProjectTemplates</ProjectTemplate>
        <ItemTemplate>ItemTemplates</ItemTemplate>
       </Content>
    

    Now, you must also add some properties to your .csproj file that your vsix package is built in.  These properties can probably be added via some convoluted method, but the easiest is just to drop them directly in the .csproj file.

       <PropertyGroup>
        <GetVsixSourceItemsDependsOn>$(GetVsixSourceItemsDependsOn);GetVsixTemplateItems</GetVsixSourceItemsDependsOn>
       </PropertyGroup>
       <Target Name="GetVsixTemplateItems" DependsOnTargets="ZipProjects;ZipItems">
        <ItemGroup>
         <VSIXSourceItem Include="@(IntermediateZipItem)">
          <VSIXSubPath>ItemTemplates\%(IntermediateZipItem.Language)\%(IntermediateZipItem.OutputSubPath)\%(IntermediateZipItem.Culture)</VSIXSubPath>
         </VSIXSourceItem>
         <VSIXSourceItem Include="@(IntermediateZipProject)">
          <VSIXSubPath>ProjectTemplates\%(IntermediateZipProject.Language)\%(IntermediateZipProject.OutputSubPath)\%(IntermediateZipProject.Culture)</VSIXSubPath>
         </VSIXSourceItem>
        </ItemGroup>
       </Target>
    

    This will now include your zip files into your project, and with any luck, you'll now have projects and items that are linked correctly together.

    Luckily, there is quite a bit of documentation out there for item/project templates.  It just took me a while to realize that is the correct way to go.  Good luck,

    Giawa
    • Marked as answer by Giawa Wednesday, July 27, 2011 6:57 AM
    Wednesday, July 27, 2011 6:56 AM
  • Hi Giawa,

    Glad to know you have found a workaround.

    Cheers,

    Yi


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, July 28, 2011 6:06 AM
  • Hi, Giawa,

    I followed the process, but project template didnot show in "Add new project" dialog.

    and I can see the project zip is generated.

    could you please give me some hints?

    thank you very much.

    Monday, August 22, 2011 6:21 AM
  • Hi kerryqi,

    You need to make sure that your language package uses a project factory, and that the LanguageVsTemplate name matches the name you have used in your templates.  For example:

      [ProvideProjectFactory(typeof(VerilogProjectFactory), "Verilog", "Verilog Project (*.vproj);*.vproj",
        "vproj", "vproj", ".\\NullPath", LanguageVsTemplate = "Verilog")]
      public sealed class VerilogLanguagePackage : ProjectPackage, IOleComponent
      {
    

    is how I associate my project factory and language name with my language package.  And in my vstemplate files I have the following:

      <ProjectType>Verilog</ProjectType>
    

    I believe that is all you need to get this up and running.  Good luck,

    Giawa

    Monday, August 22, 2011 10:58 PM
  • Hi Giawa,

    Thank you so much.

    it works in Experimental mode. but when I used it in Isolated Shell, it does not work.

    Do you have any idea?

    thanks.

    Tuesday, August 23, 2011 2:42 AM
  • Hi Kerryqi,

    I found that I needed to include my ProjectTemplates/ItemTemplates in the ShellExtensionsVSIX manifest for it to work.  Here's the relevant part of my source.extension.vsixmanifest (from the ShellExtensionsVSIX project) when targeting the isolated shell.

     <Content>
     <VsPackage>|VerilogStudioShell.AboutBoxPackage;PkgdefProjectOutputGroup|</VsPackage>
     <VsPackage>|VerilogLanguagePackage;PkgdefProjectOutputGroup|</VsPackage>
     <ProjectTemplate>ProjectTemplates</ProjectTemplate>
     <ItemTemplate>ItemTemplates</ItemTemplate>
     </Content>
    
    You also need to ensure that projectitems/etc are all copied to the isolated shell output directory.  In the project properties for your package that contains your templates, ensure that you are copying the VSIX content to your extensions directory.  This can be done in the 'VSIX' tab by selecting 'Copy VSIX content to the following location:'

    My location is as follows (Release Mode):  D:\projects\VisualVerilog\Release\Extensions\Application

    Good luck,

    Giawa

    • Edited by Giawa Tuesday, August 23, 2011 5:09 PM typo
    Tuesday, August 23, 2011 5:09 PM
  • Giawa,

    thank you so much. it works now.

    Friday, August 26, 2011 5:22 AM
  • Happy to hear it :)  Best of luck with your project!

    Giawa

    Saturday, August 27, 2011 5:31 AM
  • After a lot of reading and work, here's my answer:

    Use vstemplate files for your project and items, and compile them as vstemplate (do not copy to output, do not include with VSIX, contrary to what the Microsoft Walkthrough suggests).  The compiler will package each item and project template into their own zip file.  Now, you need to tell the vsixmanifest where those files are.  The default directories for projects and items are ProjectTemplates and ItemTemplates, respectively.  Add them to your vsixmanifest like so:

       <Content>
        <VsPackage>|%CurrentProject%;PkgdefProjectOutputGroup|</VsPackage>
        <ProjectTemplate>ProjectTemplates</ProjectTemplate>
        <ItemTemplate>ItemTemplates</ItemTemplate>
       </Content>
    

    Now, you must also add some properties to your .csproj file that your vsix package is built in.  These properties can probably be added via some convoluted method, but the easiest is just to drop them directly in the .csproj file.

       <PropertyGroup>
        <GetVsixSourceItemsDependsOn>$(GetVsixSourceItemsDependsOn);GetVsixTemplateItems</GetVsixSourceItemsDependsOn>
       </PropertyGroup>
       <Target Name="GetVsixTemplateItems" DependsOnTargets="ZipProjects;ZipItems">
        <ItemGroup>
         <VSIXSourceItem Include="@(IntermediateZipItem)">
          <VSIXSubPath>ItemTemplates\%(IntermediateZipItem.Language)\%(IntermediateZipItem.OutputSubPath)\%(IntermediateZipItem.Culture)</VSIXSubPath>
         </VSIXSourceItem>
         <VSIXSourceItem Include="@(IntermediateZipProject)">
          <VSIXSubPath>ProjectTemplates\%(IntermediateZipProject.Language)\%(IntermediateZipProject.OutputSubPath)\%(IntermediateZipProject.Culture)</VSIXSubPath>
         </VSIXSourceItem>
        </ItemGroup>
       </Target>
    

    This will now include your zip files into your project, and with any luck, you'll now have projects and items that are linked correctly together.

    Luckily, there is quite a bit of documentation out there for item/project templates.  It just took me a while to realize that is the correct way to go.  Good luck,

    Giawa

    Hi, first of all, sorry for my english.

    I follow the example but didn't work.

    What I did is create a project template and item template in my isolated shell, in .csproj of project template is where copy the code. Is that correct?

    I don't know if I could explain well, but any questions or assistance would serve me.

    Thank you very much.
    Fabiana.

    Tuesday, August 13, 2013 9:02 PM