8

Please note that this question was originally asked on StackOverflow, as I didn't know StackExchange had a Sharepoint Q&A board until thoroughly researching an answer to this question. Feel free to vote to close the Stackoverflow duplicate.

Also, I'm using SharePoint through Office 365 (logging into the email and navigating from there to the SharePoint dashboard). I believe Tiago commented that this would be SharePoint v16.

  • I have the URL for a SharePoint location: https://company.sharepoint.com/ (A).
  • Upon this, there's a site, at https://company.sharepoint.com/sites/Web Projects (B).

All I'm trying to do is curl a file into B.

I've tried

  • curl -c cookies.txt --sslv3 --ntlm -u email@domain.co.uk:pass --upload-file hi.txt http://company.sharepoint.com/sites/Web Projects/hi.txt,
  • Every variation humanly imaginable of cookies, sslv3, ntlm and --upload-file parameters for the curl,
  • Using -k as a parameter,
  • Delimiting the space between Web Projects in the URL using both octal and hexadecimal encoding (040,%20).
  • Quadruple-checking the password and username combination!

None of these seem to work, or at least they don't anymore.

Annoyingly, downloading a file is a simple matter of curl --ntlm -u email@domain.co.uk:pass -O "https://company.sharepoint.com/Web Projects/Form1.cs".

Is there a way to upload a file to SharePoint from the command line?

EDIT:

As per some comment requests, here's a verbose output from the curl command. Note that the curl command parameters I'm using was scientifically selected by process of "that seems to be getting me the furthest".

