c# - 在 SP List oDat.Type 中为 People 属性创建项目

标签 c# microsoft-graph-api

我需要创建一个带有 Person/People 字段的项目。

我能够填充标题、日期时间、文本字段等字段,如下所示,但我根本无法填充人员字段。

我已尝试发送显示名称、用户 ID 和 UPN。

var fieldValueSet = new FieldValueSet();
fieldValueSet.AdditionalData = new Dictionary<string, object>();
fieldValueSet.AdditionalData.Add("Title@odata.type", "Edm.String");
fieldValueSet.AdditionalData.Add("Title", task.Title);
fieldValueSet.AdditionalData.Add("Plan@odata.type", "Edm.String");
fieldValueSet.AdditionalData.Add("Plan", plan.Title);
fieldValueSet.AdditionalData.Add("Assigned@odata.type", "Edm.String");
fieldValueSet.AdditionalData.Add("Assigned", assignee.UserPrincipalName);
fieldValueSet.AdditionalData.Add("DueDate@odata.type", "Edm.String");
fieldValueSet.AdditionalData.Add("DueDate", task.DueDateTime);

var listItem = new ListItem
{
   Fields = fieldValueSet
};

await graphServiceClient.Sites["SPURL:/sites/ITOddeleni:"].Lists["ListName"].Items
                                    .Request()
                                    .AddAsync(listItem)

最佳答案

您的用户主体名称看起来像电子邮件吗? (例如 some.person@yourtenant.onmicrosoft.com)。如果是这种情况,那么您应该尝试将其转换为共享点声明版本:i:0#.f|membership|some.dude@yourtenant.onmicrosoft.com

如果您的应用程序中有经过身份验证的 ClientContext,则可以通过调用 SharePoint utility 来解析用户名:
Utility.ResolvePrincipal(context, context.Web, userEmail, PrincipalType.User, PrincipalSource.All, null, true); 如果您没有上下文,那么您可以尝试组合字符串(不是那么健壮,但应该也能正常工作)。

This answer建议您尝试为该字段设置 LookupId 值(与使用 SharePoint 的 REST api 设置用户值的方式相同):

var assigneeLookupId = // Get the assignee's user id on the site (wssid)
fieldValueSet.AdditionalData.Add("AssignedLookupId@odata.type", "Edm.Int32");
fieldValueSet.AdditionalData.Add("AssignedLookupId", assigneeLookupId);

以下模式允许您插入多个人的值: 解决方案 在评论的帮助下,我得到了这个有效的答案

{
    "AssignedLookupId@odata.type" : "Collection(Edm.Int32)"
    "AssignedLookupId": [1,2,3]
}

此方法符合 MS Graph 返回查找字段值的方式。

关于c# - 在 SP List oDat.Type 中为 People 属性创建项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57111540/

相关文章:

c# - 方向基于 2 个纬度、经度点

c# - 使用 Microsoft Graph REST API 筛选事件消息

office365 - Microsoft Graph 列表组成员 OData 查询参数错误

sharepoint - Sharepoint REST API 和 Microsoft Graph Sharepoint API 之间的区别

javascript - Unity3D - 错误提示意外符号 `extends'

c# - 为什么要使用 .NET 编译器的 Debug模式?

c# - 获取实际进程 RAM 使用情况

c# - 我怎样才能直接从我的特定于应用程序的异常映射到 Http 异常?

python - 从azure Rest api上传证书失败

Azure AD B2C 获取具有自定义属性的用户