Search code examples
c#collectionstfs-sdk

Is it possible to get the list of all Workitem Types in a Project collection?


I am trying to get the list of all the workitem types from project collection without accessing project in project collection.

I have tried till now

I can get the list of all workitemType list using project object as in following code:

 TfsTeamProjectCollection tfctc = new TfsTeamProjectCollection(newUri
            ("http://servername:8080/tfs/teamprojectcollection"));
 WorkItemStore wis = tfctc.GetService<WorkItemStore>();

 foreach(Project project in wis.Projects)
 {               
   foreach (WorkItemType type in project.WorkItemTypes)
   {
     Console.WriteLine(type.Name);
   }
 }

By searching on getting list of WorkitemType I come to know one class in TFS as WorkItemTypeCollection.

but while using it I am not able to get the object of workitemcollection using projectCollection.

I have tried below code to do so:

 TfsTeamProjectCollection tfctc = new TfsTeamProjectCollection(newUri
            ("http://servername:8080/tfs/teamprojectcollection"));
 WorkItemTypeCollection witc = tfctc.GetService<WorkItemTypeCollection>(); // here I am getting nil object every time
// some more code          

What I Want to do

I want to get the list of WorkItemTypes in a projectcollection.

Can anyone help me on this? Correct me if I am wrong any where. Thanks in advance.


Solution

  • Work Item Types are associated to team projects, so you can only get Work Item Types for Team Projects. The reason behind this is that each Team Project can have different sets of Work Item Types. And even if you find multiple types that are equally named, e.g. "Task" or "Bug", each Team Project may have modified its definition. So essentially, the combined Work Item Types of all of your Team Projects are the Work Item Types of your Team Project Collection.

    The WorkItemTypeCollection class that you have found is actually a container for WorkItemType instances. The Project.WorkItemTypes property that you have already used returns a WorkItemTypeCollection that you can iterate. Though, this type is not registered as a service - that's why tfctc.GetService<WorkItemTypeCollection>() returns null.

    So, what you have done in your nested foreach in the example is actually how it works. You can refine that using LINQ and SelectMany, but that's about it:

    var workItemTypes = wis.Projects
                           .Cast<Project>()
                           .SelectMany(p => p.WorkItemTypes.Cast<WorkItemType>())
                           .ToArray();