Search code examples
c#apitfsdefects

How to create a bug in TFS Programmatically


I'm looking for a small code snippet which helps me to create a defect in TFS Programmatically using C# VSTS2015.

My TFS Server: http://tfs2.dell.com:8080/tfs/eDell/eDellPrograms/. server is: http://tfs2.dell.com:8080/tfs. Collection is: eDell. Project is: eDellPrograms.

Work Item = Defect.


Solution

  • You have 2 options:

    1. TFS Soap API (SDK)
    2. TFS Rest API

    For TFS Soap API (SDK) you need these DLL's:

    Microsoft.TeamFoundation.Client;
    Microsoft.TeamFoundation.WorkItemTracking.Client;
    

    The code is:

    using System;
    using Microsoft.TeamFoundation.Client;
    using Microsoft.TeamFoundation.WorkItemTracking.Client;
    
    namespace createNewWorkItem
    {
        class Program
        {
            static int Main(string[] args)
            {
                Uri collectionUri = new Uri("http://server:8080/TFS/");
                TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(collectionUri);
                WorkItemStore workItemStore = tpc.GetService<WorkItemStore>();
                Project teamProject = workItemStore.Projects["MyProject"];
                WorkItemType workItemType = teamProject.WorkItemTypes["Defect"];
    
                WorkItem Defect = new WorkItem(workItemType);
    
                Defect.Title = "TITLE GOES HERE";
                Defect.Description = "DESCRIPTION GOES HERE";
                Defect.Fields["Issue ID"].Value = "999999";
    
    
                Defect.Save();
                return (Defect.Id);
    
            }
        }
    
    }
    

    If you want use Rest API you don't need the above DLL's.

    The code is:

    public static async void createtWorkItem()
    {
            string requestUrl = "http://TFS2015servername:8080/tfs/{collectionname}/{teamprojectname}/_apis/wit/workitems/$Defect?api-version=1.0";
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string json = serializer.Serialize(new object[]{new
            {
                op = "add",
                path = "/fields/System.Title",
                value = "New Task from TFS 2015 REST API"
            }});
    
            HttpClientHandler authtHandler = new HttpClientHandler()
            {
               // Credentials = CredentialCache.DefaultNetworkCredentials
               Credentials = new NetworkCredential("username", "password", "domainname")
            };
    
            using (HttpClient client = new HttpClient(authtHandler))
            {
                var method = new HttpMethod("PATCH");
    
                var request = new HttpRequestMessage(method, requestUrl)
                {
                    Content = new StringContent(json, Encoding.UTF8,
                        "application/json-patch+json")
                };
            HttpResponseMessage hrm = await client.SendAsync(request);
            }
    
            Console.WriteLine("Completed!");
        };
    }