如何在Microsoft Graph API中组合Expand与Select获取DriveItem发布状态
错误原因
- 第一个报错:Microsoft Graph .NET SDK的
Select方法仅支持选择当前查询对象(此处为ListItem)的直接属性,不能直接选择嵌套对象(如DriveItem)的子属性,因此item => item.DriveItem.Publication的写法会触发参数校验错误。 - 第二个报错:你添加的
select=publication查询参数指定返回ListItem的publication属性,但ListItem本身不存在该属性,接口返回的异常JSON结构不符合SDK解析规则,因此抛出JSON解析错误。
正确实现代码
你需要在扩展DriveItem的同时,指定返回DriveItem的publication属性,同时保留原有所有DriveItem属性,推荐使用字符串形式的Expand参数,兼容性最好:
var directoryContents = await App.GraphClient.Sites[SiteIdShortName].Lists[sharedDocsDriveId] .Items .Request() // 扩展DriveItem时指定返回全部原有属性 + publication发布状态属性 .Expand("driveItem($select=*,publication)") .GetAsync(); SharedDocumentList.ItemsSource = directoryContents.CurrentPage.ToList();
如果你使用的是较新版本的Graph .NET SDK,也可以使用Lambda写法:
var directoryContents = await App.GraphClient.Sites[SiteIdShortName].Lists[sharedDocsDriveId] .Items .Request() .Expand(item => item.DriveItem.Select(driveItem => new { driveItem, driveItem.Publication })) .GetAsync(); SharedDocumentList.ItemsSource = directoryContents.CurrentPage.ToList();
验证方式
拿到查询结果后,通过ListItem.DriveItem.Publication即可访问发布状态信息,包含Level(发布级别)、Status(发布状态,如已发布、草稿、签出等)字段。
内容的提问来源于stack exchange,提问作者dot
相关产品推荐
相关产品推荐

