From b8f9094034aefe50952ed533added9cf985298c4 Mon Sep 17 00:00:00 2001 From: Will Smythe Date: Mon, 27 Aug 2018 16:10:05 -0400 Subject: [PATCH 1/8] Lookup org URL by name, ID, etc Add org URL helper: --- .../netcore/ShowWorkItemConsole/Program.cs | 80 ++++++++++++------- .../ShowWorkItemConsole.csproj | 4 + Helpers/Helpers.csproj | 11 +++ Helpers/OrganizationUrlHelpers.cs | 71 ++++++++++++++++ 4 files changed, 136 insertions(+), 30 deletions(-) create mode 100644 Helpers/Helpers.csproj create mode 100644 Helpers/OrganizationUrlHelpers.cs diff --git a/ClientLibrary/Quickstarts/netcore/ShowWorkItemConsole/Program.cs b/ClientLibrary/Quickstarts/netcore/ShowWorkItemConsole/Program.cs index 593c55f8..9c4ab86c 100644 --- a/ClientLibrary/Quickstarts/netcore/ShowWorkItemConsole/Program.cs +++ b/ClientLibrary/Quickstarts/netcore/ShowWorkItemConsole/Program.cs @@ -1,51 +1,71 @@ -using Microsoft.TeamFoundation.WorkItemTracking.WebApi; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Runtime.Serialization.Json; +using System.Threading.Tasks; + +using Microsoft.TeamFoundation.WorkItemTracking.WebApi; using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models; + using Microsoft.VisualStudio.Services.Common; using Microsoft.VisualStudio.Services.WebApi; -using System; -namespace ConsoleApp +using Samples.Helpers; + +namespace Samples.ClientLibrary.Quickstarts.ShowWorkItemConsole { class Program { static void Main(string[] args) { - if (args.Length == 3) - { - Uri accountUri = new Uri(args[0]); // Account URL, for example: https://fabrikam.visualstudio.com - String personalAccessToken = args[1]; // See https://www.visualstudio.com/docs/integrate/get-started/authentication/pats - int workItemId = int.Parse(args[2]); // ID of a work item, for example: 12 + string organizationName = args[0]; // Organization (formerly "account") name, for example: "fabrikam" + string accessToken = args[1]; // Personal access token. See https://docs.microsoft.com/vsts/integrate/get-started/authentication/pats?view=vsts + int workItemId = int.Parse(args[2]); // Work item ID, for example: 12 - // Create a connection to the account - VssConnection connection = new VssConnection(accountUri, new VssBasicCredential(string.Empty, personalAccessToken)); - - // Get an instance of the work item tracking client - WorkItemTrackingHttpClient witClient = connection.GetClient(); + try + { + WorkItem workitem = GetWorkItem(organizationName, accessToken, workItemId).GetAwaiter().GetResult(); - try + // Output the work item's field values + foreach (var field in workitem.Fields) { - // Get the specified work item - WorkItem workitem = witClient.GetWorkItemAsync(workItemId).Result; - - // Output the work item's field values - foreach (var field in workitem.Fields) - { - Console.WriteLine(" {0}: {1}", field.Key, field.Value); - } + Console.WriteLine($" {field.Key}: {field.Value}"); } - catch (AggregateException aex) + } + catch (OrganizationNotFoundException onfe) + { + Console.WriteLine($"Could not find organizatiokn {organizationName}: {onfe.Message}"); + } + catch (AggregateException aex) + { + VssServiceException vssex = aex.InnerException as VssServiceException; + if (vssex != null) { - VssServiceException vssex = aex.InnerException as VssServiceException; - if (vssex != null) - { - Console.WriteLine(vssex.Message); - } + Console.WriteLine(vssex.Message); } } - else + catch (Exception ex) { - Console.WriteLine("Usage: ConsoleApp {accountUri} {personalAccessToken} {workItemId}"); + Console.WriteLine("Exception occurred: " + ex.Message); } } + + static async Task GetWorkItem(string organizationName, string accessToken, int workItemId) + { + // Get the connection URL for the specified VSTS organization + Uri organizationUrl = await OrganizationUrlHelpers.GetConnectionUrl(organizationName); + + // Create a connection to the organization + VssConnection connection = new VssConnection(organizationUrl, new VssBasicCredential(string.Empty, accessToken)); + + // Get an instance of the work item tracking client + WorkItemTrackingHttpClient witClient = connection.GetClient(); + + // Return the specified work item + return await witClient.GetWorkItemAsync(workItemId); + } } } \ No newline at end of file diff --git a/ClientLibrary/Quickstarts/netcore/ShowWorkItemConsole/ShowWorkItemConsole.csproj b/ClientLibrary/Quickstarts/netcore/ShowWorkItemConsole/ShowWorkItemConsole.csproj index 6bc1fb8d..fc1680de 100644 --- a/ClientLibrary/Quickstarts/netcore/ShowWorkItemConsole/ShowWorkItemConsole.csproj +++ b/ClientLibrary/Quickstarts/netcore/ShowWorkItemConsole/ShowWorkItemConsole.csproj @@ -9,4 +9,8 @@ + + + + diff --git a/Helpers/Helpers.csproj b/Helpers/Helpers.csproj new file mode 100644 index 00000000..2aaac964 --- /dev/null +++ b/Helpers/Helpers.csproj @@ -0,0 +1,11 @@ + + + + netstandard2.0 + + + + + + + diff --git a/Helpers/OrganizationUrlHelpers.cs b/Helpers/OrganizationUrlHelpers.cs new file mode 100644 index 00000000..8635f290 --- /dev/null +++ b/Helpers/OrganizationUrlHelpers.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Threading.Tasks; + +using Microsoft.VisualStudio.Services.Common; +using Microsoft.VisualStudio.Services.Location; +using Microsoft.VisualStudio.Services.WebApi; + +namespace Samples.Helpers +{ + /// + /// Helper methods for building URLs to VSTS organization ("account") resources. + /// + public static class OrganizationUrlHelpers + { + /// + /// Gets the connection URL for the specified VSTS organization name. + /// + public static async Task GetConnectionUrl(string organizationName) + { + try + { + HttpResponseMessage response = await s_client.GetAsync($"https://app.vssps.visualstudio.com/_apis/resourceAreas?accountName={organizationName}"); + + var wrapper = await response.Content.ReadAsAsync>>(); + + return new Uri(wrapper.Value.First(resourceArea => resourceArea.Id == s_coreResourceAreaId).LocationUrl); + } + catch (Exception ex) + { + throw new OrganizationNotFoundException(organizationName, ex); + } + } + + /// + /// Gets the connection URL for the specified VSTS organization ID. + /// + public static async Task GetConnectionUrl(Guid organizationId) + { + try + { + HttpResponseMessage response = await s_client.GetAsync($"https://app.vssps.visualstudio.com/_apis/resourceAreas?hostId={organizationId}"); + + var wrapper = await response.Content.ReadAsAsync>>(); + + return new Uri(wrapper.Value.First(resourceArea => resourceArea.Id == s_coreResourceAreaId).LocationUrl); + } + catch (Exception ex) + { + throw new OrganizationNotFoundException(organizationId.ToString(), ex); + } + } + + private static readonly HttpClient s_client = new HttpClient(); + + public static readonly Guid s_coreResourceAreaId = Guid.Parse("79134C72-4A58-4B42-976C-04E7115F32BF"); + } + + public class OrganizationNotFoundException : Exception + { + public OrganizationNotFoundException(string message, Exception ex) + : base(message, ex) + { + } + } + +} From 3bc2198b839b6495f4c3d670ee07e0bd5c4a8990 Mon Sep 17 00:00:00 2001 From: Will Smythe Date: Mon, 27 Aug 2018 17:30:24 -0400 Subject: [PATCH 2/8] Update readmes --- .../netcore/ShowWorkItemConsole/README.md | 8 +++- Helpers/README.md | 37 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 Helpers/README.md diff --git a/ClientLibrary/Quickstarts/netcore/ShowWorkItemConsole/README.md b/ClientLibrary/Quickstarts/netcore/ShowWorkItemConsole/README.md index fa13dbf3..bff9a9f0 100644 --- a/ClientLibrary/Quickstarts/netcore/ShowWorkItemConsole/README.md +++ b/ClientLibrary/Quickstarts/netcore/ShowWorkItemConsole/README.md @@ -6,11 +6,15 @@ This sample shows how to use the Work Item Tracking client, but other clients (b ## How to run -1. If you do not already have a Visual Studio Team Services account, [create one](https://docs.microsoft.com/vsts/organizations/accounts/create-account-msa-or-work-student?view=vsts) (it's free) +1. If you do not already have a Visual Studio Team Services account, [create one](https://docs.microsoft.com/vsts/organizations/accounts/create-organization-msa-or-work-student?view=vsts) (it's free) + 2. Create a work item in your account + 3. Create a personal access token ([steps](https://docs.microsoft.com/vsts/organizations/accounts/use-personal-access-tokens-to-authenticate?view=vsts)) + 4. Install [.NET SDK (2.0 or later)](https://microsoft.com/net/core) + 5. Build `dotnet build` > Note: you may see warnings about certain dependencies not being fully compatible with the project. This is expected and is due to some dependencies not explicitly listing .NET Standard or .NET Core as a supported framework. -6. Run `dotnet run https://{account}.visualstudio.com {token} {workItemId}` (substituting these values for your account name, your personal access token, and a valid work item ID) +6. Run `dotnet run {organizationName} {token} {workItemId}` (substituting these values for your VSTS organization name, your personal access token, and a valid work item ID) diff --git a/Helpers/README.md b/Helpers/README.md new file mode 100644 index 00000000..722c0551 --- /dev/null +++ b/Helpers/README.md @@ -0,0 +1,37 @@ +# Helpers + +This project include various useful "helper" samples. + +## OrganizationUrlHelpers + +Provides sample helper methods for properly constructing URLs to VSTS organizations (formerly "account") from an organizastion ID or name. + +### Create a connection to an organization using its name + +```cs +public async Task DoSomething() +{ + Uri orgUrl = OrganizationUrlHelpers + .GetConnectionUrl("myOrgName"); // https://myorgname.visualstudio.com + + VssConnection orgConnection = new VssConnection(orgUrl, credentials); + + BuildHttpClient buildClient = orgConnection.GetClient(); +} +``` + +### Create a connection to an organization using its ID + +```cs +public async Task DoSomething() +{ + Guid orgId = Guid.Parse("279ed1c4-2f72-4e61-8c95-4bafcc13fd02"); // ID for the "MyOrgName" organzation + + Uri orgUrl = OrganizationUrlHelpers + .GetConnectionUrl(orgId); // https://myorgname.visualstudio.com + + VssConnection orgConnection = new VssConnection(orgUrl, credentials); + + BuildHttpClient buildClient = orgConnection.GetClient(); +} +``` From 845cb01f440b29b66f1cfa4d49997350aa47809b Mon Sep 17 00:00:00 2001 From: Will Smythe Date: Mon, 27 Aug 2018 17:44:24 -0400 Subject: [PATCH 3/8] Add api-version param --- Helpers/OrganizationUrlHelpers.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Helpers/OrganizationUrlHelpers.cs b/Helpers/OrganizationUrlHelpers.cs index 8635f290..69414e37 100644 --- a/Helpers/OrganizationUrlHelpers.cs +++ b/Helpers/OrganizationUrlHelpers.cs @@ -24,7 +24,7 @@ public static async Task GetConnectionUrl(string organizationName) { try { - HttpResponseMessage response = await s_client.GetAsync($"https://app.vssps.visualstudio.com/_apis/resourceAreas?accountName={organizationName}"); + HttpResponseMessage response = await s_client.GetAsync($"https://app.vssps.visualstudio.com/_apis/resourceAreas?accountName={organizationName}&api-version=5.0-preview.1"); var wrapper = await response.Content.ReadAsAsync>>(); @@ -43,7 +43,7 @@ public static async Task GetConnectionUrl(Guid organizationId) { try { - HttpResponseMessage response = await s_client.GetAsync($"https://app.vssps.visualstudio.com/_apis/resourceAreas?hostId={organizationId}"); + HttpResponseMessage response = await s_client.GetAsync($"https://app.vssps.visualstudio.com/_apis/resourceAreas?hostId={organizationId}&api-version=5.0-preview.1"); var wrapper = await response.Content.ReadAsAsync>>(); From 58aeec63a1905fa9d2494cd0bfa1596daa25c202 Mon Sep 17 00:00:00 2001 From: Will Smythe Date: Tue, 28 Aug 2018 17:38:09 -0400 Subject: [PATCH 4/8] Simplify org url helpers --- .../netcore/ShowWorkItemConsole/Program.cs | 5 +- Helpers/OrganizationUrlHelpers.cs | 65 +++++++++---------- 2 files changed, 33 insertions(+), 37 deletions(-) diff --git a/ClientLibrary/Quickstarts/netcore/ShowWorkItemConsole/Program.cs b/ClientLibrary/Quickstarts/netcore/ShowWorkItemConsole/Program.cs index 9c4ab86c..95ff9b98 100644 --- a/ClientLibrary/Quickstarts/netcore/ShowWorkItemConsole/Program.cs +++ b/ClientLibrary/Quickstarts/netcore/ShowWorkItemConsole/Program.cs @@ -6,11 +6,10 @@ using System.Net.Http.Headers; using System.Runtime.Serialization.Json; using System.Threading.Tasks; - using Microsoft.TeamFoundation.WorkItemTracking.WebApi; using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models; - using Microsoft.VisualStudio.Services.Common; +using Microsoft.VisualStudio.Services.Organization; using Microsoft.VisualStudio.Services.WebApi; using Samples.Helpers; @@ -56,7 +55,7 @@ static void Main(string[] args) static async Task GetWorkItem(string organizationName, string accessToken, int workItemId) { // Get the connection URL for the specified VSTS organization - Uri organizationUrl = await OrganizationUrlHelpers.GetConnectionUrl(organizationName); + Uri organizationUrl = await OrganizationUrlHelpers.GetUrl(organizationName); // Create a connection to the organization VssConnection connection = new VssConnection(organizationUrl, new VssBasicCredential(string.Empty, accessToken)); diff --git a/Helpers/OrganizationUrlHelpers.cs b/Helpers/OrganizationUrlHelpers.cs index 69414e37..fe1d989f 100644 --- a/Helpers/OrganizationUrlHelpers.cs +++ b/Helpers/OrganizationUrlHelpers.cs @@ -5,9 +5,9 @@ using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; - using Microsoft.VisualStudio.Services.Common; using Microsoft.VisualStudio.Services.Location; +using Microsoft.VisualStudio.Services.Organization; using Microsoft.VisualStudio.Services.WebApi; namespace Samples.Helpers @@ -20,52 +20,49 @@ public static class OrganizationUrlHelpers /// /// Gets the connection URL for the specified VSTS organization name. /// - public static async Task GetConnectionUrl(string organizationName) + public static async Task GetUrl(string organizationName) { - try - { - HttpResponseMessage response = await s_client.GetAsync($"https://app.vssps.visualstudio.com/_apis/resourceAreas?accountName={organizationName}&api-version=5.0-preview.1"); - - var wrapper = await response.Content.ReadAsAsync>>(); + string requestUrl = $"{s_locationServiceUrl}/_apis/resourceAreas/{s_defaultResourceAreaId}/?accountName={organizationName}&api-version=5.0-preview.1"; - return new Uri(wrapper.Value.First(resourceArea => resourceArea.Id == s_coreResourceAreaId).LocationUrl); - } - catch (Exception ex) + HttpResponseMessage response = await s_client.GetAsync(requestUrl); + + if (response.StatusCode == HttpStatusCode.OK) { - throw new OrganizationNotFoundException(organizationName, ex); - } + ResourceAreaInfo value = await response.Content.ReadAsAsync(); + + if (value != null) + { + return new Uri(value.LocationUrl); + } + }; + + throw new OrganizationNotFoundException(organizationName); } /// /// Gets the connection URL for the specified VSTS organization ID. /// - public static async Task GetConnectionUrl(Guid organizationId) + public static async Task GetUrl(Guid organizationId) { - try - { - HttpResponseMessage response = await s_client.GetAsync($"https://app.vssps.visualstudio.com/_apis/resourceAreas?hostId={organizationId}&api-version=5.0-preview.1"); - - var wrapper = await response.Content.ReadAsAsync>>(); + string requestUrl = $"{s_locationServiceUrl}/_apis/resourceAreas/{s_defaultResourceAreaId}/?hostid={organizationId}&api-version=5.0-preview.1"; - return new Uri(wrapper.Value.First(resourceArea => resourceArea.Id == s_coreResourceAreaId).LocationUrl); - } - catch (Exception ex) + HttpResponseMessage response = await s_client.GetAsync(requestUrl); + + if (response.StatusCode == HttpStatusCode.OK) { - throw new OrganizationNotFoundException(organizationId.ToString(), ex); - } - } + ResourceAreaInfo value = await response.Content.ReadAsAsync(); - private static readonly HttpClient s_client = new HttpClient(); + if (value != null) + { + return new Uri(value.LocationUrl); + } + }; - public static readonly Guid s_coreResourceAreaId = Guid.Parse("79134C72-4A58-4B42-976C-04E7115F32BF"); - } - - public class OrganizationNotFoundException : Exception - { - public OrganizationNotFoundException(string message, Exception ex) - : base(message, ex) - { + throw new OrganizationNotFoundException(organizationId.ToString()); } - } + private static readonly HttpClient s_client = new HttpClient(); + private static readonly string s_locationServiceUrl = "https://app.vssps.visualstudio.com"; + private static readonly Guid s_defaultResourceAreaId = Guid.Parse("79134C72-4A58-4B42-976C-04E7115F32BF"); + } } From d7be334418f7347e479542513056ac33c633d264 Mon Sep 17 00:00:00 2001 From: Will Smythe Date: Wed, 29 Aug 2018 15:41:34 -0400 Subject: [PATCH 5/8] More updates to 'org URL helper' sample --- .../netcore/ShowWorkItemConsole/Program.cs | 25 +++++-------- Helpers/Helpers.csproj | 8 +++- Helpers/OrganizationUrlHelpers.cs | 37 +++++++------------ 3 files changed, 29 insertions(+), 41 deletions(-) diff --git a/ClientLibrary/Quickstarts/netcore/ShowWorkItemConsole/Program.cs b/ClientLibrary/Quickstarts/netcore/ShowWorkItemConsole/Program.cs index 95ff9b98..9cb737ea 100644 --- a/ClientLibrary/Quickstarts/netcore/ShowWorkItemConsole/Program.cs +++ b/ClientLibrary/Quickstarts/netcore/ShowWorkItemConsole/Program.cs @@ -1,26 +1,25 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Net.Http; -using System.Net.Http.Headers; -using System.Runtime.Serialization.Json; using System.Threading.Tasks; using Microsoft.TeamFoundation.WorkItemTracking.WebApi; using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models; using Microsoft.VisualStudio.Services.Common; -using Microsoft.VisualStudio.Services.Organization; using Microsoft.VisualStudio.Services.WebApi; using Samples.Helpers; namespace Samples.ClientLibrary.Quickstarts.ShowWorkItemConsole { + /// + /// Simple .NET console program that shows information about a VSTS work item. + /// + /// Usage: + /// ShowWorkItemConsole [organizationName] [personalAccessToken] [workItemNumber] + /// class Program { static void Main(string[] args) { - string organizationName = args[0]; // Organization (formerly "account") name, for example: "fabrikam" + string organizationName = args[0]; // Organization (formerly called "account") name, for example: "fabrikam" string accessToken = args[1]; // Personal access token. See https://docs.microsoft.com/vsts/integrate/get-started/authentication/pats?view=vsts int workItemId = int.Parse(args[2]); // Work item ID, for example: 12 @@ -34,10 +33,6 @@ static void Main(string[] args) Console.WriteLine($" {field.Key}: {field.Value}"); } } - catch (OrganizationNotFoundException onfe) - { - Console.WriteLine($"Could not find organizatiokn {organizationName}: {onfe.Message}"); - } catch (AggregateException aex) { VssServiceException vssex = aex.InnerException as VssServiceException; @@ -53,8 +48,8 @@ static void Main(string[] args) } static async Task GetWorkItem(string organizationName, string accessToken, int workItemId) - { - // Get the connection URL for the specified VSTS organization + { + // Get the connection URL for the specified VSTS organization name Uri organizationUrl = await OrganizationUrlHelpers.GetUrl(organizationName); // Create a connection to the organization @@ -63,7 +58,7 @@ static async Task GetWorkItem(string organizationName, string accessTo // Get an instance of the work item tracking client WorkItemTrackingHttpClient witClient = connection.GetClient(); - // Return the specified work item + // Make the call and return the work item return await witClient.GetWorkItemAsync(workItemId); } } diff --git a/Helpers/Helpers.csproj b/Helpers/Helpers.csproj index 2aaac964..5f47a172 100644 --- a/Helpers/Helpers.csproj +++ b/Helpers/Helpers.csproj @@ -4,8 +4,12 @@ netstandard2.0 - - + + + + + + diff --git a/Helpers/OrganizationUrlHelpers.cs b/Helpers/OrganizationUrlHelpers.cs index fe1d989f..3d73cf93 100644 --- a/Helpers/OrganizationUrlHelpers.cs +++ b/Helpers/OrganizationUrlHelpers.cs @@ -1,19 +1,13 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Net; using System.Net.Http; -using System.Net.Http.Headers; using System.Threading.Tasks; -using Microsoft.VisualStudio.Services.Common; -using Microsoft.VisualStudio.Services.Location; -using Microsoft.VisualStudio.Services.Organization; -using Microsoft.VisualStudio.Services.WebApi; +using Newtonsoft.Json.Linq; namespace Samples.Helpers { /// - /// Helper methods for building URLs to VSTS organization ("account") resources. + /// Helper methods for building URLs to VSTS organization ("account") resources using just a name or ID. /// public static class OrganizationUrlHelpers { @@ -26,17 +20,7 @@ public static async Task GetUrl(string organizationName) HttpResponseMessage response = await s_client.GetAsync(requestUrl); - if (response.StatusCode == HttpStatusCode.OK) - { - ResourceAreaInfo value = await response.Content.ReadAsAsync(); - - if (value != null) - { - return new Uri(value.LocationUrl); - } - }; - - throw new OrganizationNotFoundException(organizationName); + return await ExtractLocationUrl(response); } /// @@ -48,17 +32,22 @@ public static async Task GetUrl(Guid organizationId) HttpResponseMessage response = await s_client.GetAsync(requestUrl); + return await ExtractLocationUrl(response); + } + + private static async Task ExtractLocationUrl(HttpResponseMessage response) + { if (response.StatusCode == HttpStatusCode.OK) { - ResourceAreaInfo value = await response.Content.ReadAsAsync(); + var resourceArea = await response.Content.ReadAsAsync(); - if (value != null) + if (resourceArea != null) { - return new Uri(value.LocationUrl); + return new Uri(resourceArea["locationUrl"].ToString()); } - }; + } - throw new OrganizationNotFoundException(organizationId.ToString()); + return null; } private static readonly HttpClient s_client = new HttpClient(); From 5484dbc3f8df9b0f7aa089a7decc88cc59ea63ca Mon Sep 17 00:00:00 2001 From: Will Smythe Date: Wed, 29 Aug 2018 15:47:21 -0400 Subject: [PATCH 6/8] update org helper readme --- Helpers/README.md | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Helpers/README.md b/Helpers/README.md index 722c0551..921afc02 100644 --- a/Helpers/README.md +++ b/Helpers/README.md @@ -4,34 +4,34 @@ This project include various useful "helper" samples. ## OrganizationUrlHelpers -Provides sample helper methods for properly constructing URLs to VSTS organizations (formerly "account") from an organizastion ID or name. +Provides sample helper methods for properly constructing URLs to VSTS organizations (formerly "account") using an organization ID or name. -### Create a connection to an organization using its name +### Get the URL for a VSTS organization (by name) ```cs public async Task DoSomething() { - Uri orgUrl = OrganizationUrlHelpers - .GetConnectionUrl("myOrgName"); // https://myorgname.visualstudio.com + Uri orgUrl = Samples.Helpers.OrganizationUrlHelpers + .GetUrl("myOrgName"); // https://myorgname.visualstudio.com - VssConnection orgConnection = new VssConnection(orgUrl, credentials); + HttpClient client = new HttpClient(); - BuildHttpClient buildClient = orgConnection.GetClient(); + await client.GetAsync(orgUrl); } ``` -### Create a connection to an organization using its ID +### Get the URL for a VSTS organization (by ID) ```cs public async Task DoSomething() { - Guid orgId = Guid.Parse("279ed1c4-2f72-4e61-8c95-4bafcc13fd02"); // ID for the "MyOrgName" organzation + Guid orgId = Guid.Parse("279ed1c4-2f72-4e61-8c95-4bafcc13fd02"); // ID for the "myOrgName" organzation - Uri orgUrl = OrganizationUrlHelpers - .GetConnectionUrl(orgId); // https://myorgname.visualstudio.com + Uri orgUrl = Samples.Helpers.OrganizationUrlHelpers + .GetUrl(orgId); // https://myorgname.visualstudio.com - VssConnection orgConnection = new VssConnection(orgUrl, credentials); - - BuildHttpClient buildClient = orgConnection.GetClient(); + HttpClient client = new HttpClient(); + + await client.GetAsync(orgUrl); } ``` From ad5edf41fe298cfa14a4303e559621152b1e1161 Mon Sep 17 00:00:00 2001 From: Will Smythe Date: Wed, 29 Aug 2018 15:48:26 -0400 Subject: [PATCH 7/8] update csproj --- Helpers/Helpers.csproj | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Helpers/Helpers.csproj b/Helpers/Helpers.csproj index 5f47a172..9cc67415 100644 --- a/Helpers/Helpers.csproj +++ b/Helpers/Helpers.csproj @@ -4,10 +4,6 @@ netstandard2.0 - - - - From c94741771403bf3cfee80f48bf4cbf105b7cf0ce Mon Sep 17 00:00:00 2001 From: Will Smythe Date: Thu, 6 Sep 2018 15:36:03 -0400 Subject: [PATCH 8/8] Switch to official VssConnectionHelper --- .../netcore/ShowWorkItemConsole/Program.cs | 12 +++++------- .../ShowWorkItemConsole/ShowWorkItemConsole.csproj | 6 +----- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/ClientLibrary/Quickstarts/netcore/ShowWorkItemConsole/Program.cs b/ClientLibrary/Quickstarts/netcore/ShowWorkItemConsole/Program.cs index 9cb737ea..14d69045 100644 --- a/ClientLibrary/Quickstarts/netcore/ShowWorkItemConsole/Program.cs +++ b/ClientLibrary/Quickstarts/netcore/ShowWorkItemConsole/Program.cs @@ -5,12 +5,10 @@ using Microsoft.VisualStudio.Services.Common; using Microsoft.VisualStudio.Services.WebApi; -using Samples.Helpers; - -namespace Samples.ClientLibrary.Quickstarts.ShowWorkItemConsole +namespace ShowWorkItemConsole { /// - /// Simple .NET console program that shows information about a VSTS work item. + /// Simple console program that shows information about a VSTS work item. /// /// Usage: /// ShowWorkItemConsole [organizationName] [personalAccessToken] [workItemNumber] @@ -21,7 +19,7 @@ static void Main(string[] args) { string organizationName = args[0]; // Organization (formerly called "account") name, for example: "fabrikam" string accessToken = args[1]; // Personal access token. See https://docs.microsoft.com/vsts/integrate/get-started/authentication/pats?view=vsts - int workItemId = int.Parse(args[2]); // Work item ID, for example: 12 + int workItemId = int.Parse(args[2]); // Work item ID, for example: 12 try { @@ -50,7 +48,7 @@ static void Main(string[] args) static async Task GetWorkItem(string organizationName, string accessToken, int workItemId) { // Get the connection URL for the specified VSTS organization name - Uri organizationUrl = await OrganizationUrlHelpers.GetUrl(organizationName); + Uri organizationUrl = await VssConnectionHelper.GetOrganizationUrlAsync(organizationName); // Create a connection to the organization VssConnection connection = new VssConnection(organizationUrl, new VssBasicCredential(string.Empty, accessToken)); @@ -59,7 +57,7 @@ static async Task GetWorkItem(string organizationName, string accessTo WorkItemTrackingHttpClient witClient = connection.GetClient(); // Make the call and return the work item - return await witClient.GetWorkItemAsync(workItemId); + return await witClient.GetWorkItemAsync(workItemId); } } } \ No newline at end of file diff --git a/ClientLibrary/Quickstarts/netcore/ShowWorkItemConsole/ShowWorkItemConsole.csproj b/ClientLibrary/Quickstarts/netcore/ShowWorkItemConsole/ShowWorkItemConsole.csproj index fc1680de..fcef9e38 100644 --- a/ClientLibrary/Quickstarts/netcore/ShowWorkItemConsole/ShowWorkItemConsole.csproj +++ b/ClientLibrary/Quickstarts/netcore/ShowWorkItemConsole/ShowWorkItemConsole.csproj @@ -6,11 +6,7 @@ - - - - - +