Microsoft365: Get User ID
Obtains a Microsoft365 user ID by specifying an email address.
Configs
  • A: OAuth2 Setting *
  • B: User Email *#{EL}
  • C: User ID (Update) *
  • X: Access Log (update)
Script (click to open)


// Get Microsoft365 User ID via Microsoft Graph API v1.0 (ver. 202210)
// (c) 2022, Questetra, Inc. (the MIT License)

//// == Config Retrieving ==
const oauth2  = configs.get( "conf_OAuth2" ) + "";

const email   = configs.get( "conf_email" ) + "";

const dataId_userId = configs.get( "conf_userId" );
const dataId_log    = configs.get( "conf_log" );

//engine.log("email:"+email);

//// == Calculating ==
let accessLog = "";
let uri = "https://graph.microsoft.com/v1.0/users?$filter=startswith(mail,'" + email + "')";
let response = httpClient.begin()
  .authSetting( oauth2 )
  .get( uri );
accessLog += "---GET request--- " + response.getStatusCode() + "\n";
accessLog += response.getResponseAsString() + "\n";


//// == Data Updating ==
const status = response.getStatusCode();

let userId = "";
if( status >= 300 ){
  engine.log(accessLog);
  throw `Failed in GET request. status: ${status}`;
} else {
  const jsonObj = JSON.parse( response.getResponseAsString() );
  const values = jsonObj.value;
  for (let i = 0; i < values.length; i++) {
    let value = values[i];
    if (value.mail == email) {
      userId = value.id;
      engine.setDataByNumber( dataId_userId, userId );
    }
  }
}
if (userId == "") {
  throw `specified user None`;
}

if( dataId_log !== "" ){
  engine.setDataByNumber( dataId_log, accessLog );
}

Download

2022-10-15 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/microsoft365-user-id-get/
The Addon-import feature is available with Professional Edition
Freely modifiable JavaScript (ECMAScript) code. No warranty of any kind.

Notes

Capture

See also

%d bloggers like this: