Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void Main(string[] args)
- {
- GetWI ex = new GetWI();
- ex.GetWorkItemsByWiql();
- }
- public void GetWorkItemsByWiql()
- {
- string _personalAccessToken = "xxxx";
- string _credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", _personalAccessToken)));
- //this is needed because we want to create a project scoped query
- string project = "Agileportfolio";
- //create wiql object
- var wiql = new
- {
- query = "Select [State], [Title] " +
- "From WorkItems " +
- "Where [Work Item Type] = 'Bug' " +
- "And [System.TeamProject] = '" + project + "' " +
- "And [System.State] = 'New' " +
- "Order By [State] Asc, [Changed Date] Desc"
- };
- using (var client = new HttpClient())
- {
- client.BaseAddress = new Uri("https://test.visualstudio.com");
- client.DefaultRequestHeaders.Accept.Clear();
- client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
- client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials);
- //serialize the wiql object into a json string
- var postValue = new StringContent(JsonConvert.SerializeObject(wiql), Encoding.UTF8, "application/json"); //mediaType needs to be application/json for a post call
- var method = new HttpMethod("POST");
- var httpRequestMessage = new HttpRequestMessage(method, "https://abrahamdhanyaraj.visualstudio.com/_apis/wit/wiql?api-version=2.2") { Content = postValue };
- var httpResponseMessage = client.SendAsync(httpRequestMessage).Result;
- if (httpResponseMessage.IsSuccessStatusCode)
- {
- WorkItemQueryResult workItemQueryResult = httpResponseMessage.Content.ReadAsAsync<WorkItemQueryResult>().Result;
- //now that we have a bunch of work items, build a list of id's so we can get details
- var builder = new System.Text.StringBuilder();
- foreach (var item in workItemQueryResult.WorkItems)
- {
- builder.Append(item.Id.ToString()).Append(",");
- }
- //clean up string of id's
- string ids = builder.ToString().TrimEnd(new char[] { ',' });
- HttpResponseMessage getWorkItemsHttpResponse = client.GetAsync("_apis/wit/workitems?ids=" + ids + "&fields=System.Id,System.Title,System.State&asOf=" + workItemQueryResult.AsOf + "&api-version=2.2").Result;
- if (getWorkItemsHttpResponse.IsSuccessStatusCode)
- {
- var result = getWorkItemsHttpResponse.Content.ReadAsStringAsync().Result;
- //Read title
- }
- }
- // Create Channel
- string name = "xyzz3";
- var payload = new
- {
- token = "xoxp-291239704800-292962676087-297314229698-a80e720d98e443c8afb0c4cb2c09e745",
- name = "xyzz3",
- };
- var serializedPayload = JsonConvert.SerializeObject(payload);
- var response = client.PostAsync("https://slack.com/api/channels.create" + "?token=test&name=" + name + "&pretty=1",
- new StringContent(serializedPayload, Encoding.UTF8, "application/json")).Result;
- if (response.IsSuccessStatusCode)
- {
- dynamic content = JsonConvert.DeserializeObject(
- response.Content.ReadAsStringAsync()
- .Result);
- }
- }
- }
Add Comment
Please, Sign In to add comment