c# - SharePoint 列表项的 AddAsync 上无法识别字段

标签 c# microsoft-graph-api

当我尝试使用基本日历/列表字段创建新列表项时,一切都运行良好。但是,当我尝试使用“非标准”字段(即我添加的字段)执行此操作时,我收到“字段无法识别”错误。

领域显然就在那里!我需要使用某种特殊方法来填充这些自定义字段吗?

// get a specific list
ISiteListsCollectionPage list = await graphClient.Sites["root"].Lists.Request()
    .Filter($"DisplayName eq 'Outlook Integration'").GetAsync();

// create a dictionary of [calendar] list properties
Dictionary<string, object> props = new Dictionary<string, object>();

// populate properties, all of these work just fine
props.Add("Title", evt.Subject);
props.Add("Location", evt.Location?.DisplayName);
props.Add("EventDate", utcStart.ToString("yyyy-MM-ddTHH:mm:ssK"));
props.Add("EndDate", utcEnd.ToString("yyyy-MM-ddTHH:mm:ssK"));
props.Add("Description", Regex.Replace(evt.Body.Content, "<.*?>", String.Empty)); // remove HTML content

// populate custom properties
props.Add("ResourceID", evt.Id); // throws error: Field 'ResourceID' is not recognized

// create list item with our properties dictionary
var newItem = new ListItem
{
    Name = "My New Event",
    Fields = new FieldValueSet()
    {
        AdditionalData = props
    }
};

// call the service and get the result
var newListItem = await graphClient.Sites["root"].Lists[list[0].Id].Items.Request().AddAsync(newItem);

这是我的列表中字段的完整列表:

enter image description here

在这里您可以看到显示名称是“ResourceID”,而 API 名称是“O365EventId”。但是,两者都会导致相同的错误“无法识别字段。”

enter image description here

注意: ResourceID 是我添加的字段之一。如何通过 Graph API 设置该字段的值?

最佳答案

Marc 在关于列名称的评论中说得对,提供的屏幕截图显示 Column.displayName ,即

The user-facing name of the column.

但实际上 FieldValueSet.AdditionalData 期望作为键的是 Column.name ,它是:

The API-facing name of the column as it appears in the fields on a listItem. For the user-facing name, see displayName.

在您的情况下,displayNamename 属性很可能不同,您可以通过以下端点验证它: https://graph.microsoft.com/v1.0/sites/root/lists/Outlook Integration/columns

这就是发生此错误的原因。

通过 Graph API 客户端 (C#),您可以查看任何给定列表的所有列的列表,如下所示:

// get specific list by name
ISiteListsCollectionPage list = await graphClient.Sites["root"].Lists.Request()
    .Filter($"DisplayName eq 'YOUR_LIST_NAME_HERE'").GetAsync();

// get columns and output them to the log as a serialized object
var listColumns = await graphClient.Sites["root"].Lists[list[0].Id].Columns.Request().GetAsync();
logger.LogInformation($"List Columns Object: {JsonConvert.SerializeObject(listColumns).ToString()}");

关于c# - SharePoint 列表项的 AddAsync 上无法识别字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51293925/

相关文章:

c# - 实体类对象改变了它的值,尽管它没有改变

C# CLR/编译问题

azure - Microsoft Graph 处理按 ID 获取订阅的白色存储扩展时出错

c# - 如何将网络摄像头捕获的帧坐标投影到屏幕坐标

c# - 调整电子邮件中嵌入的图像的大小

c# - JSON 序列化器和 CSLA 对象

office365 - 由于字符无效,无法反序列化来自 Graph API 的错误响应

microsoft-graph-api - 随机开始得到400 "Invalid request"错误: "The provided encoding is not supported."

microsoft-graph-api - 在 Microsoft Graph API 中确定转发和回复的电子邮件

c# - Azure AD b2b "Read all users' 基本配置文件”权限