Try Except Python Upload File to Sharepoint

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

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

    How To, Custom SharePoint, Python

There are several tools out there for interacting with SharePoint through a Python script, just today, I am going to demonstrate a very simple mode 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 Press

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

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

I used the following script to have newly rendered timelapses and upload them to SharePoint. This allows users to easily access their impress timelapses and frees infinite on the pi.

Setup

Y'all will need to install the requests package and NTLM authentication parcel on your environs. 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 upwards the url for requesting a file upload requestUrl = sharePointUrl + '/_api/web/getfolderbyserverrelativeurl(\'' + folderUrl + '\')/Files/add together(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': 'awarding/json;odata=verbose'}  #Execute a request to get the FormDigestValue. This volition be used to authenticate our upload request r = requests.post(sharePointUrl + "/_api/contextinfo",auth=HttpNtlmAuth('Domain\\username','countersign'), headers=headers) formDigestValue = r.json()['d']['GetContextWebInformation']['FormDigestValue']  #Update headers to employ the newly acquired FormDigestValue headers = {'Content-Type': 'application/json; odata=verbose', 'accept': 'application/json;odata=verbose', 'x-requestdigest' : formDigestValue}  #Execute the asking. If you see issues, audit 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 lot should at present see the newly uploaded file.

Learn more about DMC's SharePoint Consulting Services and Digital Workplace Solutions squad. Contact us for your next custom SharePoint project.

Comments

Alexandre Willame

Thank you for the post.

Quick question: for download, what is typically the office to utilise?

Thanks,

Curtis Weir

Hi Casey,

Subsequently uploading the certificate, you'll need to make a dissever call to update the document metadata. The "uploadResult" variable should contain the ID of the new document. Please utilise the following resource to update the list particular (laissez passer the ID to the url)

https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-lists-and-list-items-with-rest#update-list-particular

Casey

Hullo Curtis,

I realize this was posted a yr agone, but I hope you lot can offering me a little guidance. I was able to piece of work through this snippet and go a document uploaded to SharePoint, now I am looking for how to ship over the other fields nosotros take for the document library. Any assistance would be profoundly appreciated.

Casey

Curtis

How-do-you-do Afzal,

Sounds like in that location is an issue when you are attempting to go SharePoint context info (line 23). Please examine the value of r.json() to encounter what the specific fault is. It is likely due to invalid credentials.

Afzal

Getting Key Mistake ; 'd' in FormdigestValue

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

spanoofue1966.blogspot.com

Source: https://www.dmcinfo.com/latest-thinking/blog/id/9388/how-to-upload-a-file-to-sharepoint-on-premises-using-python

0 Response to "Try Except Python Upload File to Sharepoint"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel