How to programmatically close a TFS work item
I am trying to import items from an old issue tracker stored in an Excel worksheet in Team Foundation Server. I am successfully processing rows of an Excel file and I can create new work items, but they are always in the sentence. If I try to change the state to Closed, then call the Validate method on the work item, I get a validation error on the State property - InvalidListValue {4}.
Dim MyProj As Project = store.Projects("MyProject")
Dim WIT As WorkItemType = MyProj.WorkItemTypes("Task")
Dim WorkItem As WorkItem = WIT.NewWorkItem()
WorkItem.Title = Title
WorkItem.Description = Description
WorkItem.History = History
WorkItem.State = "Closed"
WorkItem.Fields("Assigned To").Value = AssignedTo
WorkItem.Fields("Priority").Value = Priority
WorkItem.Fields("Closed By").Value = ClosedBy
I also tried the below code, trying to save the work item, change the state to closed and save it again, but that doesn't work either - the state is still prompted when I open it with a TFS request for My Work Items:
WorkItem.Save()
WorkItem.State = "Closed"
WorkItem.Fields("Closed By").Value = ClosedBy
WorkItem.Save()
Has anyone else tried such a thing and succeeded, or have any ideas for this? Oh, and this is a CMMI task that I am trying to create and close. I wonder if I'm trying to skip some of the steps required by CMMI, but I'm new to this and that's just an assumption.
a source to share
I figured out how to create and close a CMMI TFS task programmatically. The key had to go through the CMMI process, which can be found at http://msdn.microsoft.com/en-us/library/bb668962.aspx , changing the state of the state and saving the WorkItem after each change.
... WorkItem creation tasks
WorkItem.Fields("Assigned To").Value = AssignedTo
WorkItem.Fields("Priority").Value = Priority
'This first Save creates a WorkItem in the Proposed state'
WorkItem.Save()
WorkItem.State = "Active"
Errors = WorkItem.Validate()
WorkItem.Save()
WorkItem.State = "Resolved"
WorkItem.Fields("Resolved By").Value = ClosedBy
WorkItem.Fields("Resolved Reason").Value = "Just because"
Errors = WorkItem.Validate()
WorkItem.Save()
WorkItem.State = "Closed"
WorkItem.Fields("Closed By").Value = ClosedBy
Errors = WorkItem.Validate()
WorkItem.Save()
a source to share