.NET Graph SDK更新SharePoint在线列表项值 [英] .NET Graph SDK Updating Sharepoint Online List Item Values

查看:90
本文介绍了.NET Graph SDK更新SharePoint在线列表项值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将列表项上载到列表后,我试图将值添加到列表项上的自定义列.我可以将项目添加到列表中,并且可以查询列表并获取项目的数据,但是当我尝试为多余的字段添加数据时,出现以下Microsoft.SharePoint.Client.InvalidClientQueryException错误:

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);

最初,当我在执行fieldVals.AdditionalData.Add()时,我有阶段"和一个List(string),这导致有关未设置OData字段类型的错误,但是我在文档中找不到任何表示OData预期内容的地方字段值是.我尝试将其设置为microsoft.graph.fieldValueSet,但这没有用.

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.

推荐答案

对于多选字段类型,实际上注释的存在是强制性的请求有效负载,这是一个如何指定有效负载的示例:

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>"]
  }
}

其中

  • ChoiceFieldName-选择字段名称
  • val1val2-字段值
  • ChoiceFieldName - choice field name
  • val1, val2 - field values

示例

假设列表包含名为Categories的选择字段,则以下示例演示如何通过 msgraph-sdk-dotnet :

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 }
            }
      }
 }); 

参考

  • Update an item in a list
  • Entity Data Model: Primitive Data Types

这篇关于.NET Graph SDK更新SharePoint在线列表项值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