使用Microsoft搜索 API 搜索 OneDrive 和 SharePoint 内容

使用 Microsoft Graph 中的Microsoft搜索 API 搜索存储在 OneDrive 或 SharePoint 中的内容:文件、文件夹、列表、列表项或网站。

警告

测试版中的搜索 API 架构已更改。 搜索请求和响应中的某些属性已重命名或删除。 有关详细信息,请参阅 架构更改弃用警告。 本主题中的示例显示了最新的架构。

搜索 API 允许通过在 searchRequest 上指定 entityTypes 属性来限定在 OneDrive 或 SharePoint 中检索的内容类型。 本文介绍一些示例。

示例 1:搜索文件

请求

POST /search/query
Content-Type: application/json

{
  "requests": [
    {
      "entityTypes": [
        "driveItem"
      ],
      "query": {
        "queryString": "contoso"
      }
    }
  ]
}

响应

HTTP/1.1 200 OK
Content-type: application/json

{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#search",
  "value": [
    {
      "searchTerms": [
        "contoso"
      ],
      "hitsContainers": [
        {
          "total": 1,
          "moreResultsAvailable": false,
          "hits": [
            {
              "hitId": "FlULeN/ui/1GjLx1rUfio5UAAEl",
              "rank": 1,
              "summary": "<c0>Contoso</c0> Detailed Design <ddd/>",
              "resource": {
                "@odata.type": "#microsoft.graph.driveItem",
                "createdDateTime": "2019-06-10T06:37:43Z",
                "lastModifiedDateTime": "2019-06-10T06:37:43Z",
                "name": "web_part_test_long Notebook",
                "webUrl": "https://contoso.sharepoint.com/sites/contoso-team/contoso-designs.docx",
                "createdBy": {
                 "user": {
                   "displayName": "Michaelvincent Santos;Provisioning User"
                  }
                },
                "lastModifiedBy": {
                  "user": {
                    "displayName": "Richard Mayer"
                  }
                },
                "parentReference": {
                  "siteId": "m365x231305.sharepoint.com,5724d91f-650c-4810-83cc-61a8818917d6,c3ba25dc-2c9f-48cb-83be-74cdf68ea5a0",
                  "driveId": "da61a2b0-4120-4a3f-812b-0fc0d79bf16b",
                  "sharepointIds": {
                      "listId": "c61d1892-ca82-4f53-b16f-6bb8a379e2b2",
                      "listItemId": "1027",
                      "listItemUniqueId": "E320AFEB-AD73-46A2-83D7-985FAA4B206D"
                  }
                },
                "fileSystemInfo": {
                  "createdDateTime": "2019-06-10T06:37:43Z",
                  "lastModifiedDateTime": "2019-06-10T06:37:43Z"
                }
              }
            }
          ]
        }
      ]
    }
  ]
}

示例 2:搜索列表项

请求

POST /search/query
Content-Type: application/json

{
  "requests": [
    {
      "entityTypes": [
        "listItem"
      ],
      "query": {
        "queryString": "contoso"
      }
    }
  ]
}

响应

HTTP/1.1 200 OK
Content-type: application/json

{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#search",
  "value": [
    {
      "searchTerms": [
        "contoso"
      ],
      "hitsContainers": [
        {
          "total": 1,
          "moreResultsAvailable": false,
          "hits": [
            {
              "hitId": "FlULeN/ui/1GjLx1rUfio5UAAEl",
              "rank": 1,
              "summary": "",
              "resource": {
                "@odata.type": "#microsoft.graph.listItem",
                "createdDateTime": "2019-06-10T06:37:43Z",
                "lastModifiedDateTime": "2019-06-10T06:37:43Z",
                "name": "web_part_test_long Notebook",
                "webUrl": "https://contoso.sharepoint.com/sites/contoso-team/Lists/Issue tracker list/DispForm.aspx?ID=1",
                "sharepointIds": {
                    "listId": "33498de0-d695-4d23-ac26-e1bf95a3206e",
                    "listItemId": "13"
                },
                "createdBy": {
                 "user": {
                   "displayName": "Michaelvincent Santos;Provisioning User"
                  }
                },
                "lastModifiedBy": {
                  "user": {
                    "displayName": "Richard Mayer"
                  }
                },
                "parentReference": {
                  "sharepointIds":{
                    "listId":"da61a2b0-4120-4a3f-812b-0fc0d79bf16b"  
                  },
                  "siteId": "m365x231305.sharepoint.com,5724d91f-650c-4810-83cc-61a8818917d6,c3ba25dc-2c9f-48cb-83be-74cdf68ea5a0"
                }
              }
            }
          ]
        }
      ]
    }
  ]
}

