Thiago Luan
Thiago Luan

Reputation: 23

Add list item to Sharepoint using Sharepoint Online REST API

How do I add one or multiple values to a 'Person' type column in a SharePoint Online list? The only information I have is the full names of the people, such as 'Thiago Luan Pereira'.

from office365.runtime.auth.client_credential import ClientCredential
from office365.sharepoint.fields.user_value import FieldUserValue
import xlrd

# Configurações de conexão e autenticação
site_url = "https://tenant.sharepoint.com/sites/xxxxxxxx"
tenant = "tenant"
client_id = "clientid"
thumbprint = "tp"
cert_path = r"C:\Users\f034067\OneDrive - Aethra Sistemas Automotivos S.A\Área de Trabalho\xxxxxxx.pem"

try:
    print("Authenticating...")
    ctx = ClientContext(site_url).with_client_certificate(tenant, client_id, thumbprint, cert_path=cert_path)
    print("Successfully authenticated.")
except Exception as e:
    print(f"Authentication error: {e}")

sp_list = ctx.web.lists.get_by_title("Senior acessos")


new_item = sp_list.add_item({
                "Name":
}).execute_query()

I appreciate your help!

Upvotes: 0

Views: 55

Answers (1)

To add values to a 'Person' type column in a SharePoint Online list using only the full names, you need to resolve these names to their corresponding user IDs in SharePoint. Can you try this code make sure to define the correct paramters):-

from office365.runtime.auth.client_credential import ClientCredential
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.fields.user_value import FieldUserValue

# Authentication and connection setup
site_url = "https://<tenant>.sharepoint.com/sites/<site>"
tenant = "tenant"
client_id = "client_id"
thumbprint = "thumbprint"
cert_path = r"C:\path\to\certificate.pem"

# Names of people to add to the Person field
person_names = ["Thiago Luan Pereira"]

try:
    print("Authenticating...")
    ctx = ClientContext(site_url).with_client_certificate(tenant, client_id, thumbprint, cert_path=cert_path)
    print("Successfully authenticated.")
except Exception as e:
    print(f"Authentication error: {e}")
    exit()

# Reference the SharePoint list
sp_list = ctx.web.lists.get_by_title("Senior acessos")

# Resolve each person's name to a User ID
user_values = []
for person_name in person_names:
    try:
        user = ctx.web.site_users.get_by_email(person_name + "@<yourdomain>.com").execute_query()
        user_values.append(FieldUserValue.from_user(user.login_name))
    except Exception as e:
        print(f"Error resolving user {person_name}: {e}")

# Add a new item to the SharePoint list with the resolved user(s)
if user_values:
    new_item = sp_list.add_item({
        "Title": "Example Title",  # Replace with your actual required fields
        "PersonField": user_values  # Replace 'PersonField' with your actual Person/Group column internal name
    })
    new_item.execute_query()
    print("Item created successfully.")
else:
    print("No valid users found.")

Upvotes: 0

Related Questions