使用 python api 更改共享点中的文件自定义属性/列

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

我正在尝试使用 python api 更新共享点中文件的自定义创建列。

(在写这篇文章时我解决了这个问题,请参阅下面的答案)

python sharepoint properties sharepoint-api
1个回答
0
投票

以下函数将更新属性/列。

def update_sp_file_property(
    ctx: ClientContext, filepath: str, properties: dict
) -> None:
    """
    Update sharepoint files customn properties (columns)

    Args:
        ctx: ClientContext
        filepath: serverRelativePath to file
        properties: dict of properties to update
    Returns:
        None
    """

    # Create query for property update
    for k, v in properties.items():
        updates = (
            ctx.web.get_file_by_server_relative_path(filepath)
            .listItemAllFields.set_property(name=k, value=v, persist_changes=True)
            .update()
            .execute_query()
        )
        print(f'Updated {os.path.basename(filepath)} property: "{k}" : "{v}"')

    # Send query
    updates.execute_query()

运行如下:

# Define parameters
client_id = cfg["SHAREPOINT"]["CLIENT_ID"]
client_secret = cfg["SHAREPOINT"]["CLIENT_SECRET"]
base_url = cfg["SHAREPOINT"]["BASE_URL"]

Create sharepoint context for auth
ctx = sp.get_client_context(
        client_id=client_id, client_secret=client_secret, base_url=base_url
    )
filepath = "/sites/<sharepoint_site_name>/Shared Documents/General/path_to_file"
properties = {
    "property1": "value1",
    "property2": "value2",
  }

# Run fucntion
update_sp_file_property(ctx=ctx, filepath=filepath, properties=properties)
© www.soinside.com 2019 - 2024. All rights reserved.