示例 3:搜索网站

请求

POST /search/query
Content-Type: application/json

{
  "requests": [
    {
      "entityTypes": [
        "site"
      ],
      "query": {
        "queryString": "contoso"
      }
    }
  ]
}

响应

HTTP/1.1 200 OK
Content-type: application/json

{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#search",
  "value": [
    {
      "searchTerms": [
        "contoso"
      ],
      "hitsContainers": [
        {
          "total": 1,
          "moreResultsAvailable": false,
          "hits": [
            {
              "hitId": "contoso.sharepoint.com,6598ee0b-0f5f-4416-a0ae-66d864efb43a,60024ce8-e74d-4d63-a939-ad00cd738670",
              "rank": 1,
              "summary": "",
              "resource": {
                "@odata.type": "#microsoft.graph.site",
                "id": "contoso.sharepoint.com,6598ee0b-0f5f-4416-a0ae-66d864efb43a,60024ce8-e74d-4d63-a939-ad00cd738670",
                "createdDateTime": "2019-06-10T06:37:43Z",
                "description": "Contoso Communication Site",
                "lastModifiedDateTime": "2020-08-30T06:41:56Z",
                "webUrl": "https://contoso.sharepoint.com/sites/contoso-team/"
              }
            }
          ]
        }
      ]
    }
  ]
}

示例 4:在 OneDrive 和 SharePoint 中搜索所有内容

此示例查询已登录用户有权读取的 OneDrive 和 SharePoint 网站中的所有内容。 响应中的 资源 属性返回作为 driveItem 对象的文件和文件夹的匹配项、作为容器 (SharePoint 列表) 为 列表的匹配项,以及作为 listItem 的所有其他匹配项。

请求

POST /search/query
Content-Type: application/json

{
  "requests": [
    {
      "entityTypes": [
        "driveItem", "listItem", "list"
      ],
      "query": {
        "queryString": "contoso"
      }
    }
  ]
}

响应

HTTP/1.1 200 OK
Content-type: application/json

{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#search",
  "value": [
    {
      "searchTerms": [
        "contoso"
      ],
      "hitsContainers": [
        {
          "total": 1,
          "moreResultsAvailable": false,
          "hits": [
            {
              "@odata.type": "#microsoft.graph.searchHitsContainer",
              "hitId": "FlULeN/ui/1GjLx1rUfio5UAAEl",
              "rank": 1,
              "summary": "<c0>Contoso</c0> Detailed Design <ddd/>",
              "resource": {
                "@odata.type": "#microsoft.graph.driveItem",
                "createdDateTime": "2019-06-10T06:37:43Z",
                "lastModifiedDateTime": "2019-06-10T06:37:43Z",
                "name": "web_part_test_long Notebook",
                "webUrl": "https://contoso.sharepoint.com/sites/contoso-team/contoso-designs.docx",
                "createdBy": {
                 "user": {
                   "displayName": "Michaelvincent Santos;Provisioning User"
                  }
                },
                "lastModifiedBy": {
                  "user": {
                    "displayName": "Richard Mayer"
                  }
                },
                "parentReference": {
                  "siteId": "m365x231305.sharepoint.com,5724d91f-650c-4810-83cc-61a8818917d6,c3ba25dc-2c9f-48cb-83be-74cdf68ea5a0",
                  "driveId": "da61a2b0-4120-4a3f-812b-0fc0d79bf16b",
                  "sharepointIds": {
                      "listId": "c61d1892-ca82-4f53-b16f-6bb8a379e2b2",
                      "listItemId": "1027",
                      "listItemUniqueId": "E320AFEB-AD73-46A2-83D7-985FAA4B206D"
                  }
                },
                "fileSystemInfo": {
                  "createdDateTime": "2019-06-10T06:37:43Z",
                  "lastModifiedDateTime": "2019-06-10T06:37:43Z"
                }
              }
            },
            {
              "@odata.type": "#microsoft.graph.searchHit",
              "hitId": "51eef59e-5d49-4d28-96f0-864cf90765e0",
              "rank": 2,
              "summary": "",
              "resource": {
                "@odata.type": "#microsoft.graph.list",
                "displayName": "Contoso - Documents",
                "id": "51eef59e-5d49-4d28-96f0-864cf90765e0",
                "description": "",
                "lastModifiedDateTime": "2020-07-08T18:17:59+00:00",
                "name": "Shared Documents",
                "parentReference": {
                  "siteId": "microsoft.sharepoint-df.com,220fd155-0ea2-477c-a816-5c08fdc45f5d,fad16ab6-0736-4fbc-a053-087296b47c99"
                },
                "webUrl": "https://microsoft.sharepoint-df.com/teams/spoppe/collab/TaskBoard/Contoso/Shared Documents/Forms/AllItems.aspx"
              }
            }
          ]
        }
      ]
    }
  ]
}