Note that some information has been dubbed (such as replacing a few characters in the IP address with #'s) so I'm not posting anything I might regret:

With -k option

sudo curl -vvv --ntlm -u email@domain.co.uk:PASS --upload-file ~/hi.txt -k "https://company.sharepoint.com/Web%20Projects/hi.txt"
* Hostname was NOT found in DNS cache
*   Trying 104.146.###.###...
* Connected to company.sharepoint.com (104.146.###.###) port 443 (#0)
* successfully set certificate verify locations:
*   CAfile: none
  CApath: /etc/ssl/certs
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server key exchange (12):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using ECDHE-RSA-AES256-SHA384
* Server certificate:
*    subject: C=US; ST=WA; L=Redmond; O=Microsoft Corporation; OU=Microsoft Corporation; CN=*.sharepoint.com
*    start date: 2016-02-23 19:42:10 GMT
*    expire date: 2018-02-22 19:42:10 GMT
*    issuer: C=US; ST=Washington; L=Redmond; O=Microsoft Corporation; OU=Microsoft IT; CN=Microsoft IT SSL SHA2
*    SSL certificate verify ok.
* Server auth using NTLM with user 'email@domain.co.uk'
> PUT /Web%20Projects/hi.txt HTTP/1.1
> Authorization: NTLM TlRMTVNTUAABAAAABoIIAAAAAAAAAAAAAAAAAAAAAAA=
> User-Agent: curl/7.35.0
> Host: company.sharepoint.com
> Accept: */*
> Content-Length: 0
> Expect: 100-continue
> 
< HTTP/1.1 401 Unauthorized
* Server Microsoft-IIS/8.5 is not blacklisted
< Server: Microsoft-IIS/8.5
< WWW-Authenticate: NTLM **base64 GIBBERISH REMOVED BECAUSE IT LOOKED LIKE SSL KEY**
< SPRequestGuid: 30fd909d-30c4-3000-1f64-de473aa63271
< request-id: 30fd909d-30c4-3000-1f64-de473aa63271
< Strict-Transport-Security: max-age=31536000
< X-FRAME-OPTIONS: SAMEORIGIN
< SPRequestDuration: 10
< SPIisLatency: 1
< X-Powered-By: ASP.NET
< MicrosoftSharePointTeamServices: 16.0.0.5423
< X-Content-Type-Options: nosniff
< X-MS-InvokeApp: 1; RequireReadOnly
< P3P: CP="ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI"
< Date: Mon, 18 Jul 2016 10:56:14 GMT
< Content-Length: 0
< 
* Connection #0 to host company.sharepoint.com left intact
* Issue another request to this URL: 'https://company.sharepoint.com/Web%20Projects/hi.txt'
* Found bundle for host company.sharepoint.com: 0x2117d00
* Re-using existing connection! (#0) with host company.sharepoint.com
* Connected to company.sharepoint.com (104.146.###.###) port 443 (#0)
* Server auth using NTLM with user 'email@domain.co.uk'
> PUT /Web%20Projects/hi.txt HTTP/1.1
> Authorization: NTLM **base64 GIBBERISH REMOVED BECAUSE IT LOOKED LIKE SSL KEY**
> User-Agent: curl/7.35.0
> Host: company.sharepoint.com
> Accept: */*
> Content-Length: 0
> Expect: 100-continue
> 
< HTTP/1.1 401 Unauthorized
* Server Microsoft-IIS/8.5 is not blacklisted
< Server: Microsoft-IIS/8.5
< SPRequestGuid: 30fd909d-20c6-3000-1f64-d2f727ac0347
< request-id: 30fd909d-20c6-3000-1f64-d2f727ac0347
< Strict-Transport-Security: max-age=31536000
< X-FRAME-OPTIONS: SAMEORIGIN
< SPRequestDuration: 17
< SPIisLatency: 1
* NTLM handshake rejected
* Authentication problem. Ignoring this.
< WWW-Authenticate: NTLM
< X-Powered-By: ASP.NET
< MicrosoftSharePointTeamServices: 16.0.0.5423
< X-Content-Type-Options: nosniff
< X-MS-InvokeApp: 1; RequireReadOnly
< P3P: CP="ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI"
< Date: Mon, 18 Jul 2016 10:56:16 GMT
< Content-Length: 0
* HTTP error before end of send, stop sending
< 
* Closing connection 0
* SSLv3, TLS alert, Client hello (1):

Without -k option

sudo curl -vvv --ntlm -u nick.bull@jgregan.co.uk --upload-file ~/hi.txt "https://company.sharepoint.com/Web%20Projects/hi.txt"
Enter host password for user 'email@domain.co.uk':
* Hostname was NOT found in DNS cache
*   Trying 104.146.###.###...
* Connected to company.sharepoint.com (104.146.###.###) port 443 (#0)
* successfully set certificate verify locations:
*   CAfile: none
  CApath: /etc/ssl/certs
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server key exchange (12):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using ECDHE-RSA-AES256-SHA384
* Server certificate:
*    subject: C=US; ST=WA; L=Redmond; O=Microsoft Corporation; OU=Microsoft Corporation; CN=*.sharepoint.com
*    start date: 2016-02-23 19:42:10 GMT
*    expire date: 2018-02-22 19:42:10 GMT
*    subjectAltName: company.sharepoint.com matched
*    issuer: C=US; ST=Washington; L=Redmond; O=Microsoft Corporation; OU=Microsoft IT; CN=Microsoft IT SSL SHA2
*    SSL certificate verify ok.
* Server auth using NTLM with user 'email@domain.co.uk'
> PUT /Web%20Projects/hi.txt HTTP/1.1
> Authorization: NTLM TlRMTVNTUAABAAAABoIIAAAAAAAAAAAAAAAAAAAAAAA=
> User-Agent: curl/7.35.0
> Host: company.sharepoint.com
> Accept: */*
> Content-Length: 0
> Expect: 100-continue
> 
< HTTP/1.1 401 Unauthorized
* Server Microsoft-IIS/8.5 is not blacklisted
< Server: Microsoft-IIS/8.5
< WWW-Authenticate: NTLM **base64 GIBBERISH REMOVED BECAUSE THIS LOOKED LIKE AN SSL KEY**
< SPRequestGuid: e3fc909d-2013-3000-1f64-d5aa6c556593
< request-id: e3fc909d-2013-3000-1f64-d5aa6c556593
< Strict-Transport-Security: max-age=31536000
< X-FRAME-OPTIONS: SAMEORIGIN
< SPRequestDuration: 9
< SPIisLatency: 2
< X-Powered-By: ASP.NET
< MicrosoftSharePointTeamServices: 16.0.0.5423
< X-Content-Type-Options: nosniff
< X-MS-InvokeApp: 1; RequireReadOnly
< P3P: CP="ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI"
< Date: Mon, 18 Jul 2016 10:50:57 GMT
< Content-Length: 0
< 
* Connection #0 to host company.sharepoint.com left intact
* Issue another request to this URL: 'https://company.sharepoint.com/Web%20Projects/hi.txt'
* Found bundle for host company.sharepoint.com: 0x832cf0
* Re-using existing connection! (#0) with host company.sharepoint.com
* Connected to company.sharepoint.com (104.146.###.###) port 443 (#0)
* Server auth using NTLM with user 'email@domain.co.uk'
> PUT /Web%20Projects/hi.txt HTTP/1.1
> Authorization: NTLM **base64 GIBBERISH REMOVED BECAUSE THIS LOOKED LIKE AN SSL KEY**
> User-Agent: curl/7.35.0
> Host: company.sharepoint.com
> Accept: */*
> Content-Length: 0
> Expect: 100-continue
> 
< HTTP/1.1 401 Unauthorized
* Server Microsoft-IIS/8.5 is not blacklisted
< Server: Microsoft-IIS/8.5
< SPRequestGuid: e3fc909d-2015-3000-1f64-d0e1cefbb2d4
< request-id: e3fc909d-2015-3000-1f64-d0e1cefbb2d4
< Strict-Transport-Security: max-age=31536000
< X-FRAME-OPTIONS: SAMEORIGIN
< SPRequestDuration: 15
< SPIisLatency: 1
* NTLM handshake rejected
* Authentication problem. Ignoring this.
< WWW-Authenticate: NTLM
< X-Powered-By: ASP.NET
< MicrosoftSharePointTeamServices: 16.0.0.5423
< X-Content-Type-Options: nosniff
< X-MS-InvokeApp: 1; RequireReadOnly
< P3P: CP="ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI"
< Date: Mon, 18 Jul 2016 10:50:57 GMT
< Content-Length: 0
* HTTP error before end of send, stop sending
< 
* Closing connection 0
* SSLv3, TLS alert, Client hello (1):
  • Can you capture and share the response headers and body you get on running the upload cUrl? – SlackerAmit Jul 15 '16 at 11:32
  • They're rather large... Could you clarify how you'd go about doing that or what information you'd like from them? – Nick Bull Jul 15 '16 at 12:30
  • 1
    you don't specify the version of sharepoint or the error (if any) as a result of running the command, which would help narrow it down. also, maybe you can try using curl verbose mode to get more data – Tiago Duarte Jul 18 '16 at 10:13
  • @TiagoDuarte Thank you for your response, it's the online SharePoint using Microsoft Outlook Web. Is there a version number for that? I would assume it's the same current version for everybody. – Nick Bull Jul 18 '16 at 10:14
  • 1
    typically we have SP2010 (v14), SP2013 (v15) and Office 365 (v16). on-premise (SharePoint Server) and online versions (Office 365/SharePoint Online) vary regarding the available APIs, so knowing this will allow us to tell which API or permissions issue you might be bumping into. I still would like to see the output of the command. also, in order to upload a document you should make sure that the user has at least Contribute permissions to the site – Tiago Duarte Jul 18 '16 at 10:26
2

From what I could find, in order to perform operations on Office 365, we need to authenticate using claims-based authentication, which is done in not one, but a series of sequential steps.

  • build a SAML request
  • submit a SAML token request to Microsoft Online Security Token Service
  • receive a signed security token
  • POST the token to SharePoint Online
  • receive FedAuth and rtFa authentication cookies
  • store the cookies in the client for use in subsequent requests

This has been explained in detail by Wictor Wilén.

Alternatively, and since you are using Linux, you may be interested in using a PHP Library:

Similar Posts:

  • This is pretty much how I achieved it using Python, with the requests library. In the end I created a simple module that allows you to authenticate in a single line: sharepy.session.connect("example.sharepoint.com"). The rest is achieved using standard REST API requests. – Jonathan Holvey Jul 19 '16 at 3:53
  • I've tried two different Python libraries, and while they are brilliant for list commands, there are none available that I can find that allow me to upload to a document library. How can I upload a file to a document library? – Nick Bull Jul 21 '16 at 15:28
  • 1
    You would make a POST request to the API endpoint http://site url/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/Files/add(url='a.txt',overwrite=true), and include the contents of the file in the body of the request. Remember to replace site url, /Folder Name and a.txt with actual values for your site, folder and filename. See this MSDN article for more details. – Jonathan Holvey Jul 25 '16 at 4:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.