9

I'm currently trying to iterate over all of my projects (sharepoint) to get all feature guids into an file. there i want to prefix them with the projects name. My problem is DTE.Solution.Item and DTE.Solution.Projects.Item (or the enumerators for foreach) will not take an integer as parameter and foreach returns an object which is not castable to Project.

Here is my code snippet:

var hostServiceProvider = (IServiceProvider) Host;
var dte = (DTE) hostServiceProvider.GetService(typeof(DTE));
var projectCount = dte.Solution.Projects.Count;

var projects = new Dictionary<string, string>();

foreach(Project dteProject in dte.Solution)
{
    var dteProject = dte.Solution.Item(i);
    projects.Add(dteProject.Name, dteProject.FullName);
}

Okay - the code is alright - the debugger is NOT! My Exceptions where thrown in a debug context, but the Template will run fine, if the debugger is not attached.

TGlatzer
  • 5,815
  • 2
  • 25
  • 46
  • I am seeing this exact same problem, and just encountered it this morning (good timing on the question). In my case it's throwing an InvalidCastException when iterating Project.ProjectItems. As described below, it only throws when debugging the template. – daveaglick Oct 19 '12 at 14:00

2 Answers2

14

Try the Solution.Projects property:

<#@ template language="C#" debug="true" hostspecific="true" #>
<#@ assembly name="Microsoft.VisualStudio.Shell.Interop.8.0" #>
<#@ assembly name="EnvDTE" #>
<#@ assembly name="EnvDTE80" #>
<#@ assembly name="VSLangProj" #>
<#@ import namespace="Microsoft.VisualStudio.Shell.Interop" #>
<#@ import namespace="EnvDTE" #>
<#@ import namespace="EnvDTE80" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<#@ output extension=".txt" #>
<#

var hostServiceProvider = (IServiceProvider)this.Host;
var dte = (DTE)hostServiceProvider.GetService(typeof(DTE));

foreach (Project project in dte.Solution)
{
    #>
    <#= project.Name #>
    <#
}

#>
Steven
  • 166,672
  • 24
  • 332
  • 435
  • @Grumbler85: Updated my answer. The code above is a T4 template that runs succesfully on my Visual Studio 2010. – Steven Oct 18 '12 at 10:57
  • Okay - i see the problem now - i tried to debug my t4 since a while now - but i did not debug yours, but when i did, yours was failing too. So it seems T4-Debugging is not at it's best. – TGlatzer Oct 18 '12 at 11:08
  • I don't think this works if you're using solution folders, you'd have to implement some kind of recursion and probably check the Kind. – Hunter Web Apps Feb 04 '22 at 19:31
  • This solution will handle solution folders with a recursive pattern. https://stackoverflow.com/questions/38740773/how-to-get-project-inside-of-solution-folder-in-vsix-project – Hunter Web Apps Feb 04 '22 at 20:34
2

Try this

        var item = dte.Solution.Projects.GetEnumerator();
        while (item.MoveNext())
        {
            var project = item.Current as EnvDTE.Project;
            if (project == null)
            {
                continue;
            }
            ...
        }
podiluska
  • 50,950
  • 7
  • 98
  • 104