Microsoft 365: Create, delete, and rename mail folders with the Microsoft Graph API

To access the data in Microsoft 365, like emails, Microsoft is deprecating Basic authentication for IMAP and POP3 and is increasingly promoting the Graph API.

With this last set of functions that includes all the features to manage folders from the 4D v20, you have in 4D NetKit a set of functions to manage your mailboxes by programming with the Microsoft Graph API.

HDI Office365 Manage folder

In this blog post, we will see how to create a new mail folder and rename and delete it.

Account connection

Before starting, we will create the OAuth2 connection object and the Office 365 object.

var $oAuth2: cs.NetKit.OAuth2Provider
var $office365 : cs.NetKit.Office365

$oAuth2:=cs.NetKit.OAuth2Provider.new($param)
$office365:=cs.NetKit.Office365.new($oAuth2;\
 New object("mailType"; "Microsoft"))

Create a mail folder.

You can create a new “Backup” folder to store your emails with the .createFolder() function:

// Creates a new folder on the root
$status:=$office365.mail.createFolder("Backup")
If ($status.success)
  $folderId:=$status.id
  // Moves your email in the new folder
  $status:=$office365.mail.move($mailId; $folderId)
End if

Rename a mail folder

You can change the name of a folder thanks to the .renameFolder() funtion. For example, we can rename “Backup” to “Backup 2022”:

// Renames a folder
$status:=$office365.mail.renameFolder($folderId; "Backup 2022")
$folderId:=$status.id

Delete a mail folder

If you don’t need the mail in your folder, you can delete it with the .deleteFolder() function:

$status:=$office365.mail.deleteFolder($folderId)

With this last set of commands, you can manage your mailboxes through the Graph API, like with SMTP, IMAP, or POP3. If you haven’t already done so, you can check out the other blog posts on mailboxes management here:

 

Check out this feature with the HDI and the documentation for more details!

Fabrice Mainguené
• Product Owner •Fabrice Mainguené joined 4D Program team in November, 2016. As a Product Owner, he is in charge of writing the user stories then translating it to functional specifications. His role is also to make sure that the feature implementation delivered is meeting the customer need.After obtaining a Bachelor degree in Computer Science at CNAM, Fabrice joined a small software publishing company as a Windev developer. Then he worked for different companies in industry and trade areas as a Windev and web developer as well as technical advisor on new features.