Guest User

Untitled

a guest
Jan 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. static void Main(string[] args)
  2. {
  3. GetWI ex = new GetWI();
  4. ex.GetWorkItemsByWiql();
  5. }
  6. public void GetWorkItemsByWiql()
  7. {
  8. string _personalAccessToken = "xxxx";
  9. string _credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", _personalAccessToken)));
  10.  
  11. //this is needed because we want to create a project scoped query
  12. string project = "Agileportfolio";
  13.  
  14. //create wiql object
  15. var wiql = new
  16. {
  17. query = "Select [State], [Title] " +
  18. "From WorkItems " +
  19. "Where [Work Item Type] = 'Bug' " +
  20. "And [System.TeamProject] = '" + project + "' " +
  21. "And [System.State] = 'New' " +
  22. "Order By [State] Asc, [Changed Date] Desc"
  23. };
  24.  
  25. using (var client = new HttpClient())
  26. {
  27. client.BaseAddress = new Uri("https://test.visualstudio.com");
  28. client.DefaultRequestHeaders.Accept.Clear();
  29. client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
  30. client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials);
  31.  
  32. //serialize the wiql object into a json string
  33. var postValue = new StringContent(JsonConvert.SerializeObject(wiql), Encoding.UTF8, "application/json"); //mediaType needs to be application/json for a post call
  34.  
  35. var method = new HttpMethod("POST");
  36. var httpRequestMessage = new HttpRequestMessage(method, "https://abrahamdhanyaraj.visualstudio.com/_apis/wit/wiql?api-version=2.2") { Content = postValue };
  37. var httpResponseMessage = client.SendAsync(httpRequestMessage).Result;
  38.  
  39. if (httpResponseMessage.IsSuccessStatusCode)
  40. {
  41. WorkItemQueryResult workItemQueryResult = httpResponseMessage.Content.ReadAsAsync<WorkItemQueryResult>().Result;
  42.  
  43. //now that we have a bunch of work items, build a list of id's so we can get details
  44. var builder = new System.Text.StringBuilder();
  45. foreach (var item in workItemQueryResult.WorkItems)
  46. {
  47. builder.Append(item.Id.ToString()).Append(",");
  48. }
  49.  
  50. //clean up string of id's
  51. string ids = builder.ToString().TrimEnd(new char[] { ',' });
  52.  
  53. HttpResponseMessage getWorkItemsHttpResponse = client.GetAsync("_apis/wit/workitems?ids=" + ids + "&fields=System.Id,System.Title,System.State&asOf=" + workItemQueryResult.AsOf + "&api-version=2.2").Result;
  54.  
  55. if (getWorkItemsHttpResponse.IsSuccessStatusCode)
  56. {
  57. var result = getWorkItemsHttpResponse.Content.ReadAsStringAsync().Result;
  58. //Read title
  59.  
  60. }
  61.  
  62. }
  63.  
  64. // Create Channel
  65. string name = "xyzz3";
  66.  
  67. var payload = new
  68. {
  69. token = "xoxp-291239704800-292962676087-297314229698-a80e720d98e443c8afb0c4cb2c09e745",
  70. name = "xyzz3",
  71. };
  72. var serializedPayload = JsonConvert.SerializeObject(payload);
  73. var response = client.PostAsync("https://slack.com/api/channels.create" + "?token=test&name=" + name + "&pretty=1",
  74. new StringContent(serializedPayload, Encoding.UTF8, "application/json")).Result;
  75. if (response.IsSuccessStatusCode)
  76. {
  77. dynamic content = JsonConvert.DeserializeObject(
  78. response.Content.ReadAsStringAsync()
  79. .Result);
  80. }
  81.  
  82. }
  83.  
  84. }
Add Comment
Please, Sign In to add comment