使用MS Graph获取OneNote Section父级Notebook的displayName问题
解决方案
你需要修改API请求参数和实体类,就能直接从接口返回结果中拿到Section对应父Notebook的displayName,具体操作如下:
步骤1:修改API请求路由
调用MS Graph接口时添加$expand参数展开父笔记本属性,将原请求路由修改为:
public static readonly string apiSectionRoute = "https://graph.microsoft.com/v1.0/me/onenote/sections?$expand=parentNotebook";
步骤2:扩展实体类属性
在GenericEntityResponse类中新增父笔记本属性,用于接收反序列化的父级信息:
public class GenericEntityResponse : ApiBaseResponse { /// <summary> /// 实体名称 /// </summary> public string displayName; /// <summary> /// 实体的自链接 /// </summary> public string Self { get; set; } public List<GenericEntityResponse> Sections { get; set; } public List<GenericEntityResponse> SectionGroups { get; set; } // 新增:父级笔记本属性 public GenericEntityResponse ParentNotebook { get; set; } public override string ToString() { return "Name: " + displayName + ", Id: " + Id; } }
步骤3:获取父Notebook的displayName
你现有代码中apiBaseResponse里存储的实际是GenericEntityResponse类型的Section对象,直接类型转换后即可读取父笔记本名称:
// 你原有获取SectionId的逻辑 Globals.CurrentSectionsId=apiBaseResponse[0].Id.ToString(); // 新增:获取对应父Notebook的displayName if (apiBaseResponse[0] is GenericEntityResponse section) { string parentNotebookName = section.ParentNotebook?.displayName; // 这里直接使用parentNotebookName即可 }
补充说明
- MS Graph的sections接口默认不会返回关联的父级资源信息,
$expand参数是官方指定的关联资源展开方式,无需额外发起二次请求 - 如果需要同时获取父分区组信息,可将路由参数改为
?$expand=parentNotebook,parentSectionGroup即可同时拿到两级父级资源的所有属性
内容的提问来源于stack exchange,提问作者Trey Balut
相关产品推荐
相关产品推荐

