[TFS2010] HowTo: Create a WorkItem with custom fields using c#

image47dd1de4

Good,

While the object model in Visual Studio 2010 to work with Team Foundation Server 2010 is not very complex, there are some particularities to be taken into account. For example, when we want to create a WorkItem that we changed you the structure of fields.

For example, for the definition of a task we have added 2 more to the same text fields, named “version” and “scope”, and then we’ve added them visually to the edit form (if you want to see how it is possible to make this change, this post is for you). In the following image it is possible to see how the “version” field is required for the creation of a task.

image

If we take the example available from MSDN for the creation of a WorkItem using the Client Object Model, we see that the same not takes into account the use of custom fields. We modify it to save data from a task, as shown in the following code:

   1: using System;

2: using Microsoft.TeamFoundation.Client;

3: using Microsoft.TeamFoundation.WorkItemTracking.Client;

4: 

5: namespace ElBruno.CreateWiFromCode

6: {

7: class Program

8: {

9: static void Main(string[] args)

10: {

11: var collectionUri = new Uri(“http://localhost:8080/tfs/tpb”);

12: var tpc = new TfsTeamProjectCollection(collectionUri);

13: var workItemStore = tpc.GetService<WorkItemStore>();

14: var teamProject = workItemStore.Projects[“AgileLabs”];

15: var workItemType = teamProject.WorkItemTypes[“Task”];

16: 

17: var task = new WorkItem(workItemType)

18: {

19: Title = “Sample Title”,

20: Description = “Sample desc”

21: };

22: task.Save();

23: 

24: Console.WriteLine(@”WorkItem id: ” + task.Id);

25: Console.ReadLine();

26: }

27: }

28: }

When you want to save the WI line (22), we find the following exception:

image

   1: Microsoft.TeamFoundation.WorkItemTracking.Client.ValidationException was unhandled

2: Message=TF237124: Work Item is not ready to save

3: Source=Microsoft.TeamFoundation.WorkItemTracking.Client

4: IsRemoteException=false

5: ErrorId=0

6: LogException=false

7: StackTrace:

8: at Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem.Save(SaveFlags saveFlags)

9: at Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem.Save()

10: at ElBruno.CreateWiFromCode.Program.Main(String[] args) in

11: at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)

12: at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)

13: at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()

14: at System.Threading.ThreadHelper.ThreadStart_Context(Object state)

15: at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)

16: at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

17: at System.Threading.ThreadHelper.ThreadStart()

18: InnerException:

 

The solution is to define the value of the “version” field in order to thus fulfil the fields required for the task. One of the possible ways to view the status of an WI, is analyzing the IsDirty field of the same. In this case we can perform the following validation

   1: var task = new WorkItem(workItemType)

2: {

3: Title = “Sample Title”,

4: Description = “Sample desc”

5: };

6: if(task.IsDirty)

7: Console.WriteLine(“Can’t save WorkItem”);

8: else

9: task.Save();

 

The objective of the post was to explain how to add values to the custom fields, in this case the magic performed again, line 22.

   1: using System;

2: using Microsoft.TeamFoundation.Client;

3: using Microsoft.TeamFoundation.WorkItemTracking.Client;

4: 

5: namespace ElBruno.CreateWiFromCode

6: {

7: class Program

8: {

9: static void Main(string[] args)

10: {

11: var collectionUri = new Uri(“http://localhost:8080/tfs/tpb&#8221;);

12: var tpc = new TfsTeamProjectCollection(collectionUri);

13: var workItemStore = tpc.GetService<WorkItemStore>();

14: var teamProject = workItemStore.Projects[“AgileLabs”];

15: var workItemType = teamProject.WorkItemTypes[“Task”];

16: 

17: var task = new WorkItem(workItemType)

18: {

19: Title = “Sample Title”,

20: Description = “Sample desc”

21: };

22: task.Fields[“ElBruno.Version”].Value = “version 01”;

23: task.Save();

24: 

25: Console.WriteLine(@”WorkItem id: ” + task.Id);

26: Console.ReadLine();

27: }

28: }

29: }

 

Greetings @ Here

The Bruno

   

Source: http://msdn.microsoft.com/en-us/library/bb130322.aspx#Y697

One thought on “[TFS2010] HowTo: Create a WorkItem with custom fields using c#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.