使用 Sharepoint Online REST API 将列表项添加到 Sharepoint

问题描述 投票:0回答:1

如何将一个或多个值添加到 SharePoint Online 列表中的“人员”类型列?我所掌握的唯一信息是这些人的全名,例如“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()

感谢您的帮助!

python sharepoint-online sharepoint-rest-api
1个回答
0
投票

要仅使用全名将值添加到 SharePoint Online 列表中的“人员”类型列,您需要将这些名称解析为 SharePoint 中相应的用户 ID。您可以尝试此代码吗?请确保定义正确的参数):-

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.")
© www.soinside.com 2019 - 2024. All rights reserved.