Office365-rest-python-client: How to update a single list item?

Created on 10 May 2018  ·  4Comments  ·  Source: vgrem/Office365-REST-Python-Client

I am using the update method of SharePoint listitem.py
item_object.update()
but how can I put the data I want to update? for instance:
item_properties = {'__metadata': {'type': 'SP.Data.'+listTitle+'ListItem'},
'Title': 'new item',
'Value':99,
}
and then update the item...

question

Most helpful comment

Previous example not works for me - got 204 response code without actual update.
The right way - set object properties via method:

# Open list object from portal
ctx_auth = AuthenticationContext(url=sharepoint_site_url)
ctx = ClientContext(sharepoint_site_url, ctx_auth)
target_list = ctx.web.lists.get_by_title(sharepoint_root_folder_name)

# Fetch list item object by id and set it's properties
item = target_list.get_item_by_id(item_id)
item.set_property('Title', 'new-title')
item.set_property('NewItem', '99')

# Update list item object and send request back to portal
item.update()
ctx.execute_query()

All 4 comments

If I understand correctly, you want to update an object already synced with Sharepoint. Here's how I achieved it:

    ctx_auth = AuthenticationContext(url=sharepoint_site_url)
    ctx = ClientContext(sharepoint_site_url, ctx_auth)
    target_list = ctx.web.lists.get_by_title(sharepoint_root_folder_name)

    # Fetch item by id and add properties to it.
    item = target_list.get_item_by_id(item_id)
    item.properties.update({
        "NewItem": "99"
    })
    item.update()
    ctx.execute_query()

Previous example not works for me - got 204 response code without actual update.
The right way - set object properties via method:

# Open list object from portal
ctx_auth = AuthenticationContext(url=sharepoint_site_url)
ctx = ClientContext(sharepoint_site_url, ctx_auth)
target_list = ctx.web.lists.get_by_title(sharepoint_root_folder_name)

# Fetch list item object by id and set it's properties
item = target_list.get_item_by_id(item_id)
item.set_property('Title', 'new-title')
item.set_property('NewItem', '99')

# Update list item object and send request back to portal
item.update()
ctx.execute_query()

Resolved with #146

Thanks for those examples!
I propose to close it since was resolved.

Was this page helpful?
0 / 5 - 0 ratings