<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sorted Bits &#187; Team Foundation Server</title>
	<atom:link href="http://www.sortedbits.com/tag/team-foundation-server/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sortedbits.com</link>
	<description>objectiveC, C# and more interesting stuff</description>
	<lastBuildDate>Wed, 04 Jan 2012 10:11:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Example: Add workitem to Team Foundation Server</title>
		<link>http://www.sortedbits.com/example-add-workitem-to-team-foundation-server/</link>
		<comments>http://www.sortedbits.com/example-add-workitem-to-team-foundation-server/#comments</comments>
		<pubDate>Sat, 03 Mar 2007 13:05:01 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Team Foundation Server]]></category>

		<guid isPermaLink="false">http://www.depl0y.com/personal/example-add-workitem-to-team-foundation-server</guid>
		<description><![CDATA[For my project at work I had to find out how to add a workitem to Team Foundation Server, through C#. Because I don&#8217;t want you to go through the same research as I did, I created a small example project which shows the basics. The code [...]]]></description>
			<content:encoded><![CDATA[<p>For my project at work I had to find out how to add a workitem to Team Foundation Server, through C#.  Because I don&#8217;t want you to go through the same research as I did, I created a small example project which shows the basics.</p>
<p>The code example at the end of this post shows how a connection is made to a Team Foundation Server (with the right credentials). After that the WorkItemStore is initialized, which we can use to create a Project object.</p>
<p><span id="more-19"></span><br />
[csharp]
Project tfsProject = workItemStore.Projects["Test"];<br />
[/csharp]
<p>On the line above you see how a Project object is created, the <em>Test</em> mentioned above is a currently existing TFS Project.</p>
[csharp]WorkItemType wiType = tfsProject.WorkItemTypes["task"];[/csharp]
<p>When a Project object is successfully made, we can use that to create a so-called WorkItemType. In this example we are going to be creating a <em>task</em> type. You can create a couple of different WorkItems, those are supplied by the TFS installation. It is also possible to create custom workitem types within TFS.</p>
[csharp]WorkItem workItem = new WorkItem(wiType);[/csharp]
<p>When the WorkItemType is created, we can use that to create a new object of the type WorkItem. This object has <a href="http://msdn2.microsoft.com/en-us/library/microsoft.teamfoundation.workitemtracking.client.workitem_members(VS.80).aspx">loads of different properties and methods</a>. A lot of those properties are names of fields in TFS from the standard workitem template.</p>
[csharp]
workItem.Title = &#8220;Normal workitem title&#8221;;<br />
workItem.Description = &#8220;The normal description&#8221;;<br />
[/csharp]
<p>Here we fill 2 standard fields, Title and Description. Yes, it&#8217;s easy like that <img src='http://www.sortedbits.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
[csharp]
// custom.comment is a custom field added to the &#8216;task&#8217; template from from the &#8216;Test&#8217; project<br />
workItem.Fields["custom.comment"].Value = &#8220;A different description!&#8221;;<br />
[/csharp]
<p>Because at my work we use a couple of custom fields in our template, we also need to be able to store data in there with our function. Well as you can see here I store <em>A different description</em> in a custom field with the reference name <em>custom.comment</em></p>
[csharp]ArrayList invalidFields = workItem.Validate();[/csharp]
<p>The validate method of the workitem creates an ArrayList which contains the names of fields that do not match the template&#8217;s requirements. When the .Count of the ArrayList is 0, all is OK.</p>
[csharp]workItem.Save();[/csharp]
<p>Save our workitem to TFS. That&#8217;s it, more isn&#8217;t to it. Well I posted the full source below and I also attached the project I used so you can download it.<br />
[csharp]
using System;<br />
using System.Text;<br />
using System.Collections;<br />
using Microsoft.TeamFoundation.Client;<br />
using Microsoft.TeamFoundation.WorkItemTracking.Client;<br />
using System.Net;</p>
<p>namespace Example_AddTFSWorkitem<br />
{<br />
	class Program<br />
	{<br />
		static void Main(string[] args)<br />
		{<br />
			NetworkCredential teamFoundationCredential = new NetworkCredential(&#8220;TeamFoundationUsername&#8221;, &#8220;TeamFoundationPassword&#8221;);<br />
			TeamFoundationServer tfsServer = new TeamFoundationServer(&#8220;http://tf-server:8080&#8243;, teamFoundationCredential);<br />
			tfsServer.EnsureAuthenticated();<br />
			WorkItemStore workItemStore = (WorkItemStore)tfsServer.GetService(typeof(WorkItemStore));<br />
			Project tfsProject = workItemStore.Projects["Test"];</p>
<p>			WorkItemType wiType = tfsProject.WorkItemTypes["task"];<br />
			WorkItem workItem = new WorkItem(wiType);</p>
<p>			workItem.Title = &#8220;Normal workitem title&#8221;;<br />
			workItem.Description = &#8220;The normal description&#8221;;</p>
<p>			// custom.comment is a custom field added to the &#8216;task&#8217; template from from the &#8216;Test&#8217; project<br />
			workItem.Fields["custom.comment"].Value = &#8220;A different description!&#8221;;</p>
<p>			ArrayList invalidFields = workItem.Validate();<br />
			if (invalidFields.Count &gt; 0)<br />
			{<br />
				StringBuilder invalidFieldString = new StringBuilder();</p>
<p>				for (int count = 0; count &lt; invalidFields.Count; count++)<br />
				{<br />
					Microsoft.TeamFoundation.WorkItemTracking.Client.Field test = invalidFields[count] as Microsoft.TeamFoundation.WorkItemTracking.Client.Field;<br />
					invalidFieldString.AppendLine(test.Name + &quot; has an invalid value (&quot; + test.Value + &quot;)&quot;);<br />
				}<br />
				// invalidFieldString StringBuilder now contains a list of fields not valid for your template<br />
				return;<br />
			}<br />
			else<br />
			{<br />
				workItem.Save();<br />
				return;<br />
			}<br />
		}<br />
	}<br />
}<br />
[/csharp]
<p><a href="http://www.dotnetkicks.com/kick/?url=http://www.depl0y.com/team-foundation-server/example-add-workitem-to-team-foundation-server"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.depl0y.com/team-foundation-server/example-add-workitem-to-team-foundation-server" border="0" alt="kick it on DotNetKicks.com" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sortedbits.com/example-add-workitem-to-team-foundation-server/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

