Thought leadership from the most innovative tech companies, all in one place.

List & Download Files from SharePoint Using Python

Download files from MS SharePoint using Office365 Python SDK

Photo by  Johny vino  on  Unsplash

Photo by Johny vino on Unsplash

Introduction:

This article explains how to download your documents to Microsoft SharePoint using Python. We will be using the python office365 SDK client to interact with SharePoint.

If you'd like to know how to upload files to SharePoint, then please click here.

What is a SharePoint?

SharePoint in Microsoft 365 is a cloud-based service, hosted by Microsoft, for businesses of all sizes. Instead of installing and deploying the SharePoint Server on-premises, any business can subscribe to a Microsoft 365 plan or to the standalone SharePoint Online service. Your employees can create sites to share documents and information with colleagues, partners, and customers.

We will be using the python office365 SDK client to interact with the share point.

Requirements:

  • Install Office365 Python client
  • Initiate the authentication flow
  • Download the file from Microsoft SharePoint

Install Office365 Python client:

$pip install Office365-REST-Python-Client

https://pypi.org/project/Office365-REST-Python-Client/

https://github.com/vgrem/Office365-REST-Python-Client

Create a SharePoint client context object:

To work with SharePoint APIs, we need to create a ClientContext object. This can be done with either:

  • username and password (or)
  • client id and client secret.

For most of the real-time use cases, we may not be using username and password. Therefore, in this article, we will create the context object using client id and secret.

Bonus: The _client id_ and _secret_ can preferably be stored in a vault or any convenient way as you do in your project. If you'd like to have a look at storing secrets in _vault_, please read this article.

i) Using app credentials:

Creating context object using client id and secret

ii) Using user credentials:

Now that we have received the context object, let's start interacting with the SharePoint APIs.

List all files from a SharePoint Directory:

The above code should return all the files from the requested URL.

Download files from SharePoint:

The above code will download your files from SharePoint to the file path mentioned.

Summary:

  • We have used Office365 Python SDK to interact with share point. We could very well do the same with other Microsoft products like MS Outlook, OneDrive, Teams, etc.
  • We have 2 types of auth flows to create a context object, App credentials, and User credentials.
  • Create a context object and use the same to perform any operation in SharePoint.

Thank you for reading.




Continue Learning