【发布时间】:2021-06-22 21:41:05
【问题描述】:
我想创建一个与现有列表具有相同字段的新列表。
我可以获取现有列表的字段,并且我发现了许多使用此库创建列表的示例:
但是,这些都不能帮助我理解如何从具有非模板字段的现有列表中正确构造 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 对象?
【问题讨论】:
标签: python sharepoint-list office365-rest-client