Uploading files to SharePoint from R - r

I am trying to upload a file from R to SharePoint. I have found similar questions like Saving files to SharePoint folder from R, Saving a file to Sharepoint with R and Copying file to sharepoint library in R but I haven't been able to get it working for myself.
The things I tried so far:
system("curl --ntlm --user username:password --upload-file file.xslx https://companyname.sharepoint.com/sites/sitename/Shared%20documents/file.xlsx")
system("curl --ntlm --user username:password --upload-file file.xslx https://companyname.sharepoint.com/sites/sitename/Documents/file.xlsx")
system("curl --ntlm --user username:password --upload-file file.xslx https://companyname.sharepoint.com/sites/sitename/Shared documents/file.xlsx")
system("curl --ntlm --user username:password --upload-file file.xslx \\\\companyname.sharepoint.com#SSL\\sites\\sitename\\Shared%20documents\\file.xlsx")
Note that our SharePoint is in our native language (Dutch), so the folder "Shared documents" is "Gedeelde documenten". I tried both languages but without success. Not sure if I am supposed to the English names or the Dutch ones.
My guess is that the url I use is not in the correct format or such, so I have played around with that but can't come up with the correct way myself.
Any help is much appreciated.
EDIT:
This is how the page and folder in Sharepoint looks like.
Full url (guessed the part from /Forms till the end is not needed in the command): https://companyname.sharepoint.com/sites/SiteName/Gedeelde%20documenten/Forms/AllItems.aspx?id=%2Fsites%2FOfficeSFMT%2FGedeelde%20documenten%2FGeneral%2FTest
Screenshot of the folder:
My best guess was trying: "--upload-file C:/Users/UserName/Documents/Test.txt", "companyname.sharepoint.com/sites/SiteName/Documenten/General/Test/Test.txt"

I just tested the following code and it worked:
cmd <- paste("curl --max-time 7200 --connect-timeout 7200 --ntlm --user", "username:password", "--upload-file Book1.xlsx","teamsites.companyname.com/sites/SandBox/Documents/UserDocumentation/Test/Book1.xlsx", sep = " ")
system(cmd)
I routinely use the following function.However, the only issue will be the file that was transferred will remain 'checked-out' until the File is manually 'checked-in' for others to use.
saveToSharePoint <- function(fileName)
{
cmd <- paste("curl --max-time 7200 --connect-timeout 7200 --ntlm --user","username:password",
"--upload-file", paste0("/home/username/FolderNameWhereTheFileToTransferExists/",fileName),
paste0("teamsites.OrganizationName.com/sites/PageTitle/Documents/UserDocumentation/FolderNameWhereTheFileNeedsToBeCopied/",fileName), sep = " ")
system(cmd)
}
saveToSharePoint("SomeFileName.Ext")

Related

Download & decompress JSON file in R using curl or RCurl

