Get List Items Sharepoint Online using Python

I recently became interested in learning python as seen lot interest among community members.

How I started, if you are interested here are the references:

Introduction to Python

https://leanpub.com/PowerShell-to-Python – ebook

https://www.pluralsight.com/ – Core Python (Video training)

I started with some Active directory coding just for learning and then switched my gears towards Office 365 as that is what our customers are mainly using.

I am not getting in details on how to install python on your windows machine as that is super easy.

For IDE you can use Pycharm Community edition

Let’s now get back to the topic and get list items using Python.

With PowerShell you use SharePoint SDK which use client-side object model , similarly in Python we are utilizing Office365-REST-Python-Client

Install it by using Command : pip install Office365-REST-Python-Client

 

I have created a test list in my tenant for demonstration purposes with two columns as shown below:

It has 3 items that we will read and print on screen using python program.

 

Here is the Python Code which is similar to Powershell Client Side Object Model

#########################code#########################

from office365.runtime.auth.user_credential import UserCredential
from office365.sharepoint.client_context import ClientContext
site_url = "https://techwizard.sharepoint.com/sites/TeamSIte/"
sp_list = "DL Modification"
ctx = ClientContext(site_url).with_credentials(UserCredential("vstest@techwizard.cloud", "pass54321"))
sp_lists = ctx.web.lists
s_list = sp_lists.get_by_title(sp_list)
l_items = s_list.get_items()
ctx.load(l_items)
ctx.execute_query()

for item in l_items:
    print(item.properties['Title'],item.properties['Check'])

#########################code#########################

When we execute it will print the list items as shown below:

 

Note: Account should not be MFA enabled and check Conditional access policies are bypassed for it if that is causing issues.

I will be sharing more python examples and may be some scripts as time permits in coming future from different Microsoft products as I move with my journey to python.

 

 

Thanks for reading…

Tech Wizard

https://techwizard.cloud

https://syscloudpro.com/

PowerShell Cheat Book

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s