README Search SharePoint and OneDrive

README Search Files (Search API) #

Overview #

Searches for files across OneDrive for Business and SharePoint document libraries simultaneously using the Microsoft Search API. Authentication is via OAuth2 with MFA support using VBAuth.dll. The two sources cannot be separated at the API level — results are always combined.

Two search modes are available via radio button: Name only (server-side KQL filename: prefix) and Name and content (full-text index search). ContentType:Document is added to all queries to exclude folders, site pages, and news from results.

Results display filename, last modified date, last modified by, and the web URL of each file. Up to 25 results are returned per search; if more exist a note is appended to the output.

Office apps #

  • Access (tested M365)
  • Excel (untested M365)

Required components #

  • VBAuth.dll v1.x
  • modVBAuthGlobalConstants.bas (from /shared)
  • modSearchFilesSearchAPI.bas (this module)

Azure Entra App Registration #

Delegated permissions:

  • Files.Read.All

Note: Files.Read is insufficient for POST /search/query — it returns HTTP 403. Files.Read.All is the minimum required scope for this endpoint. Admin consent is required before users can authenticate.

Note: POST /search/query requires a work or school M365 account. It does not support personal Microsoft accounts (MSA).

VBAuth Admin Table – Required Values #

Column NameRequired Value / ExampleNotes
AppID26Identifies this feature’s configuration record
ActionNameVBAuth – Search Files (Search API)Display name — any value acceptable
ClientID(your App Client ID)GUID from Azure App Registration Overview blade
TenantID(your Tenant ID)GUID from Azure App Registration Overview blade
ObjectID(your Object ID)Set by PopulateMissingObjectIDs
ScopeClaimsFiles.Read.AllComma-separated Graph scopes
VerbPOSTHTTP method — Graph Search API uses POST
URLPart1https://graph.microsoft.com/v1.0/search/queryGraph Search API endpoint
URLParameter1NO VALUE REQUIREDNot used
URLPart2NO VALUE REQUIREDNot used
URLParameter2NO VALUE REQUIREDNot used
URLPart3NO VALUE REQUIREDNot used
WaitForLogin60Seconds — NB MSXML2 timeout parameters are in milliseconds

Module: modSearchFilesSearchAPI.bas #

Public functions #

Public Function SearchFilesSearchAPI( _
    ByVal strTerm As String, _
    ByVal intSearchMode As Integer, _
    ByRef strResult As String, _
    ByRef strMessage As String) As Boolean
Code language: JavaScript (javascript)

Builds and POSTs a JSON search request to POST /search/query with entityTypes: ["driveItem"]. intSearchMode 1 = name only (KQL filename: prefix), 2 = name and content. Returns True on success. strResult receives the formatted display text. strMessage receives a summary on success or error description on failure.

Private functions #

  • SafeString — returns a string field from a parsed JSON Dictionary, returning "" for absent fields without error.
  • SafeNestedString — reads a value from a two or three level nested Dictionary safely.
  • BuildQueryString — constructs the KQL query string for the selected search mode.

Behaviour #

  • Runs as the signed-in user (delegated permissions).
  • HTTP 200 is the success code.
  • Results cover both OneDrive for Business and SharePoint document libraries combined. There is no supported API mechanism to restrict results to one source only.
  • ContentType:Document is appended to all queries to exclude folders, SharePoint site pages, and news items.
  • Name only mode uses the KQL filename: prefix which is a server-side filter — only files whose name matches are returned.
  • Name and content mode searches the full-text index — files matching in either name or content are returned.
  • Up to 25 results are returned per call (size: 25 in the request body). If the total exceeds 25 a note is appended to the output.
  • The hits key is absent in the response when zero results are found. This is guarded with On Error Resume Next + Is Nothing check.
  • Authentication is interactive on first run; subsequent calls within the session reuse the cached token.

Notes #

  • Files.Read.All is the minimum required scope — Files.Read returns HTTP 403 for this endpoint.
  • Admin consent is required before users can authenticate.
  • This endpoint does not support personal Microsoft accounts (MSA) — it requires a work or school M365 account.
  • This feature uses a separate Entra app registration from the Drive API search feature (AppID 27) so that a developer taking only this feature uses the correct minimum scope.

VBAuth, developed and maintained by Andrew Richards (The IT Service Ltd) and Peter Bryant (Corylus Business Systems), © 2025–2026. All rights reserved. https://vbauth.com

Updated on April 3, 2026