How to Read and Write Data Through Web API in .NET Visual C# (Quick Start)

Tags API webapi

Overview

The steps and code offered below may give you a quick start on reading and writing data through the Web API and .NET Visual C#.

For more information about the API in general, please reference the Related Articles section, as well as the API and Web Services section of the KB.

Using the Code

The code will log you in, retrieve a ticket, update that ticket's title (it will append an "X"), and retrieve that ticket's feed items.

  1. Download and install the free latest Visual Studio (free) for Windows Desktop.
  2. Through it, start a new project.  Use the template for a new Visual C# Console Application project.  Set the name to be "TeamDynamixIntegration".  (If you don't use that name, then after step 6 you'll need to change the namespace to match whatever name you choose.)
  3. Download the TeamDynamix API .DLL (.NET 4.5) and save it in the the project's folder that contains the "program.cs" file.
    If you are using TeamDynamix Version 9.5 or later, you can download this DLL from TDNext > [Apps Menu] > Downloads > TeamDynamix API .DLL.
  4. Through the menu "Project", "Add Reference...".  "Browse" to the dll that you saved, select it, and click OK.
  5. Again through the menu "Project", "Manage NuGet Packages". Then search for Newtonsoft.Json and install it.
  6. Replace all your code in your "Program.cs" file with the code after the line below.
  7. Then, within your Progam.cs file, update the strings with the "QUICK START" comments.  Lines 29-33.
  8. Build the solution and start it. 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
// For the next line, install Json.NET through Project > Manage NuGet Packages
using Newtonsoft.Json;
// For the next line, download the api dll from the Downloads application
// into the same folder as this Program.cs file and add a project reference to it.
using TeamDynamix.Api;

