rcurldownloadrcurl

how to download files from a sharepoint folder with R


I have some files in my company's sharepint folder. I need to use r to download all files in that folder. How can I do that with R?

I googled the question and got this link. It's about uploading the file to a sharepoint folder. [Uploading files to SharePoint from R

The code from the link is like below:

saveToSharePoint <- function(fileName) 
  {
   cmd <- paste("curl --max-time 7200 --connect-timeout 7200 --ntlm --user","username:password", 
              "--upload-file /home/username/FolderNameWhereTheFileToTransferExists/",fileName, 
              "teamsites.OrganizationName.com/sites/PageTitle/Documents/UserDocumentation/FolderNameWhereTheFileNeedsToBeCopied/",fileName, sep = " ")
   system(cmd)
  }

 saveToSharePoint("SomeFileName.Ext")

It's about uploading one specific file. But what I need is download files from sharepoint folder.

so, I modified the code to copy from sharepoint folder. I changed --upload-file to --download-file.

copyFromSharePoint <- function(fileName) 
      {
       cmd <- paste("curl --max-time 7200 --connect-timeout 7200 --ntlm --user","username:password", 
                  "--download-file teamsites.OrganizationName.com/sites/PageTitle/Documents/FolderNameWhereTheFileToTransferExists/",fileName, 
                  "home/username/UserDocumentation/FolderNameWhereTheFileNeedsToBeCopied/",fileName, sep = " ")
       system(cmd)
      }

copyFromSharePoint("SomeFileName.Ext")

However, that does not seem to work. Error message below:

curl: option --download-file: is unknown

Does anyone know how I can do it with R?

In addition, what if I need to download all files in the folder, not one specific file.Does anyone know how I can do it with R?


Solution

  • Solution

    I made a simple R Package called sharepointr to upload and download files from SharePoint.

    The way I made the whole download/upload to sharepoint work from R was to:

    1. Make a SharePoint App Registration
    2. Add permissions to App Registration
    3. Getting "tenant_id" and "resource_id" using cURL
    4. Make get/post calls to API

    It is all described in the package Readme.md

    Example

    # Installing the package
    install.packages("devtools")
    devtools::install_github("esbeneickhardt/sharepointr")
    
    # Setting 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"
    
    # Getting a SharePoint Token
    sharepoint_token <- get_sharepoint_token(client_id, client_secret, tenant_id, resource_id, site_domain)
    
    # Getting sharepoint digest value
    sharepoint_digest_value <- get_sharepoint_digest_value(sharepoint_token, sharepoint_url)
    
    # Downloading a file
    sharepoint_path <- "Shared Documents/test"
    sharepoint_file_name <- "Mappe.xlsx"
    out_path <- "C:/Users/User/Desktop/"
    download_sharepoint_file(sharepoint_token, sharepoint_url, sharepoint_digest_value, sharepoint_path, sharepoint_file_name, out_path)