Skip to content

Commit fda495c

Browse files
committed
HTTP and nodejs samples
1 parent ed3830d commit fda495c

File tree

1 file changed

+84
-2
lines changed

1 file changed

+84
-2
lines changed

articles/active-directory/external-identities/code-samples.md

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ services: active-directory
66
ms.service: active-directory
77
ms.subservice: B2B
88
ms.topic: sample
9-
ms.date: 03/09/2022
9+
ms.date: 03/14/2022
1010

1111
ms.author: mimart
1212
author: msmimart
@@ -19,6 +19,7 @@ ms.collection: M365-identity-device-management
1919
# Azure Active Directory B2B collaboration code and PowerShell samples
2020

2121
## PowerShell example
22+
2223
You can bulk-invite external users to an organization from email addresses that you have stored in a .CSV file.
2324

2425
1. Prepare the .CSV file
@@ -50,15 +51,32 @@ You can bulk-invite external users to an organization from email addresses that
5051
```
5152

5253
This cmdlet sends an invitation to the email addresses in invitations.csv. Additional features of this cmdlet include:
54+
5355
- Customized text in the email message
5456
- Including a display name for the invited user
5557
- Sending messages to CCs or suppressing email messages altogether
5658

5759
## Code sample
60+
5861
Here we illustrate how to call the invitation API, in "app-only" mode, to get the redemption URL for the resource to which you are inviting the B2B user. The goal is to send a custom invitation email. The email can be composed with an HTTP client, so you can customize how it looks and send it through the Microsoft Graph API.
5962

60-
```csharp
6163

64+
# [HTTP](#tab/http)
65+
66+
```http
67+
POST https://graph.microsoft.com/v1.0/invitations
68+
Content-type: application/json
69+
{
70+
"invitedUserEmailAddress": "[email protected]",
71+
"invitedUserDisplayName": "David",
72+
"inviteRedirectUrl": "https://myapp.contoso.com",
73+
"sendInvitationMessage": true
74+
}
75+
```
76+
77+
# [C#](#tab/csharp)
78+
79+
```csharp
6280
using System;
6381
using System.Threading.Tasks;
6482
using Microsoft.Graph;
@@ -133,6 +151,70 @@ namespace SampleInviteApp
133151
}
134152
```
135153

154+
# [JavaScript](#tab/javascript)
155+
156+
Install the following npm packages:
157+
158+
```bash
159+
npm install express
160+
npm install isomorphic-fetch
161+
npm install @azure/identity
162+
npm install @microsoft/microsoft-graph-client
163+
```
164+
165+
```javascript
166+
const express = require('express')
167+
const app = express()
168+
169+
const { Client } = require("@microsoft/microsoft-graph-client");
170+
const { TokenCredentialAuthenticationProvider } = require("@microsoft/microsoft-graph-client/authProviders/azureTokenCredentials");
171+
const { ClientSecretCredential } = require("@azure/identity");
172+
require("isomorphic-fetch");
173+
174+
// This is the application id of the application that is registered in the above tenant.
175+
const CLIENT_ID = ""
176+
177+
// Client secret of the application.
178+
const CLIENT_SECRET = ""
179+
180+
// This is the tenant ID of the tenant you want to invite users to. For example fabrikam.onmicrosoft.com
181+
const TENANT_ID = ""
182+
183+
async function sendInvite() {
184+
185+
// Initialize a confidential client application. For more info, visit: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/identity/identity/samples/AzureIdentityExamples.md#authenticating-a-service-principal-with-a-client-secret
186+
const credential = new ClientSecretCredential(TENANT_ID, CLIENT_ID, CLIENT_SECRET);
187+
188+
// Initialize the Microsoft Graph authentication provider. For more info, visit: https://docs.microsoft.com/en-us/graph/sdks/choose-authentication-providers?tabs=Javascript#using--for-server-side-applications
189+
const authProvider = new TokenCredentialAuthenticationProvider(credential, { scopes: ['https://graph.microsoft.com/.default'] });
190+
191+
// Create MS Graph client instance. For more info, visit: https://github.com/microsoftgraph/msgraph-sdk-javascript/blob/dev/docs/CreatingClientInstance.md
192+
const client = Client.initWithMiddleware({
193+
debugLogging: true,
194+
authProvider,
195+
});
196+
197+
// Create invitation object
198+
const invitation = {
199+
invitedUserEmailAddress: '[email protected]',
200+
invitedUserDisplayName: 'David',
201+
inviteRedirectUrl: 'https://www.microsoft.com',
202+
sendInvitationMessage: true
203+
};
204+
205+
// Execute the MS Graph command. For more information, visit: https://docs.microsoft.com/en-us/graph/api/invitation-post
206+
graphResponse = await client.api('/invitations')
207+
.post(invitation);
208+
209+
// Return the invite redeem URL
210+
return graphResponse.inviteRedeemUrl
211+
}
212+
213+
const inviteRedeemUrl = await sendInvite();
214+
215+
```
216+
217+
---
136218

137219
## Next steps
138220

0 commit comments

Comments
 (0)