Office365-REST-Python-Client
Office365-REST-Python-Client copied to clipboard
Unable to search within shared documents for a specific site using office365.sharepoint.search
I'm having issues attempting to write code that will search for text within shared documents stored on a specific SharePoint site (code below). The code "works," but returns a list of documents across https://acme-my.sharepoint.com/personal/ where the document name matches the search term, but not the site I specified.
Is there a way to create a search_query that will search the contents of shared documents stored on a specific SharePoint site?
What are the valid options for query_text when using SearchRequest? Example: SearchRequest(query_text="password AND IsDocument:1 AND Path:{g.spURL}{relative_url}") - Path: or Site: does not work.
Version: 2.3.13
(Note: I omitted the ClientCredentials code)
from office365.runtime.auth.client_credential import ClientCredential
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.search.search_service import SearchService
from office365.sharepoint.search.search_request import SearchRequest
# create client context and search objects
ctx = ClientContext(g.spURL).with_credentials(client_credentials)
search = SearchService(ctx)
# search shared documents that contain the word password for a specific site
request = SearchRequest(query_text="password AND IsDocument:1")
#Adding Path or Site will not work: request = SearchRequest(query_text="password AND IsDocument:1 AND Path:{g.spURL}{relative_url}")
result = search.post_query(request).execute_query()
relevant_results = result.value.PrimaryQueryResult.RelevantResults
# display output
for r in relevant_results['Table']['Rows']:
cells = relevant_results['Table']['Rows'][r]['Cells']
print(cells[1]['Value'])
print(cells[2]['Value'])
print(cells[3]['Value'])
print(cells[4]['Value'])
print(cells[5]['Value'])
print(cells[6]['Value'])
print(cells[7]['Value'])
print(cells[8]['Value'])
print('\n')
Example output (one matching record cells[0]-cells[8]) - note the output (in all cases) matches on the document name, not the contents of the document:
994.5443134
Password
doejohn1;BUILTIN\Administrators;Doe,John
38912
https://acme-my.sharepoint.com/personal/doejohn1_acme_net/Documents/DATA/Budget/Passwords/Password.doc
None
2022-07-06T13:10:00.0000000Z
0
Thank you
Just to clarify, why is the SearchRequest ignoring the ClientContext url?
context requested: https://acme.sharepoint.com/sites/IT/ERP
actual context at runtime: https://acme-my.sharepoint.com/personal/....
Greetings,
this is expected behavior, per official documentation:
When you query in the context of a SharePoint Online user, you get results from:
Content in SharePoint Online site collections
Content in Microsoft 365 groups
Shared OneDrive for Business content (content that's accessible for others than the owner of the OneDrive for Business)
Content from SharePoint Server that's been indexed via a cloud search Service application. Learn about cloud hybrid search.
To limit search scope to search within a specific site, Path managed property could be specified, for example:
result = ctx.search.post_query(
"Path={0}/*".format(site_url), row_limit=10
).execute_query()