0

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!

  • 1
    follow [this section](https://learn.microsoft.com/en-us/graph/sdks/choose-authentication-providers?tabs=Javascript#client-credentials-provider) to generate the `client`, then follow [this section](https://learn.microsoft.com/en-us/graph/api/group-post-groups?view=graph-rest-1.0&tabs=javascript#request) to create group. – Tiny Wang Sep 28 '22 at 03:05
  • 1
    Thank you Tiny Wang! Your comment helped me structure my function. Can you repost it as an answer so that I can accept it. – magnetarneo Sep 28 '22 at 11:47

1 Answers1

1

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.
});
Tiny Wang
  • 10,423
  • 1
  • 11
  • 29