Outlook Calendar Event Attachment Post

Outlook 予定表のイベントにファイルを添付

Posts an attachment to an Outlook Calendar Event (1 file only, up to 2.9MB). You can specify the target by the event ID. The ID of the attachment can be saved.

Auto Step icon
Configs for this Auto Step
conf_OAuth2
A: OAuth 2.0 Setting *
conf_eventId
B: Event Id *#{EL}
conf_attachment
C: Attachment File (1 file only, up to 2.9MB) *
conf_attachmentId
D: Attachment File Id (update)
conf_log
X: Access Log (update)
Script (click to open)


// Post Outlook Calendar Event Attachment via Microsoft Graph API v1.0 (ver. 202209)
// (c) 2023, Questetra, Inc. (the MIT License)

//// == 工程コンフィグの参照 / Config Retrieving ==
const oauth2  = configs.get( "conf_OAuth2" ) + "";

const eventId = configs.get( "conf_eventId" ) + "";
const files   = engine.findDataByNumber( configs.get( "conf_attachment" ) );

const dataId_attachmentId = configs.get( "conf_attachmentId" );
const dataId_log          = configs.get( "conf_log" );

//// == 演算 / Calculating ==
if( eventId == "" ){
  throw `Event ID none.`;
}

if( !files ){
  throw `File none.`;
}
const file = files.get(0);

//ファイルサイズ確認
// API仕様的には3MBまで、ファイルだけでなくペイロード全体での制限、余裕をみて2.9MBまでとする
// https://learn.microsoft.com/ja-jp/graph/api/event-post-attachments?view=graph-rest-1.0&tabs=javascript
if (file.getLength() > 3040871) {
  throw `File size over.`;
}

let fileData = "";
fileRepository.readFile(file, 3145728, function(bytes) {
  fileData = base64.encodeToString(bytes);
});

let requestObj = {};
  requestObj['@odata.type'] = '#microsoft.graph.fileAttachment';
  requestObj.name = file.getName();
  requestObj.contentBytes = fileData;

//engine.log("json:" + JSON.stringify( requestObj ));

let accessLog = "";
let uri = "https://graph.microsoft.com/v1.0/me/events/" + eventId + "/attachments";
let response = httpClient.begin()
  .authSetting( oauth2 )
  .body( JSON.stringify( requestObj ), "application/json" )
  .post( uri );
accessLog += "---POST request--- " + response.getStatusCode() + "\n";
accessLog += response.getResponseAsString() + "\n";


//// == ワークフローデータへの代入 / Data Updating ==
const status = response.getStatusCode();

if( status >= 300 ){
  engine.log(accessLog);
  throw `Failed in POST request. status: ${status}`;
} else {
  const jsonObj = JSON.parse( response.getResponseAsString() );
  const attachmentId = jsonObj.id + "";

  if( dataId_attachmentId !== "" ){
    engine.setDataByNumber( dataId_attachmentId, attachmentId );
  }
}

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

Download

warning Freely modifiable JavaScript (ECMAScript) code. No warranty of any kind.
(Installing Addon Auto-Steps are available only on the Professional edition.)

Notes

Capture

See Also

%d