Performing A CRUD Operation On A SharePoint List Using Python


SharePoint is a web-site based collaboration system that uses workflow applications, list databases, and other web parts and security features to empower business teams to work together. SharePoint also gives the company using the platform the ability to control access to the information and automate the workflow processes across business units.
In simpler words, SharePoint allows you to manage all of your corporate content, creation of websites, and the organization's intranets to keep your users informed on what is happening, creating business process automation to automate tedious and mundane tasks and build customized apps that can help to boost your team's productivity.

Problem Statement

Lately, I came across a problem while making a POC on Invoice automation where i had to extract data from an invoice and store it in a SharePoint list. The problem was that the Invoice Processing System that I built was made using Python and Django, so finding a way to achieve the task seemed difficult in the initial days. After doing some research and looking for some resources, I stumbled into a Python library called shareplum which helps you to interact with SharePoint using Python.

What is Shareplum?

Shareplum is an open-source python library that makes it easy to interact with SharePoint services. It handles all of the messy parts of dealing with SharePoint and allows you to focus on your business requirement and write clean code.

Installing Shareplum in Windows/Ubuntu

Open Command Prompt / Terminal, then execute the below-mentioned statement to install the package using pip,
pip install shareplum


After installing the shareplum library, we need to connect to our SharePoint Server to access it and to perform our operations as per our business requirement. To allow our application to get access to SharePoint server, we need to provide the credentials to our application which it can use to establish a connection with SharePoint using shareplum:
  1. from shareplum import Site    
  2. from shareplum import Office365    
  3. from import Version    
  4. sharepointUsername = ""    
  5. sharepointPassword = "@123$"    
  6. sharepointSite = ""    
  7. website = ""    
  8. authcookie = Office365(website, username=sharepointUsername,    
  9. password=sharepointPassword).GetCookies()    
  10. site = Site(sharepointSite, version=Version.v2016, authcookie=authcookie)     

Create a new item in our SharePoint List

Assuming that we already have a list in SharePoint to perform the various operations, we are going to have a look at the code to add a few new list items to our List present in our SharePoint site.
  1. set_list = site.List('ListName')    
  2. my_data = data=[{'FirstName''Ashirwad','LastName':'Satapathi'},      
  3. {'FirstName''Alice','LastName':'Wonderland'}]      
  4. set_list.UpdateListItems(data=my_data, kind='New')      
In the above written code, we have added two items to an existing list called ListName.

Update an existing item in our SharePoint List

Now, since we have already added a few items to our SharePoint List, now we are going to have a look at the code snippet to update the existing list items on the basis of the ID, which acts as an unique identifier for each list item in a SharePoint List.
  1. my_data = data=[{'ID':'1','FirstName''Ash','LastName':'Satapathi'},    
  2. {'ID':'2''FirstName''Alise','LastName':'Wonderland'}]    
  3. set_list.UpdateListItems(data=my_data, kind='Update')    

Retrieve All Existing Items from our SharePoint List

Now, since we have already added and modified list items to our existing SharePoint List, Let’s have a look at a sample code to fetch all the existing list items from our SharePoint List.
  1. sp_data = set_list.GetListItems()  
To fetch the data for custom columns list items, kindly refer the below mentioned code snippet:
  1. sp_data = set_list.GetListItems(fields=['ID','FirstName'])  

Delete an Existing List Item from our SharePoint List

Now as we have already looked at the code snippets to perform the create, update and read operations, let’s have a look at how to perform the delete operations on Sharepoint List using Shareplum. Refer to the below code snippet to delete a SharePoint list using Shareplum.
  1. set_list = site.List('ListName')    
  2. my_data = data=[{'ID':1,'FirstName''Ashirwad','LastName':'Satapathi'},    
  3. {'ID':2,'FirstName''Alice','LastName':'Wonderland'}]    
  4. set_list.UpdateListItems(data=my_data, kind='delete')    


By using Shareplum, connecting SharePoint servers to your Python applications has become really easy. We can use simple method calls to achieve tasks that would have required to code a lot of things while using Rest API endpoints exposed by Microsoft to interact with the SharePoint server. Shareplum provides a wrapper that does all the tedious tasks for you. Making your life easy and allowing you to spend more time solving your problems and business requirements.