You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Azure Function更新SharePoint文档的Editor/Author字段?

SharePoint文档库上传文件时无法修改Editor/Author字段的解决方案

问题背景

通过Azure Function向SharePoint文档库上传文件时,Editor(Modified By)和Author(Created By)字段始终被“SharePoint APP”用户覆盖,无法用传入函数的JSON指定用户替换这两个系统字段值,仅能正常修改自定义字段。

原尝试代码

private static void upload(FileCreationInformation file, string folderPath, ClientContext ctx, string type, string displayName, string contractManagerEmail)
{
    Folder library = ctx.Web.GetFolderByServerRelativeUrl(folderPath);
    Microsoft.SharePoint.Client.File uploadfile = library.Files.Add(file);
    ListItem metadata = uploadfile.ListItemAllFields;
    FieldUserValue contractManagerUserValue = FieldUserValue.FromUser(contractManagerEmail);
    //THIS IS A CUSTOM COLUMN    
    metadata["Modifiedby"] = contractManagerUserValue;
    metadata.UpdateOverwriteVersion();
    ctx.Load(uploadfile);
    ctx.ExecuteQuery();
    ListItem newMeta = uploadfile.ListItemAllFields;
    //FieldUserValue value = FieldUserValue.FromUser(contractManagerEmail);
    var user = ctx.Web.EnsureUser(contractManagerEmail);
    newMeta["Editor"] = user;
    newMeta["Author"] = user;
    newMeta.Update();
    ctx.ExecuteQuery();
}

使用的连接库

#r "Microsoft.SharePoint.Client.dll"
#r "Microsoft.SharePoint.Client.Runtime.dll"
#r "Newtonsoft.Json"
#r "PnP.Framework.dll"

解决方案

要修改Author和Editor这类系统默认字段,需调整更新逻辑并注意权限:

关键要点

  • 用SystemUpdate(false)替代Update():Update()会触发SharePoint自动更新系统字段,而SystemUpdate(false)可跳过此机制,保留手动设置的字段值。
  • 提前加载用户对象属性:设置字段前需确保目标用户的ID等属性已被加载,否则无法正确赋值。
  • 权限要求:执行操作的账号需具备修改列表项系统字段的权限(如网站集管理员、列表编辑权限)。

修改后的代码示例

private static void upload(FileCreationInformation file, string folderPath, ClientContext ctx, string type, string displayName, string contractManagerEmail)
{
    Folder library = ctx.Web.GetFolderByServerRelativeUrl(folderPath);
    Microsoft.SharePoint.Client.File uploadfile = library.Files.Add(file);
    ctx.Load(uploadfile);
    ctx.ExecuteQuery();

    // 获取并加载目标用户的必要属性
    var targetUser = ctx.Web.EnsureUser(contractManagerEmail);
    ctx.Load(targetUser, u => u.Id);
    ctx.ExecuteQuery();

    ListItem item = uploadfile.ListItemAllFields;
    // 设置自定义字段
    FieldUserValue contractManagerUserValue = FieldUserValue.FromUser(contractManagerEmail);
    item["Modifiedby"] = contractManagerUserValue;
    // 设置系统字段(直接赋值用户ID)
    item["Author"] = targetUser.Id;
    item["Editor"] = targetUser.Id;
    // 跳过系统自动更新,提交修改
    item.SystemUpdate(false);
    ctx.ExecuteQuery();
}

PnP Framework简化写法

若使用PnP Framework,可直接调用封装好的方法:

private static void upload(FileCreationInformation file, string folderPath, ClientContext ctx, string type, string displayName, string contractManagerEmail)
{
    Folder library = ctx.Web.GetFolderByServerRelativeUrl(folderPath);
    Microsoft.SharePoint.Client.File uploadfile = library.Files.Add(file);
    ctx.Load(uploadfile);
    ctx.ExecuteQuery();

    var targetUser = ctx.Web.EnsureUser(contractManagerEmail);
    ctx.Load(targetUser);
    ctx.ExecuteQuery();

    ListItem item = uploadfile.ListItemAllFields;
    // 设置自定义字段
    FieldUserValue contractManagerUserValue = FieldUserValue.FromUser(contractManagerEmail);
    item["Modifiedby"] = contractManagerUserValue;
    // PnP内置方法设置Author和Editor
    item.SetAuthorEditor(targetUser.Id, targetUser.Id);
    item.SystemUpdate(false);
    ctx.ExecuteQuery();
}

内容的提问来源于stack exchange,提问作者Pasq

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 15:32:12