Shareplum - Delete rows Sharepoint list

2.4k views Asked by At

Currently I have a few codes running that fills some Sharepoint lists using Python in combination with Shareplum. The only thing I don't get is how to remove rows from a specific list. I want to do this using Python, because any list with a lot of items in Sharepoint doesn't fully load straight away and select all only picks a few rows.

On the documentation site of SharePlum i found this: SharePlum Documentation on UpdateListItems As a result I tried two options with no results:

......
#Login to Sharepoint
sp_list = site.List('Name of list')
sp_data = sp_list.GetListItems('All Items')

#Option 1:
sp_list.UpdateListItems(data=sp_data, kind='Delete')

#Option 2:
lengte = [i for i in range(1, len(sp_data))]
sp_list.UpdateListItems(data=lengte, kind='Delete')

Both ran without an error and I was able to print the sp_list correctly, but nothing happend on the Sharepoint list. Hopefully someone has encountered this before?

2

There are 2 answers

1
Rajalakshmi On

I am using Shareplum package and passing list item IDs to the updatelistitems function for deleting records from SharePoint List. Note that the below code works only if the total number of list items is less than or equal to 5000.

from shareplum import Office365,Site
from shareplum.site import Version
authcookie = Office365(server_url, username = Username, 
password=Password).GetCookies()
site = Site(site_url, version=Version.v365, authcookie=authcookie)
sp_list = site.List('list_name')
fields=['ID']
sp_data =sp_list.GetListItems(fields=fields)
del_df=pd.DataFrame.from_dict(sp_data)
list_data = del_df['ID'].tolist()
sp_list.UpdateListItems(data=list_data,kind='Delete')
0
Denys On

When kind='Delete' it accepts ID (row numbers) represented as strings. It worked out for me:

# repr(1) = '1'
lengte = list(map(repr, range(1, len(sp_data)))
sp_list.UpdateListItems(data=lengte, kind='Delete')