Question

I have problem in saving the workitem. The scenario is i have one web application, from there i am sending value to TFS. I can change value in both the places(through web application also from TFS). The problem i am facing right now is

When i try to save a work item in TFS it checks if there are any changes done since you opened it. If so you are not allowed to save. There is no such check when i save from web application, i have used workitem.isvalid method, still its not checking the workitem modified before i save it from web application.

This is my code

wi.Open();

foreach (var str in piWorkItemTfs.Values.Keys)
    wi[str] = piWorkItemTfs.Values[str];


if (!wi.IsValid())
{
    var listFelaktigaVärde = (from Field field in wi.Validate() select field.Name + "=" + field.Value).ToList();

    errorMessage = "Felaktiga värde: Id " + wi.Id + " " + String.Join(", ", listFelaktigaVärde);
}
else
{
    wi.Save();
    errorMessage = wi.Id.ToString();
}

Can any one help me to avoid the concurrency problem please. I need to save the workitem only there is no changes done it before, or else i have show the error message to the end user.

Was it helpful?

Solution

The IsValid() method on the work item only validates that the fields on the work item is correct, it does not check whether or not the work item has changed elsewhere. There is a flag on the work item object that is called IsDirty which checks to see whether or not the work item has changed since the last save.

As a side note, the fields you display as an error message are only fields that do not adhere to the rules of the work item type definition (i.e. a value of a field that is outside of its defined AllowedValues). This should be checked, but a check for whether, or not, it has changed since the last save should display a different error.

Here's a link to MSDN for the WorkItem class: http://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.workitemtracking.client.workitem(v=vs.110).aspx

EDIT

The IsDirty flag will pick up whether, or not, the work item has been modified locally. Instead, the way to check for server-side saves is to try to merge while saving the work item.

workItem.Save(SaveFlags.MergeAll);

The IsValid flag is for local changes, not server changes. Saving the work item with the SaveFlags.MergAll option will do the check, and throw the exception if the work item has been saved on the server side.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top