You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#中通过Graph SDK直接获取myJoinedTeams的DisplayName与Description

直接访问Microsoft Graph SDK中Team的DisplayName和Description属性的解决方案

你不需要依赖AdditionalData字典来获取字段,问题的核心是Graph SDK的Team实体类本身就内置了DisplayName和Description属性,只要确保请求配置正确、SDK版本适配,就能直接通过属性访问。

解决步骤:

  1. 确保使用最新稳定版的Microsoft Graph SDK
    打开NuGet包管理器,更新Microsoft.Graph到最新稳定版本,旧版本可能存在属性映射不完整的问题。

  2. 修改代码直接访问实体属性
    替换掉AdditionalData的取值方式,直接调用Team对象的对应属性即可。同时确保分页逻辑正确收集所有团队数据:

// 发起请求,指定需要的字段
IUserJoinedTeamsCollectionPage myJoinedTeams = await graphClient.Me.JoinedTeams
    .Request()
    .Select("Id,DisplayName,Description")
    .GetAsync();

List<Team> allTeams = new List<Team>();
if (myJoinedTeams != null)
{
    var currentPage = myJoinedTeams;
    // 遍历所有分页结果
    while (currentPage != null)
    {
        allTeams.AddRange(currentPage);
        // 获取下一页
        currentPage = await currentPage.NextPageRequest?.GetAsync();
    }

    // 转换为自定义MyTeam对象
    var resultSet = allTeams.Select(t => new MyTeam
    {
        Id = t.Id,
        DisplayName = t.DisplayName, // 直接访问属性
        Description = t.Description  // 直接访问属性
    }).ToList();
}

针对Microsoft Graph SDK v5.x的写法(最新版本)

如果你的项目使用的是v5.x版本的SDK,写法略有不同,核心逻辑一致:

// 配置请求并获取第一页数据
var myJoinedTeams = await graphClient.Me.JoinedTeams.GetAsync(options =>
{
    options.QueryParameters.Select = new[] { "Id", "DisplayName", "Description" };
});

List<Team> allTeams = new List<Team>();
var currentPage = myJoinedTeams;
while (currentPage != null)
{
    allTeams.AddRange(currentPage.Value);
    // 检查是否有下一页并获取
    if (!string.IsNullOrEmpty(currentPage.OdataNextLink))
    {
        currentPage = await graphClient.Me.JoinedTeams.WithUrl(currentPage.OdataNextLink).GetAsync();
    }
    else
    {
        currentPage = null;
    }
}

// 转换为自定义对象
var resultSet = allTeams.Select(t => new MyTeam
{
    Id = t.Id,
    DisplayName = t.DisplayName,
    Description = t.Description
}).ToList();

原理说明

当你通过Select指定了DisplayName和Description字段后,Graph SDK会自动将API返回的JSON数据映射到Team实体类的对应属性上,无需手动从AdditionalData字典中提取——这个字典原本是用来存放SDK实体类未定义的额外字段的,正常情况下不需要用到它来访问标准属性。

内容的提问来源于stack exchange,提问作者DevÁsith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 19:52:45