Skip to content

Commit 5e4a8fb

Browse files
committed
SharePoint API: search namespace enhancements
1 parent 79cb42a commit 5e4a8fb

File tree

16 files changed

+157
-69
lines changed

16 files changed

+157
-69
lines changed

examples/sharepoint/search/export_reports.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,3 @@
99
result = setting.export_search_reports(tenant_id="af6a80a4-8b4b-4879-88af-42ff8a545211",
1010
report_type="ReportTopQueries").execute_query()
1111
print(result)
12-
Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,10 @@
1-
import json
2-
3-
from office365.runtime.auth.user_credential import UserCredential
41
from office365.sharepoint.client_context import ClientContext
5-
from office365.sharepoint.search.request import SearchRequest
6-
from office365.sharepoint.search.service import SearchService
7-
from tests import test_site_url, settings
8-
9-
10-
def extract_cell_value(cells, name):
11-
"""
12-
Extract cell value
13-
"""
14-
return next((c.get("Value") for c in cells.values() if c.get("Key") == name), None)
15-
2+
from tests import test_site_url, test_user_credentials
163

17-
user_credentials = UserCredential(settings.get('user_credentials', 'username'),
18-
settings.get('user_credentials', 'password'))
19-
ctx = ClientContext(test_site_url).with_credentials(user_credentials)
20-
search = SearchService(ctx)
4+
ctx = ClientContext(test_site_url).with_credentials(test_user_credentials)
215

22-
request = SearchRequest(query_text="IsDocument:1", select_properties=["Title", "Path"],
23-
trim_duplicates=False, row_limit=50)
24-
result = search.post_query(request).execute_query()
25-
relevant_results = result.value.PrimaryQueryResult.RelevantResults
26-
for i in relevant_results['Table']['Rows']:
27-
cur_cells = relevant_results['Table']['Rows'][i]['Cells']
28-
file_url = extract_cell_value(cur_cells, "Path")
6+
result = ctx.search.post_query(query_text="IsDocument:1").execute_query()
7+
results = result.value.PrimaryQueryResult.RelevantResults
8+
for row in results.Table.Rows:
9+
file_url = row.Cells["Path"]
2910
print(file_url)
30-
# print(json.dumps(cells, sort_keys=True, indent=4))
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import json
21
from office365.sharepoint.client_context import ClientContext
3-
from office365.sharepoint.search.service import SearchService
42
from tests import test_site_url, test_user_credentials
53

64
ctx = ClientContext(test_site_url).with_credentials(test_user_credentials)
7-
search = SearchService(ctx)
8-
result = search.query("IsDocument:1").execute_query()
9-
relevant_results = result.value.PrimaryQueryResult.RelevantResults
10-
for i in relevant_results['Table']['Rows']:
11-
cells = relevant_results['Table']['Rows'][i]['Cells']
12-
print(json.dumps(cells, sort_keys=True, indent=4))
5+
result = ctx.search.query("IsDocument:1").execute_query()
6+
results = result.value.PrimaryQueryResult.RelevantResults
7+
for row in results.Table.Rows:
8+
print(row.Cells["Path"])
Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
from office365.sharepoint.client_context import ClientContext
2-
from office365.sharepoint.search.query.sort import Sort
3-
from office365.sharepoint.search.request import SearchRequest
4-
from office365.sharepoint.search.service import SearchService
2+
from office365.sharepoint.search.query.sort.sort import Sort
53
from tests import test_site_url, test_user_credentials
64

75
ctx = ClientContext(test_site_url).with_credentials(test_user_credentials)
8-
search = SearchService(ctx)
9-
10-
request = SearchRequest(query_text="IsDocument:1",
11-
sort_list=[Sort("LastModifiedTime", 1)],
12-
select_properties=["Path", "LastModifiedTime"],
13-
row_limit=20)
14-
result = search.post_query(request).execute_query()
15-
relevant_results = result.value.PrimaryQueryResult.RelevantResults
16-
for r in relevant_results.get('Table').get('Rows').items():
17-
cells = r[1].get('Cells')
18-
print(cells[1].get('Value'))
6+
result = ctx.search.post_query(query_text="IsDocument:1",
7+
sort_list=[Sort("LastModifiedTime", 1)],
8+
select_properties=["Path", "LastModifiedTime"],
9+
row_limit=20).execute_query()
10+
results = result.value.PrimaryQueryResult.RelevantResults
11+
for row in results.Table.Rows:
12+
print(row.Cells["Path"], row.Cells["LastModifiedTime"])

office365/sharepoint/search/custom_result.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,25 @@
22

33