namespace TeamDynamixIntegration
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("Starting.");
      RunAsync().Wait();
      Console.WriteLine("----\nPress enter to exit.");
      Console.ReadLine();
    }
    
    static async Task RunAsync()
    {
      using (var oHttpClientX = new HttpClient())
      {
        // QUICK START - Update these strings to match your environment
        string sLocationOrigin = "https://edmap.teamdynamix.com/";
        // QUICK START - Update this if necessary (it may not be necessary)
        string sWebApiBasePathname = "TDWebApi/api/";
        // QUICK START - Update this with the username for the user account you
        // want to use
        string sUsername = "xxxxxx@xxxxx.com";
        // QUICK START - Update this with the password for the user account you
        // want to use
        string sPassword = "xxxxxxxxxx";
        // QUICK START - Update this with one of your ticket id numbers
        string sTicketId = "284753";

        // FYI - This code uses the Web Api that is documented at
        // https://solutions.teamdynamix.com/TDWebApi
        // Login (authorize) and get a JSON Web Token to use in subsequent requests.
        // See the web api documentation at
        // https://solutions.teamdynamix.com/TDWebApi/Home/section/Auth#POSTapi/auth
        oHttpClientX.BaseAddress = new Uri(sLocationOrigin + sWebApiBasePathname);
        oHttpClientX.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        HttpResponseMessage oHttpResponseMsgX = await oHttpClientX.PostAsJsonAsync("auth", new { username = sUsername, password = sPassword } );
        string sResponse = oHttpResponseMsgX.Content.ReadAsStringAsync().Result;
        if (!oHttpResponseMsgX.IsSuccessStatusCode)
        {
          Console.WriteLine("ERROR.  StatusCode=" + oHttpResponseMsgX.StatusCode + ", ReasonPhrase=" + oHttpResponseMsgX.ReasonPhrase + ".");
          Console.WriteLine("Response content = " + sResponse);
          return;
        }
        oHttpClientX.DefaultRequestHeaders.Add("Authorization", "Bearer " + sResponse);

        // Get a ticket. See the web api documentation at
        // https://solutions.teamdynamix.com/TDWebApi/Home/section/Tickets#GETapi/{appId}/tickets/{id}
        oHttpResponseMsgX = await oHttpClientX.GetAsync("tickets/" + sTicketId);
        sResponse = oHttpResponseMsgX.Content.ReadAsStringAsync().Result;
        if (!oHttpResponseMsgX.IsSuccessStatusCode)
        {
          Console.WriteLine("ERROR.  StatusCode=" + oHttpResponseMsgX.StatusCode + ", ReasonPhrase=" + oHttpResponseMsgX.ReasonPhrase + ".");
          Console.WriteLine("Response content = " + sResponse );
          return;
        }
        TeamDynamix.Api.Tickets.Ticket oTicketX = JsonConvert.DeserializeObject<TeamDynamix.Api.Tickets.Ticket>(sResponse);

        // Now, all of the "Ticket" properties are available. See the property list at
        // https://solutions.teamdynamix.com/TDWebApi/Home/type/TeamDynamix.Api.Tickets.Ticket
        Console.WriteLine("Ticket ID = " + oTicketX.ID);
        Console.WriteLine("Ticket Title = " + oTicketX.Title );
        Console.WriteLine("Ticket URL = " + sLocationOrigin + "/TDNext/Apps/Tickets/TicketDet?TicketDecID=" + oTicketX.ID);

        // Update that same ticket.  (Just add an "X" to the end of the title.)
        // See the web api documentation at https://solutions.teamdynamix.com/TDWebApi/Home/section/Tickets#POSTapi/{appId}/tickets?EnableNotifyReviewer={EnableNotifyReviewer}&NotifyRequestor={NotifyRequestor}&NotifyResponsible={NotifyResponsible}&AutoAssignResponsibility={AutoAssignResponsibility}&AllowRequestorCreation={AllowRequestorCreation}
        oTicketX.Title = oTicketX.Title + "X";
        oHttpResponseMsgX = await oHttpClientX.PostAsJsonAsync("tickets/" + sTicketId, oTicketX);
        sResponse = oHttpResponseMsgX.Content.ReadAsStringAsync().Result;
        if (!oHttpResponseMsgX.IsSuccessStatusCode)
        {
          Console.WriteLine("ERROR.  Response Message = " + oHttpResponseMsgX.ToString());
          Console.WriteLine("Response content = " + sResponse);
          return;
        }

        // Get that same ticket's feed. See web api documentation at
        // https://solutions.teamdynamix.com/TDWebApi/Home/section/Tickets#GETapi/{appId}/tickets/{id}/feed
        oHttpResponseMsgX = await oHttpClientX.GetAsync("tickets/" + sTicketId + "/feed");
        sResponse = oHttpResponseMsgX.Content.ReadAsStringAsync().Result;
        if (!oHttpResponseMsgX.IsSuccessStatusCode)
        {
          Console.WriteLine("ERROR.  StatusCode=" + oHttpResponseMsgX.StatusCode + ", ReasonPhrase=" + oHttpResponseMsgX.ReasonPhrase + ".");
          Console.WriteLine("Response content = " + sResponse);
          return;
        }
        TeamDynamix.Api.Feed.ItemUpdate[] aItemUpdateX = JsonConvert.DeserializeObject<TeamDynamix.Api.Feed.ItemUpdate[]>(sResponse);

        // Now, all of the Ticket Feed "ItemUpdate" properties are available in an array of Ticket Feed ItemUpdates.
        foreach (TeamDynamix.Api.Feed.ItemUpdate oItemUpdateX in aItemUpdateX)
        {
          // For feed item properties, see https://solutions.teamdynamix.com/TDWebApi/Home/type/TeamDynamix.Api.Feed.ItemUpdate
          Console.WriteLine("Feed Item CreatedDate = " + oItemUpdateX.CreatedDate);
        }
      }
    }
  }
}
100% helpful - 1 review

Details

Article ID: 7693
Created
Fri 7/17/15 10:57 AM
Modified
Fri 11/17/23 10:39 AM