微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 Office365-REST-Python-Client 创建 SharePoint 列表时,如何在 ListCreationInformation 对象中指定字段列表?

如何解决使用 Office365-REST-Python-Client 创建 SharePoint 列表时,如何在 ListCreationInformation 对象中指定字段列表?

我想创建一个与现有列表具有相同字段的新列表。

我可以获取现有列表的字段,并且我找到了许多使用此库创建列表的示例:

但是,这些都没有帮助我理解如何从具有非模板字段的现有列表中正确构造 ListCreationinformation 对象。

这是我目前所拥有的:

from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.lists.list_creation_information import ListCreationinformation
from office365.sharepoint.lists.list_template_type import ListTemplateType

# placeholders for my actual details
username = input()
password = input()
url = 'https://company.sharepoint.com/sites/Existing%20Site'
old_list_name = 'Existing%20List'
new_list_name = 'New%20List'

# authenticate and get a context object
ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_user(username,password):
    ctx = ClientContext(url,ctx_auth)
    web = ctx.web
    ctx.load(web)
    ctx.execute_query()
    print("Connected to SharePoint Site: {0}".format(web.properties['Title']))
else:
    print(ctx_auth.get_last_error())

# get field names from the old list
sp_existing_list = ctx.web.lists.get_by_title(old_list_name)
old_fields = sp_existing_list.fields
ctx.load(old_fields)
ctx.execute_query()

# define the new list
lci = ListCreationinformation()
lci.BaseTemplate = ListTemplateType.GenericList
lci.Title = new_list_name
lci.AllowContentTypes = true
#
# what do I need here,to add the old list field details?
#

# create the new list
sp_new_list = ctx.web.lists.add(lci)
ctx.execute_query()

# add an item from the old list to the new list to confirm
items = sp_existing_list.get_items()
ctx.load(items)
ctx.execute_query()
sp_new_list.add_item(items[0].properties)
ctx.execute_query()

按原样运行此代码(不指定 ListCreationinformation 对象中的额外字段)在最后一行产生 ClientRequestException,形式如下:

ClientRequestException: ('-1,Microsoft.SharePoint.Client.InvalidClientQueryException',"The property 'missing_field_name' does not exist on type 'SP.Data.New_x0020_ListListItem'. Make sure to only use property names that are defined by the type.","400 Client Error: Bad Request for url: https://company.sharepoint.com/sites/Existing%20Site/_api/Web/lists/GetById('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')/items")

虽然已经创建了列表,并且从旧列表中检索了一个项目以插入到新列表中,但显然 missing_field_name 字段不存在。

如何使用旧的字段名称更新 ListCreationinformation 对象?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。