Categories

How to Upload a File to SharePoint On-Premises Using Python

How to Upload a File to SharePoint On-Premises Using Python

There are several tools out there for interacting with SharePoint through a Python script, but today, I am going to demonstrate a very simple way to upload a file to your SharePoint environment with minimal overhead. This can be useful for users running on Linux environments, such as a Raspberry PI, who want to script some functionality.

Using Octoprint to Monitor 3D Printing

I recently set up a tool called Octoprint on a Raspberry Pi, and am using it to control and monitor the 3D printer here at DMC.

The tool generates timelapses after each print, and it started to take up a lot of space on the Pi’s storage.

I used the following script to take newly rendered timelapses and upload them to SharePoint. This allows users to easily access their print timelapses and frees space on the pi.

Setup

You will need to install the requests package and NTLM authentication package on your environment. In a Linux environment, type the following commands:

pip install requests

pip install requests_ntlm

Python Script

#Import required packages
import sys
import requests
from requests_ntlm import HttpNtlmAuth

#Read filename (relative path) from command line
fileName = sys.argv[1]

#Enter your SharePoint site and target library
sharePointUrl = 'https://your.sharepointsite.com'
folderUrl = '/Your/SharePoint/DocumentLibrary'

#Sets up the url for requesting a file upload
requestUrl = sharePointUrl + '/_api/web/getfolderbyserverrelativeurl(\'' + folderUrl + '\')/Files/add(url=\'' + fileName + '\',overwrite=true)'

#Read in the file that we are going to upload
file = open(fileName, 'rb')

#Setup the required headers for communicating with SharePoint 
headers = {'Content-Type': 'application/json; odata=verbose', 'accept': 'application/json;odata=verbose'}

#Execute a request to get the FormDigestValue. This will be used to authenticate our upload request
r = requests.post(sharePointUrl + "/_api/contextinfo",auth=HttpNtlmAuth('Domain\\username','password'), headers=headers)
formDigestValue = r.json()['d']['GetContextWebInformation']['FormDigestValue']

#Update headers to use the newly acquired FormDigestValue
headers = {'Content-Type': 'application/json; odata=verbose', 'accept': 'application/json;odata=verbose', 'x-requestdigest' : formDigestValue}

#Execute the request. If you run into issues, inspect the contents of uploadResult
uploadResult = requests.post(requestUrl,auth=HttpNtlmAuth('Domain\\username','password'), headers=headers, data=file.read())

Browse to your SharePoint site, and you should now see the newly uploaded file.

Learn more about DMC's SharePoint services.

Comments

Curtis
# Curtis
Hi Afzal,

Sounds like there is an issue when you are attempting to get SharePoint context info (line 23). Please examine the value of r.json() to see what the specific error is. It is likely due to invalid credentials.
Afzal
# Afzal
Getting Key Error ; 'd' in FormdigestValue


formDigestValue = r.json()['d']['GetContextWebInformation']['FormDigestValue']
KeyError: 'd'

Post a comment

Name (required)

Email (required)

CAPTCHA image
Enter the code shown above:

Categories

Related Blog Posts