Saving a File to Sharepoint with R

Saving a file to Sharepoint with R

Someone from IT department helped us to solve the issue. As I said we were able to import data from share point using the link to the folder. However the same link did not work when trying to save on SharePoint. To be able to do this the IT team gave us a slightly changed link. See below the type of link we used to upload and the one that was recommended and that works.

https://siteadress/foldername/subfolder/docname.rda

the one that works is

\\\\siteadress@SSL\\foldername\\subfolder\\docname.rda

Saving files to SharePoint folder from R

I tried this and it worked:

system("curl --ntlm --user username:password --upload-file someFolder/FileToCopy.ext https://sites.somecompany.com/sites/sitename/Documents/UserDocumentFolder/FileToCopy.ext")

Uploading file to sharepoint through R

I have never heard of using R to load files into SharePoint. If you are already using Excel, just run a small VBA script to load the files into SharePoint.

Dim SharepointAddress As String
Dim LocalAddress As String
Dim objNet As Object
Dim FS As Object

' Where you will enter Sharepoint location path
SharepointAddress = "\\sharepoint path to document library" & "\"
' Where you will enter the file path, ex: Excel file
LocalAddress = "your file path"
Set objNet = CreateObject("WScript.Network")
Set FS = CreateObject("Scripting.FileSystemObject")
If FS.FileExists(LocalAddress) Then
FS.CopyFile LocalAddress, SharepointAddress
End If
Set objNet = Nothing
Set FS = Nothing

Accessing Excel file from Sharepoint with R

I use

library(readxl)
read_excel('//companySharepointSite/project/.../ExcelFilename.xlsx', 'Sheet1', skip=1)

Note, no https:, and sometimes I have to open the file first (i.e., cut and paste //companySharepointSite/project/.../ExcelFilename.xlsx into my browser's address bar)

how to download files from a sharepoint folder with R

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)


Related Topics



Leave a reply



Submit