Back to Azure Sdk For

Example: Getting a subscription

sdk/resourcemanager/Azure.ResourceManager/docs/Sample1_HelloWorld.md

2019-05-16T16-521.9 KB
Original Source

Example: Getting a subscription

Note: Before getting started with the samples, make sure to go trough the prerequisites.

Namespaces for this example:

C#
using System;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Resources;

The following code shows how to get the default subscription:

C#
ArmClient client = new ArmClient(new DefaultAzureCredential());
SubscriptionResource subscription = client.GetDefaultSubscription();
Console.WriteLine(subscription.Id);

It's possible to get a specific subscription as follows:

C#
string subscriptionId = "your-subscription-id";
ArmClient client = new ArmClient(new DefaultAzureCredential());
SubscriptionCollection subscriptions = client.GetSubscriptions();
SubscriptionResource subscription = subscriptions.Get(subscriptionId);
Console.WriteLine($"Got subscription: {subscription.Data.DisplayName}");

You can also specify the default subscription when creating the ArmClient:

C#
string defaultSubscriptionId = "your-subscription-id";
ArmClient client = new ArmClient(new DefaultAzureCredential(), defaultSubscriptionId);
SubscriptionResource subscription = client.GetDefaultSubscription();
Console.WriteLine(subscription.Id);

From here, it is possible to get the resource groups from the retrieved subscription:

C#
ResourceGroupCollection resourceGroups = subscription.GetResourceGroups();

Next steps

Take a look at the Managing Resource Groups samples.