pythonsharepoint-listoffice365-rest-client

How do I specify a list of fields in a ListCreationInformation object, when using Office365-REST-Python-Client to create a SharePoint List?


I want to create a new list with the same fields as an existing list.

I can get the fields for the existing list, and I have found a number of examples of creating a list with this library:

However none of these help me understand how to properly construct the ListCreationInformation object from an existing list with non-template fields.

Here's what I have so far:

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

Running this code as-is (without specifying the extra fields in the ListCreationInformation object) yields a ClientRequestException at the last line, of the form:

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

While the List has been created, and an item from the old list retrieved for insertion into the new list, obviously the missing_field_name field doesn't exist.

How do I update the ListCreationInformation object with the old field names?


Solution

  • You can add custom fields to a list like this

    from office365.sharepoint.fields.field_creation_information import FieldCreationInformation
    
    custom_field  = FieldCreationInformation('my_custom_field', field_type_kind=FieldType.Text)
    sp_new_list.fields.add(custom_field)
    

    You could iterate over your old_fields list and add them to the new list.

    If you want to make the my_custom_field visible in the default view of your new list:

    sp_new_list.default_view.view_fields.add_view_field('my_custom_field')