Office365-rest-python-client: 如何更新单个列表项?

创建于 2018-05-10  ·  4评论  ·  资料来源: vgrem/Office365-REST-Python-Client

我正在使用 SharePoint listitem.py 的更新方法
item_object.update()
但是我如何放置我想要更新的数据? 例如:
item_properties = {'__metadata': {'type': 'SP.Data.'+listTitle+'ListItem'},
'标题':'新项目',
'价值':99,
}
然后更新项目...

question

最有用的评论

上一个示例对我不起作用 - 得到 204 响应代码而没有实际更新。
正确的方法 - 通过方法设置对象属性:

# 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()

所有4条评论

如果我理解正确,您想更新已与 Sharepoint 同步的对象。 这是我实现它的方式:

    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()

上一个示例对我不起作用 - 得到 204 响应代码而没有实际更新。
正确的方法 - 通过方法设置对象属性:

# 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()

已解决 #146

谢谢你的例子!
我建议关闭它,因为它已经解决了。

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

oktorok picture oktorok  ·  5评论

Cesaaar picture Cesaaar  ·  7评论

Mark531 picture Mark531  ·  11评论

attibalazs picture attibalazs  ·  10评论

domdinicola picture domdinicola  ·  4评论