Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue updating Choices when adding New Field #861

Closed
sfairchild3 opened this issue May 16, 2024 · 1 comment
Closed

Issue updating Choices when adding New Field #861

sfairchild3 opened this issue May 16, 2024 · 1 comment
Labels

Comments

@sfairchild3
Copy link

I have been successful adding fields to an existing list in Sharepoint with FieldCreationInformation with just the column name and column type but when I try using the choices parameter I am getting an HTTP Error.

I am using Office365-REST-Python-Client 2.5.3

Here is my code:

``
from office365.sharepoint.fields.creation_information import FieldCreationInformation
from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.client_credential import ClientCredential

ctx = ClientContext(site_url).with_credentials(ClientCredential(client_id, client_secret))

choices= ["Choice 1", "Choice 2"]

field_info = FieldCreationInformation("TestChoice", FieldType.Choice, choices=choices)
existing_list = ctx.web.lists.get_by_title("SI Tracker - Dev")
existing_list.fields.add(field_info)
ctx.execute_query()


Here is the error:

---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
~\AppData\Local\ESRI\conda\envs\clone-pro-2\lib\site-packages\office365\runtime\client_request.py in execute_query(self, query)
     37             response = self.execute_request_direct(request)
---> 38             response.raise_for_status()
     39             self.process_response(response, query)

~\AppData\Local\ESRI\conda\envs\clone-pro-2\lib\site-packages\requests\models.py in raise_for_status(self)
    940         if http_error_msg:
--> 941             raise HTTPError(http_error_msg, response=self)
    942 

HTTPError: 400 Client Error: Bad Request for url: https://pge.sharepoint.com/sites/VMDataRequests15/_api/Web/lists/GetByTitle('SI%20Tracker%20-%20Dev')/Fields

During handling of the above exception, another exception occurred:

ClientRequestException                    Traceback (most recent call last)
<ipython-input-92-c3b89a42f9b7> in <module>
     11 existing_list = ctx.web.lists.get_by_title("SI Tracker - Dev")
     12 existing_list.fields.add(field_info)
---> 13 ctx.execute_query()

~\AppData\Local\ESRI\conda\envs\clone-pro-2\lib\site-packages\office365\runtime\client_runtime_context.py in execute_query(self)
    183         while self.has_pending_request:
    184             qry = self._get_next_query()
--> 185             self.pending_request().execute_query(qry)
    186         return self
    187 

~\AppData\Local\ESRI\conda\envs\clone-pro-2\lib\site-packages\office365\runtime\client_request.py in execute_query(self, query)
     40             self.afterExecute.notify(response)
     41         except HTTPError as e:
---> 42             raise ClientRequestException(*e.args, response=e.response)
     43 
     44     def execute_request_direct(self, request):

ClientRequestException: ('-1, Microsoft.SharePoint.Client.InvalidClientQueryException', "A node of type 'StartArray' was read from the JSON reader when trying to read a value of a property; however, a 'PrimitiveValue' or 'StartObject' node was expected.", "400 Client Error: Bad Request for url: [https://pge.sharepoint.com/sites/VMDataRequests15/_api/Web/lists/GetByTitle('SI%20Tracker%20-%20Dev')/Fields")](https://pge.sharepoint.com/sites/VMDataRequests15/_api/Web/lists/GetByTitle('SI%20Tracker%20-%20Dev')/Fields%22))



@vgrem
Copy link
Owner

vgrem commented Jun 3, 2024

Greetings,
thank you for reporting it, this issue will be addresed in the next library version.

Meanwhile FieldCollection.add_field could be utilized to create a choice field instead of FieldCollection.add method:

existing_list.fields.add_field(field_info)

or more specific FieldCollection.add_choice_field method:

choices = ["Not Started", "In Progress", "Completed", "Deferred"]
field = lib.fields.add_choice_field(title=field_name, values=choices).execute_query()

@vgrem vgrem closed this as completed Jun 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants