Microsoft 365 Outlook: Post Attachment to Calendar Event
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.
Configs
  • A: OAuth 2.0 Setting *
  • B: Event Id *#{EL}
  • C: Attachment File (1 file only, up to 2.9MB) *
  • D: Attachment File Id (update)
  • X: Access Log (update)
Script (click to open)


// Post Outlook Calendar Event Attachment via Microsoft Graph API v1.0 (ver. 202209)
// (c) 2022, 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

2022-09-20 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/outlook-calendar-event-attachment-post/
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: