Articles Interacting with Sharepoint using Python - Part 1 by Ashirwad Satapathi

emailx45

Бывалый
Staff member
Moderator
Interacting with Sharepoint using Python - Part 1
Ashirwad Satapathi - 16/Jun/2020
[SHOWTOGROUPS=4,20]
How to interact with Sharepoint using your Python application.

In this article, we will go through some basic functionalities that we can perform on Sharepoint List using Python and Shareplum.

Introduction
This is a tutorial on using Python to interact with Sharepoint, using an open source Python library, Shareplum. In this article, we will go through installing Shareplum, establishing a connection with the Sharepoint account and create a list, add records to your List, update existing records in the list and later retrieve all the records from the list.

Background
Lately, I was working on a project in which we used to extract information from a document. Now, there was a requirement to integrate the application with an existing Sharepoint workflow. But since the project was built using Python and Django, I had to find a way to interact with Sharepoint using Python or else I would have to make a Web API using ASP.NET and later consume it in my application. As by using CSOM, you can interact with Sharepoint with ASP.NET but this was not a viable option.

Thus, I had to search a Python library which supported the cause of interacting with Sharepoint using Python. After doing a lot of search and finding the way around, finally, I found Shareplum which is an open source project in Python, which lets you interact with Sharepoint, taking away all the heavy lifting for you and allowing you to focus on your task.

Installation
Open Command Prompt/Powershell/Terminal, then enter the following to install using pip:
Code:
pip install shareplum
If you have Git installed, you can install it from the source as follows:
Code:
git clone git://github.com/jasonrollins/shareplum
cd shareplum
python setup.py install
Authentication
We are going to connect with our Sharepoint server using the Shareplum library by authenticating our application by giving the credentials to access the Sharepoint server.
Code:
from shareplum import Site
from shareplum import Office365
from shareplum.site import Version

sharepointUsername = "abc@xyz.com"
sharepointPassword = "@123$"
sharepointSite = "https://abc.sharepoint.com/sites/MySite"
website = "https://abc.sharepoint.com"

authcookie = Office365(website, username=sharepointUsername,
password=sharepointPassword).GetCookies()
site = Site(sharepointSite, version=Version.v2016, authcookie=authcookie)
Add a List to your Sharepoint Site
Now we are going to look at the code to add a new list to our Sharepoint site in the below code sample:
Code:
site.AddList('ListName', description='Great List!', templateID='Custom List')
Here, we added a new list to our Sharepoint site called ListName.

Add a Item to Your Sharepoint Site List
Now we are going to look at the code to add a few new items to our list in the Sharepoint site in the below code sample:
Code:
set_list = site.List('ListName')
my_data = data=[{'FirstName': 'Ashirwad','LastName':'Satapathi'},
{'FirstName': 'Alice','LastName':'Wonderland'}]
set_list.UpdateListItems(data=my_data, kind='New')
In the above code snippet, we have added two elements to our list ListName.

Update an Item to Your Sharepoint Site List
Now we are going to look at the code to update a few records in the list of our Sharepoint site in the below code sample:
Code:
my_data = data=[{'ID':'1','FirstName': 'Ash','LastName':'Satapathi'},
{'ID':'2''FirstName': 'Alise','LastName':'Wonderland'}]
set_list.UpdateListItems(data=my_data, kind='Update')
In the above code snippet, we have updated the two record to our list ListName with the help of the ID column of List.

Fetch Data from Your Sharepoint Site List
Now we are going to look at the code to fetch all the records from the list in our Sharepoint site in the below code samples.
One way is to fetch all the data from the list is as follows:
Code:
sp_data = set_list.GetListItems()
But if you wanted to fetch data of selected columns from the list, then try as follows:
Code:
sp_data = set_list.GetListItems(fields=['ID','FirstName'])
In the above code snippet, we fetched data from list ListName with the help of the GetListItems method of Shareplum.

Points of Interest
  • Using Python to create a List in Sharepoint
  • Add records to Sharepoint List using Python
  • Update records from Sharepoint List using Python
  • Fetch records from Sharepoint List using Python
History
  • 16th June, 2020: Initial version

License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

[/SHOWTOGROUPS]
 
Top