示例 5:在搜索查询中使用筛选器

可以在 OneDrive 和 SharePoint 的查询搜索词中使用 KQL。 例如:

  • "query": "contoso filetype:docx OR filetype:doc" 将查询范围限定为 Word 文档。
  • "query": "test path:\"https://contoso.sharepoint.com/sites/Team Site/Documents/Project\"" 将查询范围限定为网站中的特定文件夹。
  • "query": "contoso AND isDocument=true" 将查询范围限定为仅返回文档。 不会返回任何容器 (文件夹、文档库) 。
  • "query": "contoso contentclass:STS_List_Events" 将查询范围限定为 SharePoint 中存储的日历事件。
  • "query": "contoso (LastModifiedTime > 2021-02-01 AND Created > 2021-02-01)" 将查询范围限定为按日期筛选 SharePoint 和 OneDrive 项。

为了有效,属性限制应在 条件中指定有效的可查询托管属性名称。

示例 6:指定选择属性

可以在响应中指定想要返回的字段,作为 listItem 中的字段子属性的一部分,或者在响应中 searchHit 对象的 driveItem 中指定内部 listItem 子属性。 这是一种通过网络剪裁响应或请求一些不属于现成架构的特定属性的方法。

请注意,SharePoint 中自定义属性的属性选择仅适用于 listItemdriveItem ,因为这是 Microsoft Graph 中唯一支持自定义属性的两个 SharePoint 实体。

listItem 请求

POST /search/query
Content-Type: application/json

{
  "requests": [
    {
      "entityTypes": [
        "listItem"
      ],
      "query": {
        "queryString": "contoso"
      },
      "fields": [
          "title",
          "contentclass"
      ]
    }
  ]
}

listItem 响应

HTTP/1.1 200 OK
Content-type: application/json

{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#search",
  "value": [
    {
      "searchTerms": [
        "contoso"
      ],
      "hitsContainers": [
        {
          "total": 1,
          "moreResultsAvailable": false,
          "hits": [
            {
              "hitId": "contoso.sharepoint.com,6598ee0b-0f5f-4416-a0ae-66d864efb43a,60024ce8-e74d-4d63-a939-ad00cd738670",
              "rank": 1,
              "summary": "",
              "resource": {
                "@odata.type": "#microsoft.graph.listItem",
                "createdDateTime": "2019-06-10T06:37:43Z",
                "webUrl": "https://contoso.sharepoint.com/sites/contoso-team/contoso-designs.docx",
                "sharepointIds": {
                    "listId": "33498de0-d695-4d23-ac26-e1bf95a3206e",
                    "listItemId": "13"
                },
                "parentReference": {
                  "siteId": "m365x231305.sharepoint.com,5724d91f-650c-4810-83cc-61a8818917d6,c3ba25dc-2c9f-48cb-83be-74cdf68ea5a0"
                },
                "fields": {
                  "contentclass": "STS_ListItem_GenericList",
                  "title": "Contoso issue "
                }
              }
            }
          ]
        }
      ]
    }
  ]
}

driveItem 请求

POST /search/query
Content-Type: application/json

{
  "requests": [
    {
      "entityTypes": [
        "driveItem"
      ],
      "query": {
        "queryString": "contoso"
      },
      "fields": [
          "listId",
          "author",
          "title"
      ]
    }
  ]
}

driveItem 响应

