Using Python to Create SharePoint List Items

KaneW1 0 Reputation points
2024-02-12T14:49:11.8733333+00:00

I work with a team that uses SharePoint to track hardware sales, and I'm trying to teach myself Python by using it to create list items for our sales records. My manager has set up a RestAPI and provided the below code to get me connected to our sharepoint, but I can't figure out how to use it to create new list items. I've tried using resources like https://o365.github.io/python-o365/latest/usage/sharepoint.html, but I'm still having trouble. Can anyone offer guidance on how to use Python to create new list entries in SharePoint? Thanks in advance and apologies for being very new and possibly foolish! Please note, I have tried the AI Generated answer that i created and have received this "new_item.add().execute_query() ^^^^^^^^^^^^ AttributeError: 'ListItem' object has no attribute 'add'"

## Imports
import os
import datetime

## Specific Imports for SharePoint rest API
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.listitems.listitem import ListItem

site_url = 'https://xxx.sharepoint.com/sites/xxx/xxx/'

cert_credentials = {
    'tenant': 'xxx.onmicrosoft.com',
    'client_id': 'xxx',
    'thumbprint': "xxx",
    'cert_path': '{xxx'.format(os.path.dirname(__file__)),
}
ctx = ClientContext(site_url).with_client_certificate(**cert_credentials)

#Get and print current url
current_web = ctx.web.get().execute_query()
print("##### Connected To #####")
print("Site URI: {0}".format(current_web.url))
Microsoft 365 and Office SharePoint For business Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Q&A Assist
    2024-02-12T14:49:11.8766667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To create new list entries in SharePoint using Python, you can use the SharePoint REST API. The code provided in the question sets up a connection to a SharePoint site using a client certificate. Once you have a connection, you can use the ListItem class to create a new list item and add it to a list.

    Here is an example of how to create a new list item using the SharePoint REST API in Python:

    from office365.sharepoint.listitems.listitem import ListItem
    
    # Set the URL of the SharePoint site and the list name
    site_url = 'https://xxx.sharepoint.com/sites/xxx/xxx/'
    list_name = 'Sales Records'
    
    # Get the list by name
    list_obj = ctx.web.lists.get_by_title(list_name)
    
    # Create a new list item
    new_item = ListItem(list_obj)
    new_item.set_property('Title', 'New Sales Record')
    new_item.set_property('Sales_x0020_Amount', 1000)
    new_item.set_property('Sales_x0020_Date', datetime.datetime.now())
    
    # Add the new list item to the list
    new_item.add().execute_query()
    

    This code creates a new list item in the "Sales Records" list with a title of "New Sales Record", a sales amount of 1000, and a sales date of the current date and time.

    Note that you will need to replace the site_url and list_name variables with the URL of your SharePoint site and the name of your list, respectively. You will also need to set up the ctx object to connect to your SharePoint site using the appropriate credentials.

    References:


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.