.NET Graph SDK 更新 Sharepoint Online 列表项值
.NET Graph SDK Updating Sharepoint Online List Item Values
我试图在将列表项上传到列表后向列表项的自定义列添加值。我可以将项目放入列表中,我可以查询列表并取回项目的数据,但是当我尝试为额外字段添加数据时,出现以下 Microsoft.SharePoint.Client.InvalidClientQueryException
错误:
A value without a type name was found and no expected type is available.
When the model is specified, each value in the payload must have a type which can be either
specified in the payload, explicitly by the caller or implicitly inferred from the parent value.
我不确定错误消息指的是什么值或模型。这是我的代码:
var item = await graphClient
.Drives[driveId]
.Root.ItemWithPath(fileName)
.ListItem.Request()
.Select("WebURL,Fields,SharepointIds")
.Expand("Fields")
.GetAsync();
var fieldVals = await graphClient
.Sites[SPUrl + ":"]
.Sites[SpPath + ":"]
.Lists[libId]
.Items[item.SharepointIds.ListItemId]
.Fields
.Request()
.GetAsync();
fieldVals.AdditionalData.Add("Phase",
JsonConvert.SerializeObject(tags));
await graphClient
.Drives[driveId]
.Root
.ItemWithPath(fileName)
.ListItem
.Fields
.Request()
.UpdateAsync(fieldVals);
最初,当我在做 fieldVals.AdditionalData.Add()
时,我有 "Phase" 和一个 List(string),这导致了关于未设置 OData 字段类型的错误,但我没有在说明预期 OData 字段值是什么的文档。我尝试将其设置为 microsoft.graph.fieldValueSet
,但没有用。
我正在尝试更新允许多个选项作为复选框的选择列。
对于多选字段类型,确实,odata.type
注释的存在在请求负载中是强制,这里是如何指定它的示例:
PATCH https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/items/{item-id}/
{
"fields": {
"<ChoiceFieldName>@odata.type": "Collection(Edm.String)",
"<ChoiceFieldName>":["<val1>","<val2>"]
}
}
哪里
ChoiceFieldName
- 选择字段名称
val1
、val2
- 字段值
例子
假设一个列表包含一个名为Categories
的选择字段,那么下面的例子演示了如何通过msgraph-sdk-dotnet
更新列表项:
var choiceVals = new []{ "Cat1", "Cat2"};
await graphClient.Sites[siteId].Lists[listId].Items[itemId].Request().UpdateAsync(new ListItem()
{
Fields = new FieldValueSet
{
AdditionalData = new Dictionary<string, object>
{
{ "Categories@odata.type", "Collection(Edm.String)" },
{ "Categories", choiceVals }
}
}
});
参考资料
我试图在将列表项上传到列表后向列表项的自定义列添加值。我可以将项目放入列表中,我可以查询列表并取回项目的数据,但是当我尝试为额外字段添加数据时,出现以下 Microsoft.SharePoint.Client.InvalidClientQueryException
错误:
A value without a type name was found and no expected type is available. When the model is specified, each value in the payload must have a type which can be either specified in the payload, explicitly by the caller or implicitly inferred from the parent value.
我不确定错误消息指的是什么值或模型。这是我的代码:
var item = await graphClient
.Drives[driveId]
.Root.ItemWithPath(fileName)
.ListItem.Request()
.Select("WebURL,Fields,SharepointIds")
.Expand("Fields")
.GetAsync();
var fieldVals = await graphClient
.Sites[SPUrl + ":"]
.Sites[SpPath + ":"]
.Lists[libId]
.Items[item.SharepointIds.ListItemId]
.Fields
.Request()
.GetAsync();
fieldVals.AdditionalData.Add("Phase",
JsonConvert.SerializeObject(tags));
await graphClient
.Drives[driveId]
.Root
.ItemWithPath(fileName)
.ListItem
.Fields
.Request()
.UpdateAsync(fieldVals);
最初,当我在做 fieldVals.AdditionalData.Add()
时,我有 "Phase" 和一个 List(string),这导致了关于未设置 OData 字段类型的错误,但我没有在说明预期 OData 字段值是什么的文档。我尝试将其设置为 microsoft.graph.fieldValueSet
,但没有用。
我正在尝试更新允许多个选项作为复选框的选择列。
对于多选字段类型,确实,odata.type
注释的存在在请求负载中是强制,这里是如何指定它的示例:
PATCH https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/items/{item-id}/
{
"fields": {
"<ChoiceFieldName>@odata.type": "Collection(Edm.String)",
"<ChoiceFieldName>":["<val1>","<val2>"]
}
}
哪里
ChoiceFieldName
- 选择字段名称val1
、val2
- 字段值
例子
假设一个列表包含一个名为Categories
的选择字段,那么下面的例子演示了如何通过msgraph-sdk-dotnet
更新列表项:
var choiceVals = new []{ "Cat1", "Cat2"};
await graphClient.Sites[siteId].Lists[listId].Items[itemId].Request().UpdateAsync(new ListItem()
{
Fields = new FieldValueSet
{
AdditionalData = new Dictionary<string, object>
{
{ "Categories@odata.type", "Collection(Edm.String)" },
{ "Categories", choiceVals }
}
}
});
参考资料