Add users to D365 CRM using console application

72 views Asked by At

I am trying to create users in a trial instance of dynamics 365 using a console app. So far I have used Graph APIs to create AAD/Entra ID users and assign the following default licences:

  • Microsoft 365 E5 Developer (without Windows and Audio Conferencing)
  • Microsoft Power Apps for Developer

I can see the new users in AAD and MS 365 admin centre with the correct licences.

However, these users still doesn't show up on CRM's advanced settings>security>users grid. Neither do they show up on the power platform admin centre > environment > settings> users. I have to manually add them via power platform admin centre only then they show up.

I am using the following code to assign the licence.

internal static async Task<string> AssignLicenseAsync(GraphServiceClient graphServiceClient, string userId, Guid? skuId)
{
    AssignedLicense aLicense = new AssignedLicense { SkuId = skuId };
    IList<AssignedLicense> licensesToAdd = new AssignedLicense[] { aLicense };
    IList<Guid> licensesToRemove = new Guid[] { };

    var licenceAssignRequest = await graphServiceClient.Users[userId].AssignLicense(licensesToAdd, licensesToRemove).Request().PostAsync();
    Console.WriteLine($"User: {userId} assigned licence: {skuId}");
    return licenceAssignRequest.UserPrincipalName;
}

Can this be done via c# or do I need to use a powershell script? Any advice?

Many thanks

0

There are 0 answers