POST /search/query
Content-Type: application/json

{
    "value": [
        {
            "searchTerms": [],
            "hitsContainers": [
                {
                    "hits": [
                        {
                            "hitId": "01YOWRGSD34TVVP25X7NAZAW3P2JRL7FWE",
                            "rank": 1,
                            "summary": "",
                            "resource": {
                                "@odata.type": "#microsoft.graph.driveItem",
                                "listItem": {
                                    "@odata.type": "#microsoft.graph.listItem",
                                    "fields": {
                                        "listId": "3b6a49d3-6bea-4549-bed8-8b1c92a12345",
                                        "author": "Robin",
                                        "title": "Test Notebook"
                                    },
                                    "id": "57ebe47b-b7eb-41fb-905b-123452bf96c4"
                                }
                            }
                        }
                    ],
                    "total": 371,
                    "moreResultsAvailable": true
                }
            ]
        }
    ],
    "@odata.context": "https://graph.microsoft.com/beta/$metadata#Collection(microsoft.graph.searchResponse)"
}

示例 7:搜索隐藏内容

使用 includeHiddenContent 属性可在搜索结果中包含隐藏内容,例如存档内容和 SharePoint Embedded。 默认情况下,此属性设置为 false,这可以防止返回隐藏的内容。

还可以选择性地包含 KQL,将隐藏内容的查询范围限定为特定内容类型。 例如,在 SharePoint 中,管理员可以将网站标记为已存档。 如果隐藏内容不可用,则搜索结果仅包含相关的非隐藏内容,前提是没有其他错误,并返回 200 OK 响应代码。

以下示例演示如何使用 queryTemplate 通过 KQL 和 includeHiddenContent 属性将查询范围限定为包含隐藏内容。 还可以使用 ContainerTypeId 等属性来限定对 SharePoint Embedded 内容的查询范围。 有关 SharePoint Embedded 中的容器类型的详细信息,请参阅 SharePoint Embedded 容器类型

请求

POST /search/query
Content-Type: application/json

{
    "requests": [
        {
            "entityTypes": [
                "driveItem"
            ],
            "query": {
                "queryString": "*",
                "queryTemplate": "({searchTerms} AuthorOWSUSER:TestContoso)"
            },
            "sharePointOneDriveOptions": {
                "includeHiddenContent": true
            }
        }
    ]
}

响应

HTTP/1.1 200 OK
Content-type: application/json

{
  "value": [
    {
      "searchTerms": [],
      "hitsContainers": [
        {
          "hits": [
            {
              "hitId": "fc78bcb9-8b26-4bba-a250-389def493e0f",
              "rank": 2,
              "summary": "<c0>STS</c0>_<c0>View</c0> <c0>MySiteDocumentLibrary</c0> <c0>domain</c0>_<c0>allow</c0>:<c0>ALL</c0><ddd/>",
              "resource": {
                "@odata.type": "#microsoft.graph.list",
                "displayName": "TestContoso - Documents",
                "id": "fc78bcb9-8b26-4bba-a250-389def493e0f",
                "createdBy": {
                  "user": {
                    "displayName": "System Account"
                  }
                },
                "lastModifiedDateTime": "2024-03-08T18:06:33Z",
                "name": "Documents",
                "parentReference": {
                  "siteId": "contoso-my.sharepoint.com,44776ebc-4ddc-4f7e-afb8-b706c77e0883,a118ff93-1105-40b9-bed0-2cd07cd4b2a4"
                },
                "webUrl": "https://contoso-my.sharepoint.com/personal/contoso_onmicrosoft_com/Documents/Forms/All.aspx"
              }
            }
          ],
          "total": 1,
          "moreResultsAvailable": false
        }
      ]
    }
  ],
  "@odata.context": "https://graph.microsoft.com/beta/$metadata#Collection(microsoft.graph.searchResponse)"
}

已知限制

  • 搜索 驱动器时,需要在 queryString 中包含文档库名称中包含的术语。 不支持查询 * ,并且不会返回所有可用的驱动器。

  • 搜索 API 不支持站点级 搜索架构。 使用租户级或默认 搜索架构

  • includeHiddenContent 属性仅适用于具有委派权限的方案。 它不适用于应用程序权限,其中 includeHiddenContent 属性自动设置为 false

后续步骤