【问题标题】:MS Graph cycling through each item in IListItemsCollectionPageMS Graph 循环遍历 IListItemsCollectionPage 中的每个项目
【发布时间】:2019-06-11 12:37:34
【问题描述】:

我有一段代码,它从 SPO 列表中提取项目。我应该如何编写 foreach / select 类型才能读取 item.Title f.e?

尝试将 var 项键入多种类型,但无法获得正确的结果...

var items = await graphServiceClient.Sites["yourtenant.sharepoint.com:/sites/ITOddeleni:"].Lists["TeamsRequest"].Items
     .Request(queryOptions)
     .GetAsync();

foreach (var item in items)
{
    Console.WriteLine(item);
}

我希望它写出一个带有项目标题的字符串。 我现在得到的只是 2x Microsoft.Graph.ListItem(因为我在列表中有 2 个项目)

【问题讨论】:

  • 你读到的是 ToString() 方法的输出,它被 C# 中的每个对象从类 object 继承。如果没有被覆盖,它将默认打印整个类名(就像你的情况一样)。您需要手动打印所有您希望读取的值
  • 使它成为 Console.WriteLine(item.ToString());没有改变任何事情,我误会了吗?
  • 是的,你需要写Console.WriteLine($"Title: {item.Title}") 或类似的东西。不知道item是什么数据类型......
  • Microsoft.Graph.ListItem 它是... 设法通过 Console.WriteLine($"Title: {item.Id}") 获取它的 ID,但它根本不建议 Title: -(
  • name 怎么样?看看properties in the documentation

标签: c# .net microsoft-graph-api


【解决方案1】:
var queryOptions = new List<QueryOption>()
                {
                    new QueryOption("expand", "fields(select=id,Title)")
                };



                var items = await graphServiceClient.Sites["yourtenant.sharepoint.com"].Lists["TeamsRequest"].Items
                     .Request(queryOptions)
                     .GetAsync();




                foreach (var item in items)
                {
                    Console.WriteLine(item.Fields.AdditionalData["Title"]);
                    Console.WriteLine("Name:" + item.Name);
                    Console.WriteLine($"ID: {item.Id}");
                }

item.Fields.AdditionalData 做这件事。感谢您的建议:-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 2016-03-24
    • 2018-02-13
    相关资源
    最近更新 更多