Search code examples
rsharepoint

Access sharepoint folders in R


I'm currently trying to access sharepoint folders in R. I read multiple articles addressing that issue but all the proposed solutions don't seem to work in my case.

I first tried to upload a single .txt file using the httr package, as follows:

URL <- "<domain>/<file>/<subfile>/document.txt"
r <- httr::GET(URL, httr::authenticate("username","password",type="any"))

I get the following error:

Error in curl::curl_fetch_memory(url, handle = handle) : 
  URL using bad/illegal format or missing URL

I then tried another package that use a similar syntax (RCurl):

URL <- "<domain>/<file>/<subfile>/document.txt"
r <- getURL(URL, userpwd = "username:password")

I get the following error:

Error in function (type, msg, asError = TRUE)  : 

I tried many other ways of linking R to sharepoint, but these two seemed the most straightforward. (also, my URL doesn't seem to be the problem since it works when I run it in my web browser).

Ultimately, I want to be able to upload a whole sharepoint folder to R (not only a single document). Something that would really help is to set my sharepoint folder as my working directory and use the base::list.files() function to list files in my folder, but I doubt thats possible.

Does anyone have a clue how I can do that?


Solution

  • I created an R library called sharepointr for doing just that.

    What I basically did was:

      1. Create App Registration
      1. Add permissions
      1. Get credentials
      1. Make REST calls

    The Readme.md for the repository has a full description, and here is an example:

    # Install 
    install.packages("devtools")
    devtools::install_github("esbeneickhardt/sharepointr")
    
    # Parameters
    client_id <- "insert_from_first_step"
    client_secret <- "insert_from_first_step"
    tenant_id <- "insert_from_fourth_step"
    resource_id <- "insert_from_fourth_step"
    site_domain <- "yourorganisation.sharepoint.com"
    sharepoint_url <- "https://yourorganisation.sharepoint.com/sites/MyTestSite"
    
    # Get Token
    sharepoint_token <- get_sharepoint_token(client_id, client_secret, tenant_id, resource_id, site_domain)
    
    # Get digest value
    sharepoint_digest_value <- get_sharepoint_digest_value(sharepoint_token, sharepoint_url)
    
    # List folders
    sharepoint_path <- "Shared Documents/test"
    get_sharepoint_folder_names(sharepoint_token, sharepoint_url, sharepoint_digest_value, sharepoint_path)