Home > database >  Create Azure AD Groups from Azure Functions
Create Azure AD Groups from Azure Functions

Time:09-29

I need to create a NodeJS Azure Function that should create Azure AD Groups based on some logics. My question is which SDK to use for this scenario? I have been googling for the past few days and got lost in the Microsoft Documentation jungle.

My function will be called from a browser client with a parameter in the query which will be the Group name.

Thanks a lot for any advice!

CodePudding user response:

We can use ms graph api to create azure ad group, and for nodejs, Microsoft also provide graph SDK for calling graph api. Here's the code snippet:

const options = {
    authProvider,
};

const client = Client.init(options);

const group = {
  description: 'Self help community for library',
  displayName: 'Library Assist',
  groupTypes: [
    'Unified'
  ],
  mailEnabled: true,
  mailNickname: 'library',
  securityEnabled: false
};

await client.api('/groups')
    .post(group);

Here we also need to create an author provider so that it can give the authorization to graph client to create the group. Since this is an Azure function, we should use the client credential provider. Here's the code snippet:

const {
    Client
} = require("@microsoft/microsoft-graph-client");
const {
    TokenCredentialAuthenticationProvider
} = require("@microsoft/microsoft-graph-client/authProviders/azureTokenCredentials");
const {
    ClientSecretCredential
} = require("@azure/identity");

const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
const authProvider = new TokenCredentialAuthenticationProvider(credential, {
    scopes: [scopes]
});

const client = Client.initWithMiddleware({
    debugLogging: true,
    authProvider
    // Use the authProvider object to create the class.
});
  • Related