My objective is to read a csv file using pandas and add records to sharepoint list (Mahesh_Demo) in truncate-load fashion.
My initial Sample code worked Working/Sample Code
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.runtime.client_request import ClientRequest
from office365.sharepoint.client_context import ClientContext
import pandas as pd
df = pd.read_csv('rt_PP_digital_door_Access.csv',dtype=str)
df = df.iloc[:100,:]
app_settings = {
'url': 'https://host/sites/site_name/',
'client_id': 'client-id',
'client_secret': 'client_secret'
}
context_auth = AuthenticationContext(url=app_settings['url'])
context_auth.acquire_token_for_app(client_id=app_settings['client_id'],
client_secret=app_settings['client_secret'])
ctx = ClientContext(app_settings['url'], context_auth)
sp_list = ctx.web.lists.get_by_title("Mahesh_Demo")
# Truncate - Working
items = sp_list.items.get().top(1000000).execute_query()
for item in items: # type: ListItem
item.delete_object()
ctx.execute_batch()
# add records from csv to sharepoint list
for i in range(df.shape[0]):
sp_list.add_item({
'Title': str(df.loc[i,"Title"]),
'Name': str(df.loc[i,"Name"]),
"Surname": str(df.loc[i,"Surname"])
})
ctx.execute_query()
Actual code (Not working)
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.runtime.client_request import ClientRequest
from office365.sharepoint.client_context import ClientContext
import pandas as pd
df = pd.read_csv('rt_PP_digital_door_Access.csv',dtype=str)
df = df.iloc[:100,:]
app_settings = {
'url': 'https://host/sites/site_name/',
'client_id': 'client-id',
'client_secret': 'client_secret'
}
context_auth = AuthenticationContext(url=app_settings['url'])
context_auth.acquire_token_for_app(client_id=app_settings['client_id'],
client_secret=app_settings['client_secret'])
ctx = ClientContext(app_settings['url'], context_auth)
sp_list = ctx.web.lists.get_by_title("Mahesh_Demo")
# Truncate - Working
items = sp_list.items.get().top(1000000).execute_query()
for item in items: # type: ListItem
item.delete_object()
ctx.execute_batch()
# add records from csv to sharepoint list
for i in range(df.shape[0]):
sp_list.add_item({
'Title':str(i),
'CATEGORY': str(df.loc[i,"CATEGORY"]),
'APPLICATION_TYPE': str(df.loc[i,"APPLICATION_TYPE"]),
'GXP_OR_NON_GXP': str(df.loc[i,"GXP_OR_NON_GXP"]),
'USER_ID': str(df.loc[i,"USER_ID"]),
'USER_NAME': str(df.loc[i,"USER_NAME"]),
'OBJECT_NAME': str(df.loc[i,"OBJECT_NAME"]),
'SQL_DATABASE': str(df.loc[i,"SQL_DATABASE"]),
'WORKBOOK_LINK': str(df.loc[i,"WORKBOOK_LINK"])
})
ctx.execute_query()
Error I'm getting
ClientRequestException: ('-1, Microsoft.SharePoint.Client.InvalidClientQueryException',
"The property 'CATEGORY' does not exist on type 'SP.Data.Mahesh_x005f_DemoListItem'. Make sure to only use property names that are defined by the type.",
"400 Client Error: Bad Request for url: https://mytakeda.sharepoint.com/sites/INSIGHTSANDANALYTICSDIGITALDOOR/_api/Web/lists/GetByTitle('Mahesh_Demo')/items")
I found the below solution on web which is on REST API. But I don't know how to add the Content-Type header to the add_item method.
Solution - Ensure that the Content-Type request header value is application/json;odata=verbose and not application/json.
Please let me know how to resolve this error.
Thanks in advance.