Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TFS 2010 Work Item is not ready to save but there are no validation items

Tags:

c#

tfs

I'm trying to save a TFS Work Item programmatically but always get the exception:

TF237124: Work Item is not ready to save

Now, I understand what this is telling me - that the Work Item is missing a required field or similar - and my code is anticipating this by calling:

ArrayList ValidationResult = wi.Validate(); 

before the save. However my ArrayList contains no elements following this call.

I've tried logging in to the TFS web interface using the same credentials and creating a Work Item that way which works fine.

How can I discover why my Work Item won't save? Here's my code:

// get a reference to the team project collection (authenticate as generic service account)
        using (var tfs = new TfsTeamProjectCollection(tfsuri, new System.Net.NetworkCredential("My_User", "password")))
        {
            tfs.EnsureAuthenticated();
            var workItemStore = GetWorkItemStore(tfs);

             // create a new work item
             WorkItem wi = new WorkItem(GetWorkItemType(type, workItemStore));
             {
                //Values are supplied as a KVP - Field Name/Value
                foreach (KeyValuePair<string,string> kvp in values)
                {
                   if (wi.Fields.Contains(kvp.Key))
                   {
                      wi.Fields[kvp.Key].Value = kvp.Value;
                   }
                }   

                ValidationResult = wi.Validate();                       
              }

              if (ValidationResult.Count == 0)
              {
                 wi.State = wi.GetNextState("Microsoft.VSTS.Actions.Checkin");
                 wi.Save();
                 return wi.Id;
              }
              else
              { 
                 return 0;
              }
            }
        }
like image 645
Simon Avatar asked Dec 02 '11 14:12

Simon


1 Answers

You are validating the work item before you are changing it's state. Transitioning to a new state can cause Work Item Template actions/rules to be processed. These could be changing the values of some of your fields and/or adding new rules to the fields which would cause the previously valid data to be invalid.

Moving from an Open state to a Closed state might require someone to complete a "Review" field (for example) - if it's empty it cannot transission.

Try validating after the State change and see if there are any failures.

like image 149
DaveShaw Avatar answered Nov 02 '22 23:11

DaveShaw