Scott
Scott

Reputation: 195

How to programmatically close a TFS work item

I am attempting to import items from a legacy issue tracking system stored in an Excel sheet into Team Foundation Server. I loop through the rows of the Excel file successfully, and I can create new work items, but they are always in the Proposed state. If I attempt to change the state to Closed, then call the Validate method for the work item, I receive 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 have also tried the code below, attempting to save the work item, change the State to closed, and save it again, but this doesn't appear to work either - the State is still Proposed when I open it up under the My Work Items TFS query:

        WorkItem.Save()

        WorkItem.State = "Closed"
        WorkItem.Fields("Closed By").Value = ClosedBy
        WorkItem.Save()

Has anyone else tried such a thing and succeeded, or have ideas for doing it? Oh, and this is a CMMI task that I am trying to create and close. I wonder if I'm trying to skip over certain activities required by CMMI, but I'm new to this, and that's just a guess.

Upvotes: 2

Views: 3859

Answers (3)

Ravi Thapliyal
Ravi Thapliyal

Reputation: 1

The other way to work around this is to intsall "Process Editor".
Open the "WIT-WorkItem Type" from the server using visual studio.
Select the WorkItem to change from the list of Team Projects and edit the workflow by removing the assignedto=None and changing it to "Required".

Upvotes: 0

You should use the TFS Integration Platform for this.

http://tfsintegration.codeplex.com/

Upvotes: 0

Scott
Scott

Reputation: 195

I figured out how to create and close a TFS CMMI task programmatically. The key was to go through the CMMI process, which can be found at http://msdn.microsoft.com/en-us/library/bb668962.aspx, changing the State propery 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()

Upvotes: 5

Related Questions