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

使用Microsoft Graph向任务列表添加人员/组字段失败问题求助

解决Microsoft Graph向SharePoint任务列表添加AssignedTo字段失败的问题

问题原因

你的代码对SharePoint Person/Group类型字段AssignedTo的赋值方式不符合Graph规范:

  • 错误指定AssignedTo@odata.type为Collection(Edm.String),SharePoint多值用户字段需使用Collection(SP.FieldUserValue)类型
  • 直接传入ID字符串数组,不符合Graph要求的字段值结构

针对Graph 4.54.0+的修复方案

修改requestBody中AssignedTo相关代码,按照SharePoint用户字段规范构造值:

修改后的核心代码片段

// 替换原lookupIds构造逻辑,生成符合要求的用户值数组
var assignedToValues = usersArray.Select(user => new { LookupId = user.LookupId }).ToArray();

var requestBody = new ListItem
{
    Fields = new FieldValueSet
    {
        AdditionalData = new Dictionary<string, object>
        {
            { 
                "Title", item.Fields.AdditionalData["StatusLookupId"] ?? "Not specified"
            },
            {
                "AssignedTo@odata.type", "Collection(SP.FieldUserValue)"
            },
            {
                "AssignedTo", assignedToValues
            },
            { 
                "Body", parseXMLDate.Description ?? "Not specified" 
            }
        }
    }
};

关键说明

  1. OData类型修正:将AssignedTo@odata.type改为Collection(SP.FieldUserValue),这是SharePoint多值用户字段在Graph中要求的标准类型
  2. 字段值格式:每个用户项需为包含LookupId的匿名对象(值为整数类型,不要转为字符串),Graph会通过该ID关联对应SharePoint用户
  3. ID有效性验证:确保user.LookupId是当前SharePoint站点内的有效用户ID,而非其他系统(如Azure AD、自定义系统)的ID

额外检查点

  • 确认应用已配置Sites.ReadWrite.All(或对应站点的精细读写权限)
  • 可通过以下代码验证LookupId的有效性:
    // 示例:验证用户ID是否存在于当前站点
    var validUser = await _graphClient.Sites[siteName].Users[user.LookupId.ToString()].Request().GetAsync();
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 14:55:15