I'm creating a work item migration application from "something" to TFS 2013, and I want to have the TFS work items to be in corresponding workflow states as in source system. For instance, if the source work item is in "Closed" state, I want it to be in "Done" state in TFS.
I have followed advises in this article, which suggests to set BypassRules
property of the WorkItemStore
object to true
in order to be able to set CreatedDate
field. I suppose, the same applies to changing the workflow state, as it also requires bypassing rules.
So, I tried the following:
// obtain collection and authenticate towards it
var collection = new TfsTeamProjectCollection(new Uri(_tfsUrl), cred);
collection.Authenticate();
// get the work item store object
var store = new WorkItemStore(collection, WorkItemStoreFlags.BypassRules);
// creating the work item
var workItem = new WorkItem(store.Projects[_tfsProjectName].WorkItemTypes["Product Backlog Item"]);
// setting some standard fields
workItem.Title = "some name";
workItem.Description = "some description";
// validating the work item
if (workItem.Validate().Count > 0)
{
// throw validation rules violated
}
// saving the work item
workItem.Save();
As you can see, this sample doesn't violate any validation rules, and workItem.Validate().Count
returns 0
. But the call to workItem.Save()
throws the following exception:
Additional information: TF26212: Team Foundation Server could not save your changes. There may be problems with the work item type definition. Try again or contact your Team Foundation Server administrator.
I double checked that BypassRules
is set to true
right before the call to Save()
method. Besides, the workItem.IsValid
is also true
.
The interesting fact is that if I change the way I obtain the WorkItemStore
object, from
var store = new WorkItemStore(collection, WorkItemStoreFlags.BypassRules);
to
var store = collection.GetService<WorkItemStore>();
it can save without any problem! But in this case I don't know how to set BypassRules
to true
. This property is read-only when the WorkItemStore
object is created, and I get validation errors if I try to set workflow step to something other than "New".
So, my basic question is: how to create work items in TFS through API and be able to change State
field in this newly created item?