Lei Yang
Lei Yang

Reputation: 4335

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.

Upvotes: 4

Views: 3662

Answers (2)

Ruben Simpson
Ruben Simpson

Reputation: 1

field.Status.ToString()

Worked for me, this will capture the error message.

Upvotes: 0

KMoraz
KMoraz

Reputation: 14164

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();
}

Upvotes: 15

Related Questions