Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the TFS workitem validation error message by code?

I already know WorkItem.Validate method can get an ArrayList of fields in this work item that are not valid(msdn).

But they do seem to contain only invalid fields and thus names, but not contain any error messages, i.e. why they're invalid, which is useful for situation of submitting workitem without using the built in TFS controls.
How to get error tip like "new bug 1: TF200012: field 'Title' cannot be empty."?

For better understanding, please see the picture.msdn
I am using VS2010 SP1 Chinese language, and the error discription is translated as above.

like image 350
Lei Yang Avatar asked Dec 07 '22 02:12

Lei Yang


1 Answers

Visual Studio is just another client that wraps TFS error messages. You can't capture the TF* errors but you can get the FieldStatus and print you own message.

var invalidFields = workItem.Validate();
if (invalidFields.Count > 0)
{
    foreach (Field field in invalidFields)
    {
        string errorMessage = string.Empty;
        if (field.Status == FieldStatus.InvalidEmpty)
        {
            errorMessage = string.Format("{0} {1} {2}: TF20012: field \"{3}\" cannot be empty."
                , field.WorkItem.State
                , field.WorkItem.Type.Name
                , field.WorkItem.TemporaryId
                , field.Name);
        }
        //... more handling here

        Console.WriteLine(errorMessage);
    }
}
else // Validation passed
{
    workItem.Save();
}
like image 171
KMoraz Avatar answered Dec 08 '22 16:12

KMoraz