DCSIMG
More About Creating Work Item Using TFS API - Shai Raiten

Shai Raiten

 Subscribe

More About Creating Work Item Using TFS API

More About Creating Work Item Using TFS API

In my last post How To: Create Generic Work Item Migration Tool For TFS I explained how to create Work Item Using TFS API.
In this post I'll show more options available using the API.
 
First create the connection to TFS and Work Item Store.
 

TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer(ServerName); WorkItemStore store = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));

WorkItemTypeCollection workItemTypes = store.Projects[TFSProject].WorkItemTypes;

 
Open The Work Item With PartialOpen()
Opens this WorkItem for editing without transferring all the data about the work item over the wire.
workitem.PartialOpen();
 
Copy Work Item
WorkItem NewWIT = workitem.Copy();

WorkItem NewWITTask = workitem.Copy(workItemTypes["Task"]);
 
Reset Work Item
Reverts all changes that were made to this WorkItem since the last revision.
workitem.Reset();
 
Related Link - Create a Work Item Relation between Work Items
RelatedLink rl = new RelatedLink(324);
workItem.Links.Add(rl);

Hyper Link - Create a Hyper Link

Hyperlink hl = new Hyperlink("http://blogs.microsoft.co.il/blogs/shair");
hl.Comment = "Shai Raiten Blog";

External Link - Create link to "Test Result" , "Changeset", "Version Item"

RegisteredLinkTypeCollection linkTypes = store.RegisteredLinkTypes;

RegisteredLinkType changeset = linkTypes["Changeset"];
RegisteredLinkType versionitem = linkTypes["Versioned Item"];
RegisteredLinkType testresult = linkTypes["Test Result"];

ExternalLink el1 = new ExternalLink(changeset, "53421");
ExternalLink el2 = new ExternalLink(versionitem, "$/BuildProject/Main/WindowsFormsApplication1/Form1.cs");
ExternalLink el3 = new ExternalLink(testresult, "UnitTest1 from tfsBuild@TFSRTM08 2008-09-24");

workItem.Links.Add(el1);
workItem.Links.Add(el2);
workItem.Links.Add(el3);

Remove Link

LinkCollection WITLinks = workitem.Links;
WITLinks.Remove(el1);
WITLinks.Remove(hl);

To add Iteration Path & Area Path you need to write the full path using \

workitem.IterationPath = @"Main\Project\Product\Item";
workitem.AreaPath = @"Main\Project\Product\Item";

Using Query to find Work Items

string query = "SELECT [System.Id] FROM WorkItems WHERE [System.TeamProject] = 'MyProject'";
WorkItemCollection witlist = store.Query(query);

Comments

Team System News said:

Brian Harry on Sharepoint Development with Team System Guidance begins to roll out and MSBuild Extension...

# October 15, 2008 9:30 AM

irol said:

hello,

about link , can i ask to you some problem?

link object,i can't use link.item(0).name or .vale

how to look this workitem id's link name?

thanks

# January 14, 2009 6:08 AM

Gavin Stevens said:

great stuff man, thanks for sharing..

# August 25, 2009 2:13 AM

credit repair service said:

Great site - I only wish I had found it sooner.

# September 26, 2009 8:01 AM

Makarand said:

Hi

I am new to TFS SDK API, I have a very basic query.

Suppose I built the Windows Form application to create WorkItem Types on TFS and if I want to distribute this Window Form to Users, what all assemblies (.dll) I have to ship with WindowForm Exe?

I tried it copying Microsoft.TeamFoundation.Client.dll and Microsoft.TeamFoundation.WorkItemTracking.Client.dll but it did not work.

Please help

Thanks

Mak

# October 25, 2009 3:38 AM

Shai Raiten said:

Hi,

You also need to TFS dlls, try adding Microsoft.TeamFoundation

Microsoft.TeamFoundation.Client

Microsoft.TeamFoundation.Common.dll

# November 16, 2009 9:17 AM

Shahaji said:

Hi,

Is it possible to add bug in TFS with the help TFS API. I want my custom bug tracking system, which has developed long back to talk with TFS.

Thanks.

# November 19, 2009 6:28 PM

Shahaji said:

Hi,

I really appreciate if you can help on this.

I have created issue tracking list in sharepoint project portal. This list will be used by Application Users to log issues. I expect that the issues logged with portal should be accessible through TeamExplorer in visual studio, so developer can take appropriate action. I added few issues through portal and few issues through TeamExplorer, but it looks like Portal issues and TeamExplorer issues are not in sync. I do understand that I must have done mistake. Please guide me on this.

Thanks,

Shahaji.

# November 19, 2009 9:21 PM

Stephen said:

I use the add RelatedLink code and no link is visible on either work item.

Am I missing something?  Is a save needed?

I tried doing a partial open on both workitems and then a save.  That does not make anything visible either....

I am confused.

# December 10, 2009 10:16 PM

Shai Raiten said:

Stephen ,

Please send me the code so I'll be able to help you on this issue.

THanks

# December 13, 2009 8:33 AM

Shai Raiten said:

Shahaji ,

I've blogged many posts on this topic, can you please let me know what is the main problem right now?

# December 13, 2009 8:36 AM

Stephen said:

Your site is the best out there for real TFS API examples.  Thanks so much.

If you have time and inclination, it would be nice to see an example on how to set the "Reason" field when moving between States (ie done, in progress).  (If you try to set it as you would any other field then it fails.)

# February 13, 2010 1:49 AM

Jason Castellano said:

Thanks for this post, it really helped me a long with my work item/changeset linking solution.  One obstacle that I had to figure out regarding changesets is the need for encoding the ArtifactUri prior to passing it in to the ExternalLink constructor.  Just using a changeset Id string created an invalid link on my work item.  The following block resolved the issue:

var changeSetId = new ArtifactId

{

    Tool = "VersionControl",

    ArtifactType = "ChangeSet",

    ToolSpecificId = CHANGESETID

};

var changesetLink = linkTypes["Fixed in Changeset"];

var uri = LinkingUtilities.EncodeUri(changeSetId);

var externalLink = new ExternalLink(changesetLink, uri);

# June 11, 2010 4:17 PM

Jed said:

In regards to linking changesets, it does appear that in TFS 2010 the link type has changed to "Fixed in Changeset".

According to Buck Hodge's comment back in 2006 at this blog post ( blogs.msdn.com/.../691625.aspx ), "The version control Changeset object has an ArtifactUri property that returns the artifact uri for that changeset.  While you could manually build the artifact uri, as shown in the sample code, it's best to avoid it and treat the uri as being opaque."

Therefore, to link a changeset:

var changeset = VersionControlServer.GetChangeset(changeSetIdInt);

workItem.Links.Add(new ExternalLink(linkTypes["Fixed in Changeset"], changeset.ArtifactUri.ToString()))

# February 21, 2011 10:41 PM

Barclaycard said:

This is a way to get everyone engaged so that the community can come together and solve problems as they emerge.

# October 22, 2011 4:51 PM

AS Lakshmi said:

VSTS has a default screen for creating new workitem.

Is it possible to launch this screen from a windows application?

I am assuming the deployment machine has VSTS 2010 installed.

# January 28, 2012 2:30 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: