问题
I'm trying to add values to a custom column on a list item after uploading the list item to the list. I can get the item into the list, and I can query the list and get back the item's data, but when I try to add the data for the extra field I get the following Microsoft.SharePoint.Client.InvalidClientQueryException
error:
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.
I'm not sure what value or model the error message is referring to. This is my code:
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);
Originally when I was doing fieldVals.AdditionalData.Add()
I had "Phase" and a List(string) and that caused an error about the OData field type not being set but I haven't found anywhere in the documentation that says what expected OData field values are. I tried setting it to microsoft.graph.fieldValueSet
but that didn't work.
I'm trying to update a Choice column that allows multiple choices as checkboxes.
回答1:
For multi-choice field type, indeed, the presence of odata.type
annotation is mandatory in request payload, here is an example how to specify it:
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>"]
}
}
where
ChoiceFieldName
- choice field nameval1
,val2
- field values
Example
Assuming a List contains a choice field named Categories
, then the following example demonstrates how to update list item via 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 }
}
}
});
References
- Update an item in a list
- Entity Data Model: Primitive Data Types
来源:https://stackoverflow.com/questions/55251020/net-graph-sdk-updating-sharepoint-online-list-item-values