user3707430
user3707430

Reputation: 1

TFS API not returning actual value

I'm using the TFS API to check the validity of existing work items with a C# app. I know that a certain WI (---) is invalid (as seen through the work item UI, several fields have missing or invalid values). However the below code that calls the workitem.Validate() method returns 0, or no invalid fields. I get the same result if I iterate through the field list and check each one individually using the field.IsValid property. It always returns true even though the field isValid value is actually false.

Interestingly, I can get the method to return invalid/false IF I stop the debugger before the calling line and expand the workitem in the Autos window (VS 2012) to one of the fields I know is invalid (thus seeing that the isValid property is in fact "false"). If I don't open this property during debug the value always comes back true.

Any ideas? This feels like something isn't loading properly but I get a clean compile and build.

Thanks for any assistance!

TfsTeamProjectCollection teamProjectCollection = new TfsTeamProjectCollection(collectionUri);

WorkItemStore workItemStore = teamProjectCollection.GetService<WorkItemStore>();

WorkItemCollection hyperWic = workItemStore.Query("select [system.id] from workitems where [system.id] = "---");

foreach (WorkItem wi in hyperWic)
{
    ArrayList wi_Validate = wi.Validate();

    if (wi_Validate.Count > 0)
    {
        outputTextBox.Text += "Work item field is invalid" + Environment.NewLine;
    }
}

Upvotes: 0

Views: 435

Answers (1)

Antonio Bakula
Antonio Bakula

Reputation: 20693

This code works for me (TFS 2013):

public static bool ValidateAndSave(this WorkItem wit)
{
    bool valid = wit.IsValid();
    if (valid)
    {
        wit.Save();
    }
    else
    {
        string msg = "Error saving work item, validation failed ! Errors: " + Environment.NewLine;
        foreach (var field in wit.Validate().Cast<Field>())
        {
            msg += "Field " + field.Name + " has status " + field.Status + Environment.NewLine;
        }
        EventLog.WriteEntry("WemTfsSubSystem", msg);
    }
    return valid;
}

UPDATE for future readers:

Problem in this case is the fact that WorkItem is obtained trough WorkItemStore Query, probably due to Cache issues, so solution is to get WorkItem trough WorkItemStore.GetWorkItem(id) method.

Upvotes: 0

Related Questions