BUG: ProjectItem.Collection returns a wrong collection for folders of SQL Projects of Visual Studio Team Edition for Database Professionals

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio 2005
Date: September 2007  


This article explains a bug in the EnvDTE.ProjectItem.Collection implementation of folders of SQL Server projects of the Visual Studio Team Edition for Database Professionals.

More Information

An EnvDTE.ProjectItem of a project can be a folder or a file. A folder can contain folders or files and a file can contain dependant files (such as .designer.cs or .resx). For that purpose, the EnvDTE.ProjectItem class of the automation model provides two properties:

  • ProjectItem.ProjectItems: this collection returns the children of the ProjectItem. It is used to navigate the hierarchy from top to bottom.
  • ProjectItem.Collection: this collection returns the collection of items the ProjectItem belongs to. In particular, the ProjectItem.Collection.Parent property returns the parent of the ProjectItem. It is used to navigate the hierarchy from bottom to top.

Due to a bug in the implementation of ProjectItem.Collection of folders of SQL Server projects, the collection returned is the same than ProjectItem.ProjectItems.

Steps to reproduce the problem:

  • Create a SQL Server 2005 database project, which contains by default the folders "Data Generation Plans", "Schema Objects" and "Scripts".
  • Execute this macro: 
Dim objProjectItemFolder As ProjectItem
Dim colParentCollection As ProjectItems

objProjectItemFolder = DTE.Solution.Projects.Item(1).ProjectItems.Item("Data Generation Plans")
colParentCollection = objProjectItemFolder.Collection

If colParentCollection.Count = 0 Then
  MsgBox("We should be 3 folders, not 0")
End If

The macro returns 0 folders because ProjectItem.Collection is actually returning the children of the folder, which are zero.

A consequence of this bug is that since ProjectItem.Collection.Parent returns itself, you can enter in an infinite recursion if you try to navigate the hierarchy of files and folders from bottom to top.

Go to the 'Visual Studio Extensibility (VSX)' web site for more articles like this (Articles section)