Office365-REST-Python-Client icon indicating copy to clipboard operation
Office365-REST-Python-Client copied to clipboard

Trouble Adding Item to Sharepoint List

Open DanielFisher92 opened this issue 5 years ago • 1 comments

I'm running version 2.3.1 on Windows 10 Enterprise and I'm attempting to add roughly 1800 items to a sharepoint list where the data comes from an excel file.

I've followed the examples by having a loop that iterates through the rows of the excel file, builds a new item from the data, adds it to the list and executes:

for _, row in data.iterrows():

	new_row = {
	"Title" : "N/A" if pd.isnull(row["Position ID"]) else str(row["Position ID"]),
	"PayrollName" : "N/A" if pd.isnull(row["Payroll Name"]) else str(row["Payroll Name"]),
	"LastName" : "N/A" if pd.isnull(row["Last Name"]) else str(row["Last Name"]),
	"FirstName" : "N/A" if pd.isnull(row["First Name"]) else str(row["First Name"])
	}

	target_list.add_item(new_row)
	context.execute_query()

When I do this I often get the following error and when I don't get an error is takes around 30 minutes to complete:

Traceback (most recent call last): File "upload.py", line 138, in populate_list(ls, ctx, data) File "upload.py", line 71, in populate_list context.execute_query() File "C:\Users\dfisher2\AppData\Local\Programs\Python\Python38\lib\site-packages\office365\runtime\client_runtime_context.py", line 138, in execute_query self.pending_request().execute_query() File "C:\Users\dfisher2\AppData\Local\Programs\Python\Python38\lib\site-packages\office365\runtime\client_request.py", line 74, in execute_query response = self.execute_request_direct(request) File "C:\Users\dfisher2\AppData\Local\Programs\Python\Python38\lib\site-packages\office365\runtime\odata\odata_request.py", line 34, in execute_request_direct return super(ODataRequest, self).execute_request_direct(request) File "C:\Users\dfisher2\AppData\Local\Programs\Python\Python38\lib\site-packages\office365\runtime\client_request.py", line 95, in execute_request_direct result = requests.post(url=request_options.url, File "C:\Users\dfisher2\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\api.py", line 119, in post return request('post', url, data=data, json=json, **kwargs) File "C:\Users\dfisher2\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\api.py", line 61, in request return session.request(method=method, url=url, **kwargs) File "C:\Users\dfisher2\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 542, in request resp = self.send(prep, **send_kwargs) File "C:\Users\dfisher2\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 655, in send r = adapter.send(request, **kwargs) File "C:\Users\dfisher2\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\adapters.py", line 498, in send raise ConnectionError(err, request=request) requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))

I then tried updating my code to use a batch execute which I thought might help with the error and speed things up:

for _, row in data.iterrows():

	new_row = {
	"Title" : "N/A" if pd.isnull(row["Position ID"]) else str(row["Position ID"]),
	"PayrollName" : "N/A" if pd.isnull(row["Payroll Name"]) else str(row["Payroll Name"]),
	"LastName" : "N/A" if pd.isnull(row["Last Name"]) else str(row["Last Name"]),
	"FirstName" : "N/A" if pd.isnull(row["First Name"]) else str(row["First Name"])
	}

	target_list.add_item(new_row)
context.execute_batch()

But whenever I call context.execute_batch() I get the following error:

Traceback (most recent call last): File "upload.py", line 138, in populate_list(ls, ctx, data) File "upload.py", line 82, in populate_list context.execute_batch() File "C:\Users\dfisher2\AppData\Local\Programs\Python\Python38\lib\site-packages\office365\sharepoint\client_context.py", line 106, in execute_batch batch_request.execute_query() File "C:\Users\dfisher2\AppData\Local\Programs\Python\Python38\lib\site-packages\office365\runtime\client_request.py", line 79, in execute_query raise ClientRequestException(*e.args, response=e.response) office365.runtime.client_request_exception.ClientRequestException: (None, None, '400 Client Error: Bad Request for url: https://cervusequipmentcorp.sharepoint.com/hr/_api/$batch')

Any ideas on where I'm going wrong?

DanielFisher92 avatar Dec 19 '20 20:12 DanielFisher92

I got the same issue. Maybe there are too many data to be added or updated.

samlee00123 avatar Jun 17 '21 02:06 samlee00123