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

sp_list.fields.add(field_payload).execute_query() has no attribute 'FieldTypeKind' #925

Open
garagonc opened this issue Dec 18, 2024 · 0 comments
Labels

Comments

@garagonc
Copy link

I'm trying to add headers to an existing Sharepoint list but I received the error message:
'dict' object has no attribute 'FieldTypeKind'

The code I'm using is the following:

import pandas as pd
from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.user_credential import UserCredential

SharePoint credentials

USERNAME = username
PASSWORD = password
SITE_URL = site_url

Excel file path

EXCEL_FILE_PATH = excel_file_path

Existing SharePoint list name

EXISTING_LIST_NAME = "Listname"

def connect_to_sharepoint(site_url, username, password):
"""
Authenticate and connect to SharePoint.
"""
try:
ctx = ClientContext(site_url).with_credentials(UserCredential(username, password))
print(f"Connected to SharePoint site: {site_url}")
return ctx
except Exception as e:
print(f"Error connecting to SharePoint: {e}")
return None

def add_missing_columns(sp_list, df_columns, existing_field_names):
"""
Add missing columns to the SharePoint list based on the Excel file headers.
"""
for column in df_columns:
if column not in existing_field_names:
print(f"Adding missing column '{column}'...")
try:
# Define the field payload
field_payload = {
"__metadata": {"type": "SP.FieldText"},
"Title": column,
"FieldTypeKind": 2, # 2 corresponds to 'Text' field type
}
sp_list.fields.add(field_payload).execute_query()
print(f"Column '{column}' created successfully.")
except Exception as e:
print(f"Error creating column '{column}': {e}")
else:
print(f"Column '{column}' already exists. Skipping...")

def main():
"""
Main function to execute the SharePoint data upload process.
"""

# Connect to SharePoint
ctx = connect_to_sharepoint(SITE_URL, USERNAME, PASSWORD)
if ctx is None:
    return

try:
    # Access the existing SharePoint list
    sp_list = ctx.web.lists.get_by_title(EXISTING_LIST_NAME)
    print(f"List '{EXISTING_LIST_NAME}' exists. Validating and adding columns...")

    # Retrieve existing fields in the SharePoint list
    existing_fields = sp_list.fields.get().execute_query()
    print("Got existing fields")
    existing_field_names = [field.properties["Title"] for field in existing_fields]

    print(f"Existing fields in the list: {existing_field_names}")

    # Add missing columns dynamically
    add_missing_columns(sp_list, df.columns, existing_field_names)


except Exception as e:
    print(f"Error: {e}")

if name == "main":
main()

@vgrem vgrem added the question label Dec 19, 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