1

I need all Fields for a TFS Project Collection without specifiying a workitem. I'm already using the TFS API for a few other things, but I haven't found anything on this.

What I found was using the witadmin.exe listfields command, which gives me exactly what I want, but how do I get the results as a collection in my code? I'm not very experienced with commands, I just need the Field IDs and Names in my code so that I can display them in a WPF Listview. Do I need to call cmd.exe in my code or does the TFS API have an extension method for this? My "teacher" said it should have one since it can be done with the witadmin.exe.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
tweedledum11
  • 121
  • 9

1 Answers1

1

witadmin.exe is a .NET Executable, so you can use Reflector, dotPeek or ilSpy on it to see how Microsoft has implemented it. This seems to be the snippet you're after:

protected void InitFields()
{
    if (this.fields == null)
    {
        FieldDefinitionCollection definitions = new FieldDefinitionCollection(this.Store, false);
        List<FieldDefinition> list = new List<FieldDefinition>(definitions.Count);
        Dictionary<string, FieldDefinition> dictionary = new Dictionary<string, FieldDefinition>(definitions.Count, this.Store.ServerStringComparer);
        for (int i = 0; i < definitions.Count; i++)
        {
            FieldDefinition item = definitions[i];
            if (!item.IsInternal)
            {
                list.Add(item);
                dictionary[item.ReferenceName] = item;
                dictionary[item.Name] = item;
            }
        }
        list.Sort(new FieldComparer(this.Store.ServerStringComparer));
        this.fields = list;
        this.fieldsMap = dictionary;
    }
}

It will generate a list of all available fields in the Collection.

this.Store is an instance of WorkItemStore.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • I see, I didn't think you could look into an executable like that. What exactly is the FieldComparer? I can't find that type. The WorkItemStore I can get, but not the FieldComparer. – tweedledum11 Oct 10 '16 at 09:27
  • I don't know, when I try to build the Project, it cannot find type or namespace FieldComparer. How would the compiler find it? Is there a using reference I need? – tweedledum11 Oct 10 '16 at 09:34
  • Could I not just ignore the dictionary and the FieldComparer and return the list? – tweedledum11 Oct 10 '16 at 09:35
  • I see, so I don't really need the FieldComparer, I think I can sort it another way. I'll try this, I think I can work with that method, thank you very much, I'll comment again if there are any problems! – tweedledum11 Oct 10 '16 at 10:39