Archivos para 4/08/11

[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”);

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

1 comentario

[TFS2010] HowTo: Crear un WorkItem con campos personalizados en C#

image47dd1de4

Buenas,

si bien el modelo de objetos para Visual Studio 2010 para trabajar con Team Foundation Server 2010 no es muy complejo, hay algunas particularidades a tener en cuenta. Por ejemplo, cuando queremos crear un WorkItem al que le hemos modificado su estructura de campos.

Por ejemplo, para la definición de una tarea hemos agregado 2 campos de texto más a la misma, llamados “Version” y “Scope”, y luego los hemos agregado visualmente al formulario de edición (si quieres ver como es posible realizar este cambio, este post es para ti). En al siguiente imagen es posible ver como el campo “versión” es requerido para la creación de una tarea.

image

Si tomamos el ejemplo disponible desde MSDN para la creaciòn de un WorkItem utilizando el Client Object Model, veremos que en el mismo no se tiene en cuenta la utilización de campos personalizados. Modificamos el mismo para que guarde los datos de una tarea, como muestra el siguiente código:

   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: }

 

 

Cuando se quiere guardar el WI (línea 22), nos encontramos con la siguiente excepción:

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: 

La solución consiste en definir el valor del campo “version” para poder de esta forma cumplir con los campos requeridos para la tarea. Una de las formas posibles de ver el estado de un WI, es analizando el campo IsDirty del mismo. Para este caso podemos realizar la siguiente validación

   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();

 

El objetivo del post era explicar como agregar valores a los campos personalizados, en este caso la magia la realiza una vez más, la línea 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");

  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: }

 

 

Saludos @ Here

El Bruno

   

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

1 comentario

Seguir

Get every new post delivered to your Inbox.

Únete a otros 898 seguidores