使用Microsoft Graph API获取SharePoint中最新指定前缀CSV文件
原请求的问题分析
你的请求未实现排序,核心原因有两个:
- URL路径错误:错误将文档库名称
Shared Documents嵌套进站点路径,正确结构需先指定站点,再关联对应的列表/文档库。 - OData参数格式错误:
orderby缺少$前缀,Graph API的OData查询参数(如$orderby、$filter)必须以$开头,否则无法被识别。
修正后的List Items API请求
使用List Items API可精准获取所需字段并仅返回最新文件:
GET https://graph.microsoft.com/v1.0/sites/company.sharepoint.com/sites/{你的站点名称}/lists/{列表ID}/items?$select=webUrl&$expand=fields($select=LinkFilename,Datatype,Modified)&$filter=startswith(LinkFilename,'ABC_')&$orderby=Modified desc&$top=1
参数说明:
$select=webUrl:直接获取文件的网页访问地址$expand=fields(...):展开自定义字段集合,筛选出LinkFilename、Datatype和Modified$filter=startswith(LinkFilename,'ABC_'):过滤文件名以ABC_开头的文件$orderby=Modified desc:按修改时间降序排序,确保最新文件排在首位$top=1:仅返回排序后的第一条数据(即最新文件)
替代方案:使用Drive API
文档库本质是SharePoint的Drive,用Drive API操作文件更直观,也能获取自定义字段:
GET https://graph.microsoft.com/v1.0/sites/company.sharepoint.com/sites/{你的站点名称}/drive/root/children?$select=name,webUrl,lastModifiedDateTime&$expand=listItem($select=fields/Datatype)&$filter=startswith(name,'ABC_')&$orderby=lastModifiedDateTime desc&$top=1
参数说明:
name对应List Items中的LinkFilename,lastModifiedDateTime对应Modified$expand=listItem(...):关联文件对应的列表项,获取自定义的Datatype字段- 其余参数作用与List Items API一致,实现筛选、排序并获取最新文件
内容的提问来源于stack exchange,提问作者Data Rish
相关产品推荐
相关产品推荐

