Create task from template via API in Microsoft Team Foundation Server 2018












0















I'm creating an integration from our testing framework (Selenium) to Team Foundation Server (TFS) 2018 using C# (our tests are already written in it) - the integration will generate work items in TFS based on test results. I'd like to create the work item from a template, and I can't seem to find any documentation on doing so. I'm using the TFS client library from Microsoft found here.



I can pull a template from TFS:



var client = new HttpClient()
// Client auth stuff removed
var method = new HttpMethod("GET");
var httpRequestMessage = new HttpRequestMessage(method, "http://server:port/tfs/collection/team/project/_api/wit/templates/12345abc");
var httpResponseMessage = client.SendAsync(httpRequestMessage).Result;
WorkItemTemplate tfs_template = httpResponseMessage.Content.ReadAsAsync<WorkItemTemplate>().Result;


The API for creating new work items here looks fairly straightforward, but I can't find any way to connect the two actions, or a way to apply a template using this call. Is it possible to create a work item via API with a template, and if so, is there any documentation for it?










share|improve this question





























    0















    I'm creating an integration from our testing framework (Selenium) to Team Foundation Server (TFS) 2018 using C# (our tests are already written in it) - the integration will generate work items in TFS based on test results. I'd like to create the work item from a template, and I can't seem to find any documentation on doing so. I'm using the TFS client library from Microsoft found here.



    I can pull a template from TFS:



    var client = new HttpClient()
    // Client auth stuff removed
    var method = new HttpMethod("GET");
    var httpRequestMessage = new HttpRequestMessage(method, "http://server:port/tfs/collection/team/project/_api/wit/templates/12345abc");
    var httpResponseMessage = client.SendAsync(httpRequestMessage).Result;
    WorkItemTemplate tfs_template = httpResponseMessage.Content.ReadAsAsync<WorkItemTemplate>().Result;


    The API for creating new work items here looks fairly straightforward, but I can't find any way to connect the two actions, or a way to apply a template using this call. Is it possible to create a work item via API with a template, and if so, is there any documentation for it?










    share|improve this question



























      0












      0








      0








      I'm creating an integration from our testing framework (Selenium) to Team Foundation Server (TFS) 2018 using C# (our tests are already written in it) - the integration will generate work items in TFS based on test results. I'd like to create the work item from a template, and I can't seem to find any documentation on doing so. I'm using the TFS client library from Microsoft found here.



      I can pull a template from TFS:



      var client = new HttpClient()
      // Client auth stuff removed
      var method = new HttpMethod("GET");
      var httpRequestMessage = new HttpRequestMessage(method, "http://server:port/tfs/collection/team/project/_api/wit/templates/12345abc");
      var httpResponseMessage = client.SendAsync(httpRequestMessage).Result;
      WorkItemTemplate tfs_template = httpResponseMessage.Content.ReadAsAsync<WorkItemTemplate>().Result;


      The API for creating new work items here looks fairly straightforward, but I can't find any way to connect the two actions, or a way to apply a template using this call. Is it possible to create a work item via API with a template, and if so, is there any documentation for it?










      share|improve this question
















      I'm creating an integration from our testing framework (Selenium) to Team Foundation Server (TFS) 2018 using C# (our tests are already written in it) - the integration will generate work items in TFS based on test results. I'd like to create the work item from a template, and I can't seem to find any documentation on doing so. I'm using the TFS client library from Microsoft found here.



      I can pull a template from TFS:



      var client = new HttpClient()
      // Client auth stuff removed
      var method = new HttpMethod("GET");
      var httpRequestMessage = new HttpRequestMessage(method, "http://server:port/tfs/collection/team/project/_api/wit/templates/12345abc");
      var httpResponseMessage = client.SendAsync(httpRequestMessage).Result;
      WorkItemTemplate tfs_template = httpResponseMessage.Content.ReadAsAsync<WorkItemTemplate>().Result;


      The API for creating new work items here looks fairly straightforward, but I can't find any way to connect the two actions, or a way to apply a template using this call. Is it possible to create a work item via API with a template, and if so, is there any documentation for it?







      c# tfs tfs-workitem tfs-sdk






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 0:35







      chazbot7

















      asked Nov 16 '18 at 0:15









      chazbot7chazbot7

      361825




      361825
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You can not create work item from work item template. You can use work item templates to see which fields contains some default values. This example for new work item from template with rest api:



                  using Microsoft.TeamFoundation.Core.WebApi;
          using Microsoft.TeamFoundation.Core.WebApi.Types;
          using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
          using Microsoft.VisualStudio.Services.Common;
          using Microsoft.VisualStudio.Services.WebApi;
          using Microsoft.VisualStudio.Services.WebApi.Patch;
          using Microsoft.VisualStudio.Services.WebApi.Patch.Json;
          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;
          using System.Threading.Tasks;

          namespace ConsoleApp1
          {
          class Program
          {
          static string ServiceURL = "https://your_name.visualstudio.com";
          static string PAT = "your pat";
          static void Main(string args)
          {
          string projectName = "your project";
          string templateName = "Critical bug";

          /*connect to service. I use VSTS. In your case:
          VssConnection connection = new VssConnection(new Uri(ServiceURL), new VssCredential());
          https://docs.microsoft.com/ru-ru/azure/devops/integrate/get-started/client-libraries/samples?view=vsts
          */

          VssConnection connection = new VssConnection(new Uri(ServiceURL), new VssBasicCredential(string.Empty, PAT));
          //get clients
          var WitClient = connection.GetClient<WorkItemTrackingHttpClient>();
          var ProjectClient = connection.GetClient<ProjectHttpClient>();

          var project = ProjectClient.GetProject(projectName).Result;

          //get context for default project team
          TeamContext tmcntx = new TeamContext(project.Id, project.DefaultTeam.Id);

          //get all template for team
          var templates = WitClient.GetTemplatesAsync(tmcntx).Result;

          //get tempate through its name
          var id = (from tm in templates where tm.Name == templateName select tm.Id).FirstOrDefault();

          if (id != null)
          {
          var template = WitClient.GetTemplateAsync(tmcntx, id).Result;

          JsonPatchDocument patchDocument = new JsonPatchDocument();

          foreach (var key in template.Fields.Keys) //set default fields from template
          patchDocument.Add(new JsonPatchOperation()
          {

          Operation = Operation.Add,
          Path = "/fields/" + key,
          Value = template.Fields[key]

          });

          //add any additional fields
          patchDocument.Add(new JsonPatchOperation()
          {

          Operation = Operation.Add,
          Path = "/fields/System.Title",
          Value = "My critical bug"

          });

          var newWorkItem = WitClient.CreateWorkItemAsync(patchDocument, projectName, template.WorkItemTypeName).Result;
          }

          }
          }
          }


          My template:
          enter image description here






          share|improve this answer
























          • This is perfect, thank you!

            – chazbot7
            Nov 16 '18 at 20:03











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329661%2fcreate-task-from-template-via-api-in-microsoft-team-foundation-server-2018%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You can not create work item from work item template. You can use work item templates to see which fields contains some default values. This example for new work item from template with rest api:



                  using Microsoft.TeamFoundation.Core.WebApi;
          using Microsoft.TeamFoundation.Core.WebApi.Types;
          using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
          using Microsoft.VisualStudio.Services.Common;
          using Microsoft.VisualStudio.Services.WebApi;
          using Microsoft.VisualStudio.Services.WebApi.Patch;
          using Microsoft.VisualStudio.Services.WebApi.Patch.Json;
          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;
          using System.Threading.Tasks;

          namespace ConsoleApp1
          {
          class Program
          {
          static string ServiceURL = "https://your_name.visualstudio.com";
          static string PAT = "your pat";
          static void Main(string args)
          {
          string projectName = "your project";
          string templateName = "Critical bug";

          /*connect to service. I use VSTS. In your case:
          VssConnection connection = new VssConnection(new Uri(ServiceURL), new VssCredential());
          https://docs.microsoft.com/ru-ru/azure/devops/integrate/get-started/client-libraries/samples?view=vsts
          */

          VssConnection connection = new VssConnection(new Uri(ServiceURL), new VssBasicCredential(string.Empty, PAT));
          //get clients
          var WitClient = connection.GetClient<WorkItemTrackingHttpClient>();
          var ProjectClient = connection.GetClient<ProjectHttpClient>();

          var project = ProjectClient.GetProject(projectName).Result;

          //get context for default project team
          TeamContext tmcntx = new TeamContext(project.Id, project.DefaultTeam.Id);

          //get all template for team
          var templates = WitClient.GetTemplatesAsync(tmcntx).Result;

          //get tempate through its name
          var id = (from tm in templates where tm.Name == templateName select tm.Id).FirstOrDefault();

          if (id != null)
          {
          var template = WitClient.GetTemplateAsync(tmcntx, id).Result;

          JsonPatchDocument patchDocument = new JsonPatchDocument();

          foreach (var key in template.Fields.Keys) //set default fields from template
          patchDocument.Add(new JsonPatchOperation()
          {

          Operation = Operation.Add,
          Path = "/fields/" + key,
          Value = template.Fields[key]

          });

          //add any additional fields
          patchDocument.Add(new JsonPatchOperation()
          {

          Operation = Operation.Add,
          Path = "/fields/System.Title",
          Value = "My critical bug"

          });

          var newWorkItem = WitClient.CreateWorkItemAsync(patchDocument, projectName, template.WorkItemTypeName).Result;
          }

          }
          }
          }


          My template:
          enter image description here






          share|improve this answer
























          • This is perfect, thank you!

            – chazbot7
            Nov 16 '18 at 20:03
















          1














          You can not create work item from work item template. You can use work item templates to see which fields contains some default values. This example for new work item from template with rest api:



                  using Microsoft.TeamFoundation.Core.WebApi;
          using Microsoft.TeamFoundation.Core.WebApi.Types;
          using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
          using Microsoft.VisualStudio.Services.Common;
          using Microsoft.VisualStudio.Services.WebApi;
          using Microsoft.VisualStudio.Services.WebApi.Patch;
          using Microsoft.VisualStudio.Services.WebApi.Patch.Json;
          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;
          using System.Threading.Tasks;

          namespace ConsoleApp1
          {
          class Program
          {
          static string ServiceURL = "https://your_name.visualstudio.com";
          static string PAT = "your pat";
          static void Main(string args)
          {
          string projectName = "your project";
          string templateName = "Critical bug";

          /*connect to service. I use VSTS. In your case:
          VssConnection connection = new VssConnection(new Uri(ServiceURL), new VssCredential());
          https://docs.microsoft.com/ru-ru/azure/devops/integrate/get-started/client-libraries/samples?view=vsts
          */

          VssConnection connection = new VssConnection(new Uri(ServiceURL), new VssBasicCredential(string.Empty, PAT));
          //get clients
          var WitClient = connection.GetClient<WorkItemTrackingHttpClient>();
          var ProjectClient = connection.GetClient<ProjectHttpClient>();

          var project = ProjectClient.GetProject(projectName).Result;

          //get context for default project team
          TeamContext tmcntx = new TeamContext(project.Id, project.DefaultTeam.Id);

          //get all template for team
          var templates = WitClient.GetTemplatesAsync(tmcntx).Result;

          //get tempate through its name
          var id = (from tm in templates where tm.Name == templateName select tm.Id).FirstOrDefault();

          if (id != null)
          {
          var template = WitClient.GetTemplateAsync(tmcntx, id).Result;

          JsonPatchDocument patchDocument = new JsonPatchDocument();

          foreach (var key in template.Fields.Keys) //set default fields from template
          patchDocument.Add(new JsonPatchOperation()
          {

          Operation = Operation.Add,
          Path = "/fields/" + key,
          Value = template.Fields[key]

          });

          //add any additional fields
          patchDocument.Add(new JsonPatchOperation()
          {

          Operation = Operation.Add,
          Path = "/fields/System.Title",
          Value = "My critical bug"

          });

          var newWorkItem = WitClient.CreateWorkItemAsync(patchDocument, projectName, template.WorkItemTypeName).Result;
          }

          }
          }
          }


          My template:
          enter image description here






          share|improve this answer
























          • This is perfect, thank you!

            – chazbot7
            Nov 16 '18 at 20:03














          1












          1








          1







          You can not create work item from work item template. You can use work item templates to see which fields contains some default values. This example for new work item from template with rest api:



                  using Microsoft.TeamFoundation.Core.WebApi;
          using Microsoft.TeamFoundation.Core.WebApi.Types;
          using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
          using Microsoft.VisualStudio.Services.Common;
          using Microsoft.VisualStudio.Services.WebApi;
          using Microsoft.VisualStudio.Services.WebApi.Patch;
          using Microsoft.VisualStudio.Services.WebApi.Patch.Json;
          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;
          using System.Threading.Tasks;

          namespace ConsoleApp1
          {
          class Program
          {
          static string ServiceURL = "https://your_name.visualstudio.com";
          static string PAT = "your pat";
          static void Main(string args)
          {
          string projectName = "your project";
          string templateName = "Critical bug";

          /*connect to service. I use VSTS. In your case:
          VssConnection connection = new VssConnection(new Uri(ServiceURL), new VssCredential());
          https://docs.microsoft.com/ru-ru/azure/devops/integrate/get-started/client-libraries/samples?view=vsts
          */

          VssConnection connection = new VssConnection(new Uri(ServiceURL), new VssBasicCredential(string.Empty, PAT));
          //get clients
          var WitClient = connection.GetClient<WorkItemTrackingHttpClient>();
          var ProjectClient = connection.GetClient<ProjectHttpClient>();

          var project = ProjectClient.GetProject(projectName).Result;

          //get context for default project team
          TeamContext tmcntx = new TeamContext(project.Id, project.DefaultTeam.Id);

          //get all template for team
          var templates = WitClient.GetTemplatesAsync(tmcntx).Result;

          //get tempate through its name
          var id = (from tm in templates where tm.Name == templateName select tm.Id).FirstOrDefault();

          if (id != null)
          {
          var template = WitClient.GetTemplateAsync(tmcntx, id).Result;

          JsonPatchDocument patchDocument = new JsonPatchDocument();

          foreach (var key in template.Fields.Keys) //set default fields from template
          patchDocument.Add(new JsonPatchOperation()
          {

          Operation = Operation.Add,
          Path = "/fields/" + key,
          Value = template.Fields[key]

          });

          //add any additional fields
          patchDocument.Add(new JsonPatchOperation()
          {

          Operation = Operation.Add,
          Path = "/fields/System.Title",
          Value = "My critical bug"

          });

          var newWorkItem = WitClient.CreateWorkItemAsync(patchDocument, projectName, template.WorkItemTypeName).Result;
          }

          }
          }
          }


          My template:
          enter image description here






          share|improve this answer













          You can not create work item from work item template. You can use work item templates to see which fields contains some default values. This example for new work item from template with rest api:



                  using Microsoft.TeamFoundation.Core.WebApi;
          using Microsoft.TeamFoundation.Core.WebApi.Types;
          using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
          using Microsoft.VisualStudio.Services.Common;
          using Microsoft.VisualStudio.Services.WebApi;
          using Microsoft.VisualStudio.Services.WebApi.Patch;
          using Microsoft.VisualStudio.Services.WebApi.Patch.Json;
          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;
          using System.Threading.Tasks;

          namespace ConsoleApp1
          {
          class Program
          {
          static string ServiceURL = "https://your_name.visualstudio.com";
          static string PAT = "your pat";
          static void Main(string args)
          {
          string projectName = "your project";
          string templateName = "Critical bug";

          /*connect to service. I use VSTS. In your case:
          VssConnection connection = new VssConnection(new Uri(ServiceURL), new VssCredential());
          https://docs.microsoft.com/ru-ru/azure/devops/integrate/get-started/client-libraries/samples?view=vsts
          */

          VssConnection connection = new VssConnection(new Uri(ServiceURL), new VssBasicCredential(string.Empty, PAT));
          //get clients
          var WitClient = connection.GetClient<WorkItemTrackingHttpClient>();
          var ProjectClient = connection.GetClient<ProjectHttpClient>();

          var project = ProjectClient.GetProject(projectName).Result;

          //get context for default project team
          TeamContext tmcntx = new TeamContext(project.Id, project.DefaultTeam.Id);

          //get all template for team
          var templates = WitClient.GetTemplatesAsync(tmcntx).Result;

          //get tempate through its name
          var id = (from tm in templates where tm.Name == templateName select tm.Id).FirstOrDefault();

          if (id != null)
          {
          var template = WitClient.GetTemplateAsync(tmcntx, id).Result;

          JsonPatchDocument patchDocument = new JsonPatchDocument();

          foreach (var key in template.Fields.Keys) //set default fields from template
          patchDocument.Add(new JsonPatchOperation()
          {

          Operation = Operation.Add,
          Path = "/fields/" + key,
          Value = template.Fields[key]

          });

          //add any additional fields
          patchDocument.Add(new JsonPatchOperation()
          {

          Operation = Operation.Add,
          Path = "/fields/System.Title",
          Value = "My critical bug"

          });

          var newWorkItem = WitClient.CreateWorkItemAsync(patchDocument, projectName, template.WorkItemTypeName).Result;
          }

          }
          }
          }


          My template:
          enter image description here







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 12:28









          Shamrai AleksanderShamrai Aleksander

          1,651137




          1,651137













          • This is perfect, thank you!

            – chazbot7
            Nov 16 '18 at 20:03



















          • This is perfect, thank you!

            – chazbot7
            Nov 16 '18 at 20:03

















          This is perfect, thank you!

          – chazbot7
          Nov 16 '18 at 20:03





          This is perfect, thank you!

          – chazbot7
          Nov 16 '18 at 20:03




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329661%2fcreate-task-from-template-via-api-in-microsoft-team-foundation-server-2018%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Lugert, Oklahoma