44
class CustomResult(ClientValue):
5-
pass
5+
"""
6+
This contains a list of query results, all of which are of the type specified in TableType.
7+
"""
8+
9+
def __init__(self, group_template_id=None, item_template_id=None, result_title=None, properties=None):
10+
"""
11+
:param str group_template_id: Specifies the identifier of the layout template that specifies how the results
12+
returned will be arranged.
13+
:param str item_template_id: Specifies the identifier of the layout template that specifies how the result
14+
item will be displayed.
15+
:param str result_title: Specifies the title associated with results for the transformed query by query rule
16+
action. MUST NOT be more than 64 characters in length.
17+
:param dict properties: Specifies a property bag of key value pairs.
18+
"""
19+
self.GroupTemplateId = group_template_id
20+
self.ItemTemplateId = item_template_id
21+
self.Properties = properties
22+
self.ResultTitle = result_title
23+
24+
@property
25+
def entity_type_name(self):
26+
return "Microsoft.Office.Server.Search.REST.CustomResult"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from office365.runtime.client_value import ClientValue
2+
3+
4+
class QueryAutoCompletionMatch(ClientValue):
5+
"""Represents one match in the Source for the Query"""
6+
7+
@property
8+
def entity_type_name(self):
9+
return "Microsoft.SharePoint.Client.Search.Query.QueryAutoCompletionMatch"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from office365.runtime.client_value import ClientValue
2+
from office365.sharepoint.search.query.property_value import QueryPropertyValue
3+
4+
5+
class QueryProperty(ClientValue):
6+
"""This object stores additional or custom properties for a search query. A QueryProperty is structured
7+
as name-value pairs."""
8+
9+
def __init__(self, name=None, value=QueryPropertyValue()):
10+
"""
11+
:param str name: This property stores the name part of the QueryProperty name-value pair.
12+
:param QueryPropertyValue value: This property stores the value part of the QueryProperty name-value pair.
13+
"""
14+
self.Name = name
15+
self.Value = value
16+
17+
@property
18+
def entity_type_name(self):
19+
return "Microsoft.SharePoint.Client.Search.Query.QueryPropertyValue"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from office365.runtime.client_value import ClientValue
2+
3+
4+
class QueryPropertyValue(ClientValue):
5+
"""This object is used to store values of predefined types. The object MUST have a value set for only
6+
one of the property."""
7+
8+
@property
9+
def entity_type_name(self):
10+
return "Microsoft.SharePoint.Client.Search.Query.QueryPropertyValue"

office365/sharepoint/search/query_result.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
from office365.runtime.client_value import ClientValue
2+
from office365.runtime.client_value_collection import ClientValueCollection
3+
from office365.sharepoint.search.custom_result import CustomResult
24
from office365.sharepoint.search.refinement_results import RefinementResults
5+
from office365.sharepoint.search.relevant_results import RelevantResults
36

47

58
class QueryResult(ClientValue):
@@ -8,12 +11,22 @@ class QueryResult(ClientValue):
811
as specified in [MS-QSSWS] section 3.1.4.1.3.6.
912
"""
1013

11-
def __init__(self, query_id=None):
14+
def __init__(self, query_id=None, custom_results=None, relevant_results=RelevantResults(), query_rule_id=None):
1215
"""
1316
:param str query_id: Specifies the identifier for the search query
17+
:param list[CustomResults] custom_results: CustomResults is a list that contains zero or more CustomResult
18+
instances. A CustomResult instance is a ResultTable with ResultType of any kind (except RefinementResults,
19+
RelevantResults, and SpecialTermResults)
20+
:param RelevantResults relevant_results: This contains a list of query results, all of which are of the
21+
type specified in TableType.
22+
:param str query_rule_id: Specifies the unique identifier of the query rule that produced the result set.
23+
MUST be {00000000-0000-0000-0000-000000000000} if the result set is not associated to a query rule.
1424
"""
1525
self.QueryId = query_id
26+
self.QueryRuleId = query_rule_id
1627
self.RefinementResults = RefinementResults()
28+
self.CustomResults = ClientValueCollection(CustomResult, custom_results)
29+
self.RelevantResults = relevant_results
1730

1831
@property
1932
def entity_type_name(self):

office365/sharepoint/search/relevant_results.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44

55
class RelevantResults(ClientValue):
6+
"""
7+
The RelevantResults table contains the actual query results. It MUST only be present if the ResultTypes element
8+
in the properties element of the Execute message contains ResultType.RelevantResults,
9+
as specified in section 2.2.5.5
10+
"""
611

712
def __init__(self):
813
super(RelevantResults, self).__init__()

0 commit comments

Comments
 (0)