Search code examples
c#tfscommand-prompttfs-sdktfs-process-template

Get all available TFS Fields for a Project Collection


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.


Solution

  • 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.