开发者

Saving WorkItem#IterationPath on newly created Iteration

I can successfully create an iteration path via:

var commonservice = collection.GetService<ICommonStructureService>();

// create new area path and iteration path
var iterationRoot = commonservice.GetNodeFromPath("\\MyTeamProject\\Iteration");
var newIterationPath = commonser开发者_运维知识库vice.CreateNode("my new sprint", iterationRoot.Uri);

However, when I try and assign this path to a work item and save it the field doesn't validate.

If I run the tests again (with the iteration already created) the same code succeeds.

Does anybody know how to make this work?


This fixed it for me:

WorkItemStore wis = (WorkItemStore)tfsTeamProjColl.GetService(typeof(WorkItemStore));
wis.RefreshCache();
wis.SyncToCache();

Maybe it will help someone.


I experienced exactly the same behavior, and unfortunately @JWC answer didn't help. The solution which works for me can be found by this link.

So, this is a quick summary in case the original answer gets lost.

The key point is to use WorkItemServer class. It lives in the Microsoft.TeamFoundation.WorkItemTracking.Proxy.dll assembly.

First of all, you create a WorkItemStore instance:

var store = collection.GetService<WorkItemStore>();

Then, create necessary iteration paths:

var commonservice = collection.GetService<ICommonStructureService>();

var iterationRoot = commonservice.GetNodeFromPath("\\MyTeamProject\\Iteration");
var newIterationPath = commonservice.CreateNode("my sprint", iterationRoot.Uri);

Next, refresh the cache in TFS (I suspect this is similar to pressing F5 in web interface):

var wiServer = collection.GetService<WorkItemServer>();
wiServer.SyncExternalStructures(WorkItemServer.NewRequestId(), commonservice.GetProjectFromName("MyTeamProject").Uri);
store.RefreshCache();

And finally, assign newly created work item to the newly created iteration:

var wi = new WorkItem(store.Projects["MyTeamProject"].WorkItemTypes["Product Backlog Item"]);

wi.Title = "Hello from API";
wi.Description = "This work item was created from API";
wi.Fields["Assigned To"].Value = "Yan Sklyarenko";

wi.IterationPath = FormatPath(commonservice.GetNode(newIterationPath).Path, "Iteration", "MyTeamProject");

wi.Save();

That's it! The method FormatPath translates the iteration path to the form required by the work item IterationPath field, that is from \MyTeamProject\Iteration\my sprint to MyTeamProject\my sprint.

Hope this can save some time.

NOTE: I run this towards TFS 2013.


You are likely running into a caching issue. Try clearing the cache after you create the iteration. A couple things you could try:

  1. Get a new copy of the WorkItemStore.
  2. Disconnect and reconnect to TFS
  3. Check to see if there's a "refresh" method on either the WIS or on the TFS server objects. I've shut down my dev instance of TFS for the night, and I don't recall if there's anything like it.

If that's not quite it, post your code and I'll see if I can reproduce it.


I had the similar issue. I created Areapath and then created a query where the AreaPath was used. I did call store.RefreshCashe() but it did not work. Only in Debugger when I run store.RefreshCashe() two times manually. Thanks "Yan Sklyarenko". I tried your Suggestion and it works fine (TFS Server 2012).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