$invitations = import-csv C:\data\invitations.csv
$messageInfo = New-Object Microsoft.Open.MSGraph.Model.InvitedUserMessageInfo
$messageInfo.customizedMessageBody = "Hey there! Check this out. I created an invitation through PowerShell"foreach ($emailin$invitations) {
New-MgInvitation -InviteRedirectUrl"https://wingtiptoysonline-dev-ed.my.woodgrove.com" `
-InvitedUserDisplayName$email.Name -InvitedUserEmailAddress$email.InvitedUserEmailAddress `
-InvitedUserMessageInfo$messageInfo -SendInvitationMessage:$true
}
This cmdlet sends an invitation to the email addresses in invitations.csv. More features of this cmdlet include:
Customized text in the email message
Including a display name for the invited user
Sending messages to CCs or suppressing email messages altogether
Code sample
The code sample illustrates how to call the invitation API and get the redemption URL. Use the redemption URL to send a custom invitation email. You can compose the email with an HTTP client, so you can customize how it looks and send it through the Microsoft Graph API.
using System;
using System.Threading.Tasks;
using Microsoft.Graph;
using Azure.Identity;
namespaceSampleInviteApp
{
classProgram
{
///<summary>/// This is the tenant ID of the tenant you want to invite users to.///</summary>privatestaticreadonlystring TenantID = "";
///<summary>/// This is the application id of the application that is registered in the above tenant.///</summary>privatestaticreadonlystring TestAppClientId = "";
///<summary>/// Client secret of the application.///</summary>privatestaticreadonlystring TestAppClientSecret = @"";
///<summary>/// This is the email address of the user you want to invite.///</summary>privatestaticreadonlystring InvitedUserEmailAddress = @"";
///<summary>/// This is the display name of the user you want to invite.///</summary>privatestaticreadonlystring InvitedUserDisplayName = @"";
///<summary>/// Main method.///</summary>///<param name="args">Optional arguments</param>staticasync Task Main(string[] args)
{
string InviteRedeemUrl = await SendInvitation();
}
///<summary>/// Send the guest user invite request.///</summary>privatestaticasyncstringSendInvitation()
{
/// Get the access token for our application to talk to Microsoft Graph.var scopes = new[] { "https://graph.microsoft.com/.default" };
var clientSecretCredential = new ClientSecretCredential(TenantID, TestAppClientId, TestAppClientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
// Create the invitation object.var invitation = new Invitation
{
InvitedUserEmailAddress = InvitedUserEmailAddress,
InvitedUserDisplayName = InvitedUserDisplayName,
InviteRedirectUrl = "https://www.microsoft.com",
SendInvitationMessage = true
};
// Send the invitation var GraphResponse = await graphClient.Invitations
.Request()
.AddAsync(invitation);
// Return the invite redeem URLreturn GraphResponse.InviteRedeemUrl;
}
}
}
const express = require('express')
const app = express()
const { Client } = require("@microsoft/microsoft-graph-client");
const { TokenCredentialAuthenticationProvider } = require("@microsoft/microsoft-graph-client/authProviders/azureTokenCredentials");
const { ClientSecretCredential } = require("@azure/identity");
require("isomorphic-fetch");
// This is the application id of the application that is registered in the above tenant.const CLIENT_ID = ""// Client secret of the application.const CLIENT_SECRET = ""// This is the tenant ID of the tenant you want to invite users to. For example fabrikam.onmicrosoft.comconst TENANT_ID = ""asyncfunctionsendInvite() {
// 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-secretconst credential = new ClientSecretCredential(TENANT_ID, CLIENT_ID, CLIENT_SECRET);
// Initialize the Microsoft Graph authentication provider. For more info, visit: https://learn.microsoft.com/graph/sdks/choose-authentication-providers?tabs=Javascript#using--for-server-side-applicationsconst authProvider = new TokenCredentialAuthenticationProvider(credential, { scopes: ['https://graph.microsoft.com/.default'] });
// Create MS Graph client instance. For more info, visit: https://github.com/microsoftgraph/msgraph-sdk-javascript/blob/dev/docs/CreatingClientInstance.mdconst client = Client.initWithMiddleware({
debugLogging: true,
authProvider,
});
// Create invitation objectconst invitation = {
invitedUserEmailAddress: 'david@fabrikam.com',
invitedUserDisplayName: 'David',
inviteRedirectUrl: 'https://www.microsoft.com',
sendInvitationMessage: true
};
// Execute the MS Graph command. For more information, visit: https://learn.microsoft.com/graph/api/invitation-post
graphResponse = await client.api('/invitations')
.post(invitation);
// Return the invite redeem URLreturn graphResponse.inviteRedeemUrl
}
const inviteRedeemUrl = await sendInvite();
This learning path includes hands-on exercises that will show you how to perform common tasks, such as showing a user's emails, accessing calendar events, and downloading and uploading files, in a JavaScript app using Microsoft Graph APIs.