I have the following bash script to download & decompress a JSON file:
#!/bin/sh -ex
# Ensure data directory (or a link) exists.
test -e results || mkdir results
# Download and decompress data.
curl -u $GISAID_USERNAME:$GISAID_PASSWORD --retry 4 \
https://www.epicov.org/epi3/3p/$GISAID_FEED/export/provision.json.xz \
| xz -d -T8 > results/gisaid.json
Ideally I would like to have an R function to download & decompress this file in a given directory, with the environment variables above $GISAID_USERNAME, $GISAID_PASSWORD & $GISAID_FEED passed as arguments. Would anyone know how to accomplish this, e.g. using package curl or RCurl? (It would also be OK not to decompress it and leave it as .json.xz, as I would be reading the file later using
library(jsonlite)
GISAID_json <- jsonlite::stream_in(gzfile(".//data//GISAID_json//provision.json.xz"))
Something like this should work:
library(curl)
library(glue)
custom_curl <- function(user, pass, feed, dest) {
custom_handle <- curl::new_handle()
curl::handle_setopt(
custom_handle,
username = user,
password = pass
)
url <- glue::glue(
"https://www.epicov.org/epi3/3p/{feed}/export/provision.json.xz"
)
curl::curl_download(url, dest, handle = custom_handle)
}
custom_curl('my_user', 'xxxxxx', 'feed1', 'dest/filename.json.xz')
As I can't test in the real files and url, I'm not sure if little tinkering in the function is needed, but at least is a starter point for you.
Does executing your terminal commands in R using the system function already help you?
Put your terminal call into system() and it should execute and create your file. Afterwards read in the file. Of course you would have to replace the $GISAID_USERNAME, $GISAID_PASSWORD with your actual information. If the login information or the url should be flexible, you can put together a string beforehand, since system() expects a string with the command to execute.
system("curl -u $GISAID_USERNAME:$GISAID_PASSWORD --retry 4 \
https://www.epicov.org/epi3/3p/$GISAID_FEED/export/provision.json.xz \
| xz -d -T8 > results/gisaid.json")
Afterwards just read in the (hopefully) created file.
Couldn't test with your setup, but for me e.g. this small example successfully creates a file:
system("curl https://raw.githubusercontent.com/SteffenMoritz/imputeTS/master/pkgdown/favicon/favicon.ico > /Users/Steve/Downloads/x.ico")

How can I set the version of a raw file in a (Sonatype) Nexus raw repository?

I'm making an automatic script to upload a raw file in Nexus, and I need to set up the version of this file. Is this possible? I've been checking the API but it doesn't seem posible.
The command I'm currently using to upload is:
curl --proxy $my-proxy -v --user 'user:pass' --upload-file ./myRawFile 'http://12.34.56.78:1234/repository/MyRawRepo/LoL/TheUploadedFile'
This command is being used from an automatic script (and working) to upload the file, but I don't know how to set the version.
curl -k -u "xxx:xxx" -H 'Content-Type: multipart/form-data' --data-binary "#output.zip" -X PUT https://nexus.xxx.com/repository/{raw-reponame}/xxx/{version}/output.zip
Version number can be change {version}

How to upload part of a file with curl?

I know how to upload a whole file with curl, like
curl "http://192.168.1.133/***" -F "file=#./test123"
to upload test123,
what if I upload the first 10 bytes of test123, how to do it?
curl doesn't provide this feature by itself. You can however achieve the same end goal with using some other tools in combination.
How about cutting out the beginning of the file with head and make curl read that part from stdin?
$ head -c10 test123 | curl "http://192.168.1.133/***" -F "file=#-"

Need a way to copy a file to Livelink from a cmd prompt (a la davcopy)

Has anyone written something like davcopy for Livelink? (davcopy works with SharePoint)
I have downloaded davcopy and it hangs when trying to use it with Livelink.
I've asked Open Text and their response is "There is not way to do this out of the box, it will requires writing a webservices application."
I'm not sure how to write a webservice application for livelink; so, before I explore that I was wondering if anyone had done an implementation of davcopy for Livelink.
I know about a command line application which is using MS powershell to do what you want (http://www.gatevillage.net/public/content-server-desktop-library-powershell-suite)
It wouldn't be too difficult to write something like this with Ruby or Perl. Both support WS/SOAP.
With which version of Livelink (or Content Server) do you work?
You can use the curl command line tool to upload, download or delete files in Livelink. It makes HTTP requests against CS REST API, which is available in CS 10.0 or newer.
For example, uploading a file "file.ext" to folder 8372 at http://server/instance/cs as Admin:
curl \
-F "type=144" \
-F "parent_id=8372" \
-F "name=file.ext" \
-F "file=#/path/to/file.ext" \
-u "Admin:password" \
-H "Expect:" \
http://server/instance/cs/api/v1/nodes
The "Expect" header has to be forced empty, because CS REST API does not support persistent connections, but curl would always enable them for this request.

How to translate the following simple curl call to R code using RCurl?

Consider the following commands (2 examples)
curl -H "Accept:text/plain" http://rxnav.nlm.nih.gov/REST/rxcui/866350/allrelated
curl -i -L -H "Accept: application/xml" http://pub.orcid.org/search/orcid-bio?q=digital-object-ids:"10.1088/0031-9155/58/3/535"
The user is able to run those on win7 command line with curl.exe installed. The RCurl package has no vignette and it has a lot of commands to choose from. How do I get the text file and XML file output using RCurl?
I think you can use the following :
mytxt <- getURL("http://rxnav.nlm.nih.gov/REST/rxcui/866350/allrelated", httpheader=c(Accept="text/plain"))
And :
myxml <- getURL('http://pub.orcid.org/search/orcid-bio?q=digital-object-ids:"10.1088/0031-9155/58/3/535"', httpheader=c(Accept="application/xml"))
You will find many examples in the getURL help page.

Resources