Microsoft SharePoint
haystack_integrations.components.fetchers.microsoft_sharepoint.fetcher
MSSharePointFetcher
Fetches the full content of Microsoft SharePoint and OneDrive items via the Microsoft Graph API.
The fetcher complements MSSharePointRetriever, which only returns Search snippets and metadata. Wire the
retriever's documents (or a list of web_urls) into this fetcher to download the full content. It
dispatches on the entity type of each hit and always returns ByteStreams, ready for a downstream converter
(for example a FileTypeRouter in front of PyPDFToDocument, DOCXToDocument, HTMLToDocument, or a JSON
converter):
- Files (
driveItem) are downloaded as their raw bytes (PDF, DOCX, ...). - List items (
listItem) are returned as a JSONByteStreamof the item's column values (fields). - SharePoint pages (
sitePage) are returned as an HTMLByteStreambuilt from the page's web parts.
Each ByteStream's meta carries url, file_name, content_type, and a normalized entity_type
(driveItem, listItem, or sitePage).
Everything is resolved through the Microsoft Graph shares endpoint (plus the Pages API for pages), so only
the web_url already exposed by the retriever is needed. The fetcher takes a per-user access_token as a run
input, typically wired from an upstream OAuthTokenResolver. The token must carry delegated Microsoft Graph
permissions (for example Files.Read.All for files and Sites.Read.All for list items and pages).
Usage example
from haystack_integrations.components.fetchers.microsoft_sharepoint import MSSharePointFetcher
fetcher = MSSharePointFetcher()
# `access_token` is a per-user delegated Microsoft Graph bearer token.
result = fetcher.run(
access_token="my-delegated-graph-token",
targets=["https://contoso.sharepoint.com/sites/contoso-team/contoso-designs.docx"],
)
streams = result["streams"]
In a pipeline, connect MSSharePointRetriever.documents to the fetcher's targets input and an upstream
component that emits a per-user access_token to the fetcher's access_token input.
init
__init__(
*,
graph_url: str = DEFAULT_GRAPH_URL,
timeout: float = 30.0,
max_retries: int = 3,
max_concurrent_requests: int = 5,
raise_on_failure: bool = True
) -> None
Initialize the fetcher.
Parameters:
- graph_url (
str) – The Microsoft Graph base URL. Defaults tohttps://graph.microsoft.com/v1.0. Override for sovereign clouds. - timeout (
float) – The HTTP timeout in seconds for each request to Microsoft Graph. - max_retries (
int) – The maximum number of retries for throttled (HTTP 429) or transient server errors. - max_concurrent_requests (
int) – The maximum number of items fetched concurrently byrun_async. Bounds the in-flight requests to Microsoft Graph to avoid tripping its rate limits. Has no effect on the synchronousrun, which fetches items one at a time. - raise_on_failure (
bool) – IfTrue, a fetch failure raises an exception. IfFalse, the failure is logged and the item is skipped, so the other items are still returned.
Raises:
SharePointConfigError– Ifmax_retriesis negative ormax_concurrent_requestsis not positive.
run
run(
access_token: str | Secret, targets: list[Document | str]
) -> dict[str, list[ByteStream]]
Fetch the content of SharePoint and OneDrive items and return them as ByteStreams.
Parameters:
- access_token (
str | Secret) – A delegated Microsoft Graph bearer token for the user whose content is fetched, typically wired from an upstreamOAuthTokenResolver(which emits a plainstr). ASecretis also accepted and resolved internally. - targets (
list[Document | str]) – The items to fetch, as eitherDocuments emitted byMSSharePointRetrieveror raw SharePoint/OneDriveweb_urlstrings (the two may also be mixed in one list). For aDocument, theweb_urlin its meta is fetched andfile_name,mime_type,entity_type, and the SharePoint IDs are reused when present; container hits with no extractable content (for examplesiteorlist) are skipped. For a raw URL, the item is probed as a file and falls back to a list item.
Returns:
dict[str, list[ByteStream]]– A dictionary with astreamskey holding the fetched content asByteStreamobjects. Each stream'smetacarriesurl,file_name,content_type, andentity_type.
Raises:
SharePointConfigError– If an item is neither aDocumentnor astr, or ifaccess_tokenis aSecretthat does not resolve to a string.SharePointRequestError– If a fetch fails andraise_on_failureisTrue.
run_async
run_async(
access_token: str | Secret, targets: list[Document | str]
) -> dict[str, list[ByteStream]]
Asynchronously fetch the content of SharePoint and OneDrive items and return them as ByteStreams.
Parameters:
- access_token (
str | Secret) – A delegated Microsoft Graph bearer token for the user whose content is fetched, typically wired from an upstreamOAuthTokenResolver(which emits a plainstr). ASecretis also accepted and resolved internally. - targets (
list[Document | str]) – The items to fetch, as eitherDocuments emitted byMSSharePointRetrieveror raw SharePoint/OneDriveweb_urlstrings (the two may also be mixed in one list). For aDocument, theweb_urlin its meta is fetched andfile_name,mime_type,entity_type, and the SharePoint IDs are reused when present; container hits with no extractable content (for examplesiteorlist) are skipped. For a raw URL, the item is probed as a file and falls back to a list item.
Returns:
dict[str, list[ByteStream]]– A dictionary with astreamskey holding the fetched content asByteStreamobjects. Each stream'smetacarriesurl,file_name,content_type, andentity_type.
Raises:
SharePointConfigError– If an item is neither aDocumentnor astr, or ifaccess_tokenis aSecretthat does not resolve to a string.SharePointRequestError– If a fetch fails andraise_on_failureisTrue.
to_dict
Serialize this component to a dictionary.
Returns:
dict[str, Any]– The serialized component as a dictionary.
from_dict
Deserialize this component from a dictionary.
Parameters:
- data (
dict[str, Any]) – The dictionary representation of this component.
Returns:
MSSharePointFetcher– The deserialized component instance.
haystack_integrations.components.retrievers.microsoft_sharepoint.retriever
MSSharePointRetriever
Retrieves content from Microsoft SharePoint and OneDrive via the Microsoft Search (Graph) API.
Given a query, the retriever calls POST /search/query and maps each hit to a Haystack Document
whose content is the search snippet and whose meta carries the resource metadata (file_name,
web_url, entity_type, created_date_time, last_modified_date_time, created_by, last_modified_by,
mime_type, and file_extension), plus the SharePoint identifiers a downstream fetcher needs to read
list items and pages by ID (site_id, list_id, list_item_id, list_item_unique_id). It does not
download or convert the underlying files. Compose a downstream fetcher/converter (such as
MSSharePointFetcher) when full content is needed.
The retriever takes a per-user access_token as a run input, typically wired
from an upstream OAuthResolver. The token must carry delegated Microsoft Graph permissions
(for example Files.Read.All and, for site/list scoping, Sites.Read.All). The Search API supports
delegated permissions only.
Usage example
from haystack_integrations.components.retrievers.microsoft_sharepoint import (
MSSharePointRetriever,
)
retriever = MSSharePointRetriever(top_k=5)
# `access_token` is a per-user delegated Microsoft Graph bearer token.
result = retriever.run(
query="quarterly roadmap", access_token="my-delegated-graph-token"
)
documents = result["documents"]
In a pipeline, connect an upstream component that emits a per-user access_token to the retriever's
access_token input. See the integration documentation for a full example that obtains the token from
an OAuth provider.
init
__init__(
*,
entity_types: list[str] | None = None,
top_k: int = 10,
fields: list[str] | None = None,
query_template: str | None = None,
graph_url: str = DEFAULT_GRAPH_URL,
timeout: float = 30.0,
max_retries: int = 3
) -> None
Initialize the retriever.
Parameters:
- entity_types (
list[str] | None) – The Microsoft Search entity types to query. Defaults to["driveItem", "listItem"], which covers files, folders, SharePoint pages and news, and list items. Other valid values are"list"and"site". See the supported values and combinations in the Microsoft docs. - top_k (
int) – The maximum number of documents to return. Maps to the Search APIsizeand is paginated when it exceeds a single page. - fields (
list[str] | None) – Optional list of resource properties to request via the Search APIfieldsselection (only honored forlistItemanddriveItementity types). See Get selected properties. - query_template (
str | None) – Optional query template used to scope the search, for example'{searchTerms} path:"https://contoso.sharepoint.com/sites/Team"'. The literal{searchTerms}placeholder is replaced by the run-time query. The template uses Keyword Query Language (KQL). - graph_url (
str) – The Microsoft Graph base URL. Defaults tohttps://graph.microsoft.com/v1.0. Override for sovereign clouds. - timeout (
float) – The HTTP timeout in seconds for each request to Microsoft Graph. - max_retries (
int) – The maximum number of retries for throttled (HTTP 429) or transient server errors.
Raises:
SharePointConfigError– Ifentity_typesis empty,top_kis not positive, ormax_retriesis negative.
run
run(
query: str, access_token: str | Secret, top_k: int | None = None
) -> dict[str, list[Document]]
Search SharePoint and OneDrive and return the matching documents.
Parameters:
- query (
str) – The search query string. Filter results by embedding Keyword Query Language (KQL) operators directly in the query, for examplefiletype:docx,author:"Jane Doe", orpath:"https://contoso.sharepoint.com/sites/Team". See the KQL syntax reference. - access_token (
str | Secret) – A delegated Microsoft Graph bearer token for the user whose content is searched, typically wired from an upstreamOAuthResolver(which emits a plainstr). ASecretis also accepted and resolved internally. - top_k (
int | None) – Overrides thetop_kconfigured at initialization for this run.
Returns:
dict[str, list[Document]]– A dictionary with adocumentskey holding the list of retrievedDocumentobjects.
Raises:
SharePointConfigError– Ifaccess_tokenis aSecretthat does not resolve to a string.SharePointRequestError– If Microsoft Graph returns an error response.
run_async
run_async(
query: str, access_token: str | Secret, top_k: int | None = None
) -> dict[str, list[Document]]
Asynchronously search SharePoint and OneDrive and return the matching documents.
Parameters:
- query (
str) – The search query string. Filter results by embedding Keyword Query Language (KQL) operators directly in the query, for examplefiletype:docx,author:"Jane Doe", orpath:"https://contoso.sharepoint.com/sites/Team". See the KQL syntax reference. - access_token (
str | Secret) – A delegated Microsoft Graph bearer token for the user whose content is searched, typically wired from an upstreamOAuthResolver(which emits a plainstr). ASecretis also accepted and resolved internally. - top_k (
int | None) – Overrides thetop_kconfigured at initialization for this run.
Returns:
dict[str, list[Document]]– A dictionary with adocumentskey holding the list of retrievedDocumentobjects.
Raises:
SharePointConfigError– Ifaccess_tokenis aSecretthat does not resolve to a string.SharePointRequestError– If Microsoft Graph returns an error response.
to_dict
Serialize this component to a dictionary.
Returns:
dict[str, Any]– The serialized component as a dictionary.
from_dict
Deserialize this component from a dictionary.
Parameters:
- data (
dict[str, Any]) – The dictionary representation of this component.
Returns:
MSSharePointRetriever– The deserialized component instance.