Skip to content

Latest commit

 

History

History

README.md

Searching SharePoint

Use the SharePoint search REST API to find sites, documents, and list items across the tenant. Queries use Keyword Query Language (KQL) with property filters, refiners, and sort orders.


Prerequisites

Requirement Description Reference
Read access to the content being searched Search results respect item-level permissions. Users only see what they can access. SharePoint admin roles

How search works

flowchart LR
    Query["KQL Query\n(report.docx ContentType:document)"]
    SearchAPI["SharePoint Search\nREST API"]
    Results["Results\n(filtered by permissions)"]
    Refinement["Refiners /\nPagination"]

    Query --> SearchAPI
    SearchAPI --> Results
    SearchAPI --> Refinement
    Refinement --> Query
Loading

Search queries are sent to the /_api/search/query endpoint as KQL text. Results respect the requesting user's permissions.

Common KQL filters

Filter Example Description
IsDocument:1 project report IsDocument:1 Files only
contentclass:STS_Site contentclass:STS_Site Sites only
Path: Path:https://... Restrict scope
ContentType: ContentType:invoice Filter by content type
Author: Author:"Vadim G" Filter by author
LastModifiedTime: LastModifiedTime>2026-01-01 Date range

Examples

Step Operation File Required role API reference
1 Keyword search query_keyword.py Read access to content Search REST API
2 Search documents (IsDocument) query_documents.py Read access to libraries Search REST API
3 Search sites (contentclass) query_sites.py Read access to sites Search REST API
4 Search by site (Path:) query_by_site.py Read access to content Search REST API
5 Search by content type query_by_content_type.py Read access to content Search REST API
6 Filter by author / date range query_with_filter.py Read access to content Search REST API
7 Sort by managed property query_with_sort.py Read access to content Search REST API
8 Refinement / faceted drill-down query_with_refinement.py Read access to content Search REST API
9 Paginate through results query_paged.py Read access to content Search REST API
10 Export search reports export_reports.py Search admin Search REST API

Quick start

from office365.sharepoint.client_context import ClientContext

ctx = ClientContext("https://contoso.sharepoint.com/sites/team").with_client_secret(
    "contoso.onmicrosoft.com", "client_id", "client_secret"
)

# Search for documents
result = ctx.search.post_query("IsDocument:1", row_limit=10).execute_query()
rows = result.value.PrimaryQueryResult.RelevantResults.Table.Rows
for row in rows:
    print(f"  {row.Cells['Path']}")

API reference