Read and Write Excel and CSV files from Sharepoint via R

Viewed 127

I am trying to read and write csv files in SharePoint.

mysite <- get_sharepoint_site(site_url="https://****.sharepoint.com/sites/Test/test.xlsx")
mysite <- get_sharepoint_site(site_url="https://****.sharepoint.com/:f:/Test/?!sue0De2rb"")

It is always showing some error: Loading Microsoft Graph login for default tenant Error in process_response(res, match.arg(http_status_handler), simplify) : Not Found (HTTP 404). Failed to complete operation. Message: The provided path does not exist, or does not represent a site.

Loading Microsoft Graph login for default tenant Error in process_response(res, match.arg(http_status_handler), simplify) : Not Found (HTTP 404). Failed to complete operation. Message: The provided path does not exist, or does not represent a site.

Does anyone know anything that can be done?

I already have a sync in my laptop who works however want to connect directly from SharePoint to automate the process, so I don’t need to have my laptop on to read and write the files.

2 Answers

You need to login with AzureGraph before to get permissions

Something like that for instance if your want to connect with browser login:

# authenticate with AAD
# - on first login, call create_graph_login()
# - on subsequent logins, call get_graph_login()
gr <- create_graph_login()
#then
mysite <- get_sharepoint_site(site_url="https://****.sharepoint.com/sites/Test/test.xlsx")

Something like that for instance if your want to connect without browser:

tenant <-  Sys.getenv("tenant")
secret <- Sys.getenv("secret")
app <- Sys.getenv("app")

# authenticate with AAD
# - on first login, call create_graph_login()
# - on subsequent logins, call get_graph_login()
gr <- create_graph_login(tenant=tenant, app=app, password=secret)
mysite <- get_sharepoint_site(site_url="https://****.sharepoint.com/sites/Test/test.xlsx", auth_type="device_code")

How do you get this information may vary if it your company, you might want to talk to your IT team or maybe follow this guide Authenticating via Microsoft

I am not 100% sure from your question, but based on the get_sharepoint_site method it looks like you're using the Microsoft365R package.

If this is the case, then unfortunately you cannot do what you're trying to do.

I believe get_sharepoint_site can be used to get your Sharepoint site (i.e., Test), but not to get files within that site (i.e., Test/test.xlsx).

This is the meaning of the errors you're seeing — "path ... does not represent a site".

Your best bet is probably to construct your own http requests to Microsoft's Graph API. Check the documentation to see what API endpoints are available - the get range and range update endpoints are probably worth looking at.

EDIT

Since posting the above I have explored Microsoft365R a little more carefully.

It seems that the package does provide a method for interacting with files.

For example, if you wanted to get a file on your Sharepoint located at mydirectory/test.xlsx

sp   <- get_sharepoint_site("name-of-your-sharepoint-site")
dir  <- sp$get_item("mydirectory")
file <- dir$get_item("test.xlsx")
Related