Using R: Microsoft 365 (send email, send message in Teams, manage sharepoint)

Angela W.
3 min readJul 27, 2022

There is an easy way to connect R with Outlook, Teams, OneDrive, and SharePoint. The Microsoft365R R package, developed by the Microsoft Azure team, includes functions to work with Microsoft 365 APIs from R.

Install the R library from https://github.com/Azure/Microsoft365R

devtools::install_github("Azure/Microsoft365R")

Load the library. Following instruction to authenticate 1 time if any pop up appeared.

library(Microsoft365R)

Send Outlook email with R

icrosoft365R supports sending, receiving and managing emails in Outlook. Use the get_personal_outlook() method to access your emails for your personal account, and get_business_outlook() for your work or school account. Emails can optionally be composed using either the blastula or emayili packages.

outl <- get_personal_outlook()
outlb <- get_business_outlook()

# compose an email with blastula
library(blastula)
bl_body <- "## Hello!

This is an email message that was generated by the blastula package.

We can use **Markdown** formatting with the `md()` function.

Cheers,

The blastula team"

bl_em <- compose_email(
body=md(bl_body),
footer=md("sent via Microsoft365R")
)
em <- outl$create_email(bl_em, subject="Hello from R", to="bob@example.com")

# add an attachment and send it
em$add_attachment("mydocument.docx")
em$send()

# list the most recent emails in your inbox
emlst <- outl$list_emails()

# reply to the most recent email
emlst[[1]]$
create_reply("Replying from R")$
send()

Else, a simple one, especially if you used it in the error handling programme, click here for my previous article on how to handle the error in R:

tryCatch({R expression},           # When error happens, send email.           error = function(e){             message("An error occurred:\n", e)             my_outlook <- get_business_outlook()
my_email <- my_outlook$create_email(e),
subject = "R Job: Failed",
to = "abc@abc.com")
my_email$send()}

Send Teams messages with R

To access a team in Microsoft Teams, use the get_team() function and provide the team name or ID. You can also list the teams you're in with list_teams(). These return objects of R6 class ms_team which has methods for working with channels; in turn, a channel has methods for working with messages and transferring files.

list_teams()
team <- get_team("My team")
my_team <- get_team("Team name or ID", app = "04wew95-8ddb-4era-bbee-02f9asdas"))# associated SharePoint site and drive
team$get_drive()
team$get_sharepoint_site()

# channels
team$list_channels()

chan <- team$get_channel("General")

# messages
chan$list_messages()
chan$send_message("Hello from R", attachments="hello.md")

msg <- chan$get_message("msg-id")
msg$send_reply("Reply from R")

# files: similar methods to OneDrive
chan$list_files()
chan$download_file("myfile.docx")

Manage OneDrive with R

To access your personal OneDrive call get_personal_onedrive(), and to access OneDrive for Business, call get_business_onedrive(). These return an R6 client object of class ms_drive, which has methods for working with files and folders.

od <- get_personal_onedrive()
odb <- get_business_onedrive()

# list files and folders
od$list_items()
od$list_items("Documents")

# upload and download files
od$upload_file("somedata.xlsx")
od$download_file("Documents/myfile.docx")

# create a folder
od$create_folder("Documents/newfolder")

# open a document for editing in Word Online
od$open_item("Documents/myfile.docx")

Manage sharePoint Online with R

To access a SharePoint site, use the get_sharepoint_site() function and provide the site name, URL or ID. You can also list the sites you're following with list_sharepoint_sites().

list_sharepoint_sites()
site <- get_sharepoint_site("My site")

# document libraries
site$list_drives()

# default document library
drv <- site$get_drive()

# a drive has the same methods as for OneDrive above
drv$list_items()
drv$open_item("teamproject/plan.xlsx")

# lists
site$get_lists()

lst <- site$get_list("my-list")

# return the items in the list as a data frame
lst$list_items()

--

--