Create a Work Item By code in TFS returns TF237124 Work Item is not ready to save

Today my dear friend Matteo asked me some help with a snippet of code to insert a WIT into a TFS. The snippet was the following one.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Dim tfs As New TeamFoundationServer("http://tfs2010test:8080/tfs/defaultcollection",
New NetworkCredential("Administrator", "xxxxxxxx"))
Dim wis As WorkItemStore = DirectCast(tfs.GetService(GetType(WorkItemStore)), WorkItemStore)
Dim teamProject As Project = wis.Projects(0)
Dim witype As WorkItemType = teamProject.WorkItemTypes("Task")
If witype IsNot Nothing Then
Console.WriteLine("Adding new task to Team Project {0}", teamProject.Name)
Dim wi As New WorkItem(witype)
wi.Title = "You have a Task! [Ding]"
wi.State = "Active"
wi.Save()
Console.WriteLine("Added Work Item # {0} created by {1}", wi.Id, wi.CreatedBy)

He is having problem because the Save throws exception.

TF237124: Work Item is not ready to save

And he was wondering what the cause is. Whenever you want to save a Work Item you need to know that there are a lot of rules that can be violated, as an example the team project I’m trying to save the WIT into, has a customized workflow for the item *Task.*The right path to do is to validate the WorkItem prior to save , and looking at errors you can simply understand what prevents the WIT from saving, the code is really simple.

1
2
3
4
Dim result = wi.Validate()
For Each item In result
Console.WriteLine(item)
Next

Just call Validate() method of the WIT and verify each errors returned in an ArrayList object. Let’s look at what is returned in my situation inside the Arraylist returned by validate.

image

As you can see there is an error in the field System.State (look at the property ReferenceName), and the error can be found in the field Status that list InvalidListValue. The good stuff is that a list of Allowed Values can be found in this item, thus permitting you to look at the possible values. In my situation the only valid initial state is Proposed, so I need to add this line to the preceding snippet.

1
wi.Fields("System.State").Value = "Proposed"

And the insert work good. So if you ever encounter a TF237124 and whenever you need to save a Work Item do not forget to check errors returned by the Validate() method.

alk.