DerKiLLa
DerKiLLa

Reputation: 145

Assign a License (Microsoft Graph API)

I'm trying to assign a license in C# via Graph API.

https://learn.microsoft.com/en-us/graph/api/user-assignlicense?view=graph-rest-1.0

Parameters:

addLicenses (AssignedLicense collection)

A collection of assignedLicense objects that specify the licenses to add. You can disable plans associated with a license by setting the disabledPlans property on an assignedLicense object.

removeLicenses (Guid collection)

A collection of GUIDs that identify the licenses to remove.

Here is my code ...

var userQuery = await client.Users
  .Request()
  .Filter("userPrincipalName eq '[email protected]'")
  .GetAsync();
var user = userQuery.FirstOrDefault();

var skus = await client.SubscribedSkus.Request().GetAsync();

do
{
  foreach (var sku in skus)
  {

    AssignedLicense aLicense = new AssignedLicense { SkuId = sku.SkuId };

    IList<AssignedLicense> licensesToAdd = new AssignedLicense[] { aLicense };
    IList<Guid> licensesToRemove = new Guid[] { };

    try
    {
      client.Users[user.Id].AssignLicense(licensesToAdd, licensesToRemove);
    }
  }
}
while (skus.NextPageRequest != null && (skus = await skus.NextPageRequest.GetAsync()).Count > 0);

I don't get an error, but it does not work. The user has no license ...

Upvotes: 3

Views: 2811

Answers (1)

FerronSW
FerronSW

Reputation: 535

I think you forgot something here:

client.Users[user.Id].AssignLicense(licensesToAdd, licensesToRemove);

I think it should be:

await client.Users[user.Id].AssignLicense(licensesToAdd, licensesToRemove).Request().PostAsync();

PS:

You can get the user with less code like:

var userQuery = await client.Users["[email protected]"].Request().GetAsync();

Upvotes: 3

Related Questions