TFS API Part 24 – Get All Fields From TFS

09/03/2010

TFS API Part 24 – Get All Fields From TFS

I got an email regarding previous post (TFS API Part 6: WorkItemStore – Get Fields From WorkItemType) how to get Fields without regard to Work Item Type?

This is very easy using Team System API.

image

Download Demo Project

Step 1: Create Project and Add Reference

Create an WPF/WinForm application and add the following references:
     Microsoft.TeamFoundation.WorkItemTracking.Client.dll
(C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.WorkItemTracking.Client.dll)
     Microsoft.TeamFoundation.Client.dll
(C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.Client.dll)

Step 2: Connect to Team Foundation Server

To perform this action all you need to do is implement WorkItemStore object.

TeamProjectPicker tpp = new TeamProjectPicker(TeamProjectPickerMode.NoProject, false);

    tpp.ShowDialog();

 

    if (tpp.SelectedTeamProjectCollection != null)

    {

        TfsTeamProjectCollection tfs = tpp.SelectedTeamProjectCollection;

        WorkItemStore store = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));

 

        //Store object contains all fields, no just for spesific work item type

        AddFields(store.FieldDefinitions);

    }

Step 3: Get Fields from Server

void AddFields(FieldDefinitionCollection fields)

{

    foreach (FieldDefinition field in fields)

    {

        listBox1.Items.Add(new FieldItem(field));

    }

}

 

public class FieldItem

{

    public FieldType FieldType { get; set; }

    public int ID { get; set; }

    public string Name { get; set; }

    public string ReferenceName { get; set; }

    public string HelpText { get; set; }

    public ReportingAttributes ReportingAttributes { get; set; }

    public FieldUsages FieldUsages { get; set; }

    public bool IsEditable { get; set; }

    public AllowedValuesCollection AllowedValuesCollection { get; set; }

 

    public FieldItem(FieldDefinition field)

    {

        this.ID = field.Id;

        this.Name = field.Name;

        this.FieldType = field.FieldType;

        this.ReferenceName = field.ReferenceName;

        this.ReportingAttributes = field.ReportingAttributes;//TFS 2010

        this.FieldUsages = field.Usage; //TFS 2010

        this.IsEditable = field.IsEditable;

        this.AllowedValuesCollection = field.AllowedValues;

    }

 

    public override string ToString()

    {

        return string.Format("{0} ({1})", this.Name, this.ReferenceName);

    }

}

Download Demo Project

Enjoy

Add comment
facebook linkedin twitter email

Leave a Reply