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

关于在SharePoint Graph API中用contains/endswith过滤文件名的技术咨询

SharePoint列表项Graph API查询:文件名过滤(endswith/contains失效问题)

我一直在找通过Microsoft Graph API查询SharePoint列表项时按文件名过滤的方法。目前用startswith(fields/FileLeafRef, 'query')能正常过滤,但想使用endswith()或contains()时却无法生效。

请求示例

[
    {
        "id": "123",
        "method": "GET",
        "url": "/sites/{site}/lists/{list1}/items?$top=10&expand=fields(select=Dokumenttyp,Author,Editor,Customer_ID,Project_ID,LinkFilename)&$select=id,name,webUrl,createdDateTime,lastModifiedDateTime,fields&$filter=fields/ContentType ne 'Folder' and fields/Customer_ID eq '162373' and startswith(fields/FileLeafRef, 'query')",
        "headers": {
            "Prefer": "HonorNonIndexedQueriesWarningMayFailRandomly"
        }
    },
    {
        "id": "456",
        "method": "GET",
        "url": "/sites/{site}/lists/{list2}/items?$top=10&expand=fields(select=Dokumenttyp,Author,Editor,Customer_ID,Project_ID,LinkFilename)&$select=id,name,webUrl,createdDateTime,lastModifiedDateTime,fields&$filter=fields/ContentType ne 'Folder' and fields/Customer_ID eq '162373' and startswith(fields/FileLeafRef, 'query')",
        "headers": {
            "Prefer": "HonorNonIndexedQueriesWarningMayFailRandomly"
        }
    },
    {
        "id": "789",
        "method": "GET",
        "url": "/sites/{site}/lists/{list3}/items?$top=10&expand=fields(select=Dokumenttyp,Author,Editor,Customer_ID,Project_ID,LinkFilename)&$select=id,name,webUrl,createdDateTime,lastModifiedDateTime,fields&$filter=fields/ContentType ne 'Folder' and fields/Customer_ID eq '162373' and startswith(fields/FileLeafRef, 'query')",
        "headers": {
            "Prefer": "HonorNonIndexedQueriesWarningMayFailRandomly"
        }
    }
]

startswith()能正常工作,但不想限制用户只能前缀搜索文件名。contains()和endswith()属于OData高级查询功能,但在这个端点似乎无效。另外,既然startswith()是已索引的,为什么contains()或endswith()没有被索引?能不能手动创建索引来支持这两个函数?还有,用/search端点不太理想,因为速度慢很多,而且我们需要同时按Project_ID、Customer_ID等元数据过滤。


问题原因与解决办法

  1. OData函数支持限制
    SharePoint列表的Graph API端点对OData查询函数有严格限制:startswith()属于基础索引支持的函数,而contains()、endswith()需要后端支持全文索引或特定字段配置,默认情况下FileLeafRef(文件名)字段没有开启这些支持。

  2. 手动创建索引的可能性
    无法直接为FileLeafRef手动创建支持contains()/endswith()的索引,因为这个字段是SharePoint内置的系统字段,不允许用户自定义其索引类型。系统字段的索引规则由SharePoint平台预设,仅支持前缀匹配类的查询。

  3. 替代方案

  • 使用计算字段+全文索引:
    创建一个新的计算字段,将FileLeafRef的值复制到这个字段中,然后为该计算字段开启全文索引。之后就可以在Graph API查询中用contains()/endswith()过滤这个计算字段。注意计算字段需要设置为“存储值”,否则无法被索引。
  • 前端二次过滤:
    如果数据量较小,可以先用startswith()获取结果,再在前端对文件名做后缀或包含匹配的二次过滤,这种方法适合小规模列表场景。
  • 优化/search端点使用:
    虽然/search速度偏慢,但可以通过添加filter参数结合Customer_ID、Project_ID等元数据条件缩小搜索范围,同时指定select字段减少返回数据量,一定程度上提升性能。

内容的提问来源于stack exchange,提问作者Magnus

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 00:38:18