1
1
import { SPHttpClient , ISPHttpClientOptions } from '@microsoft/sp-http' ;
2
+ import RestAPIHelper from './RestAPIHelper' ;
3
+ import { Text } from '@microsoft/sp-core-library' ;
2
4
// Microsoft.SharePoint.Client.Search.Administration.DocumentCrawlLog
3
5
export default class SearchHelper
4
6
{
@@ -69,4 +71,147 @@ export default class SearchHelper
69
71
return await responseJson ;
70
72
}
71
73
}
74
+
75
+ public static async GetManagedProperties ( spHttpClient :SPHttpClient , webAbsoluteUrl :string , workId :string )
76
+ {
77
+ // Get https://chengc.sharepoint.com/_api/search/query?querytext='WorkId%3a%22xxxxxx%22'&rowlimit=1&refiners='ManagedProperties(filter%3d5000%2f0%2f*)'&clienttype='ContentSearchRegular'
78
+ // let apiUrl = `${webAbsoluteUrl}/_api/search/query?querytext='WorkId="${workId}"'&rowlimit=1&refiners='ManagedProperties(filter=5000/0/*)'`;
79
+ // let apiUrl = `https://chengc.sharepoint.com/_api/search/query?querytext='Path%3ahttps%3a%2f%2fchengc.sharepoint.com%2fExcelLib%2fexcel2.xlsx'&rowlimit=1&refiners='ManagedProperties(filter%3d5000%2f0%2f*)'&sortlist='%5bdocid%5d%3aascending'&hiddenconstraints='WorkId%3a%22157214370457891573%22'&clienttype='ContentSearchRegular'`;
80
+ let apiUrl = `${ webAbsoluteUrl } /_api/search/postquery` ;
81
+ const body : string = JSON . stringify ( {
82
+ request : {
83
+ ClientType : "ModernWebPart" ,
84
+ Querytext : `WorkId="${ workId } "` ,
85
+ RowLimit : 1 ,
86
+ Refiners :'ManagedProperties(filter=5000/0/*)'
87
+ } } ) ;
88
+ const headers : Headers = new Headers ( ) ;
89
+ headers . append ( 'Accept' , 'application/json;odata=nometadata' ) ;
90
+ headers . append ( 'Content-type' , 'application/json;charset=utf-8' ) ;
91
+ headers . append ( 'OData-Version' , '3.0' ) ;
92
+
93
+ const httpClientOptions : ISPHttpClientOptions = {
94
+ body : body ,
95
+ headers :headers
96
+ } ;
97
+
98
+ var res :any = await spHttpClient . post ( apiUrl , SPHttpClient . configurations . v1 , httpClientOptions ) ;
99
+ var propertiesFirstRequest = [ ] ;
100
+ if ( res . ok )
101
+ {
102
+ res = await res . json ( ) ;
103
+ if ( res . PrimaryQueryResult . RefinementResults . Refiners
104
+ && res . PrimaryQueryResult . RefinementResults . Refiners . length > 0
105
+ && res . PrimaryQueryResult . RefinementResults . Refiners [ 0 ] . Entries . length > 0
106
+ )
107
+ {
108
+ var selectProperties = [ ] ;
109
+ var excludeProperties = [ "ClassificationContext" , "ClassificationLastScan" , "Color" , "ContentDatabaseId" ] ;
110
+ res . PrimaryQueryResult . RelevantResults . Table . Rows [ 0 ] . Cells . forEach ( e => { propertiesFirstRequest . push ( e ) ; } ) ;
111
+ res . PrimaryQueryResult . RefinementResults . Refiners [ 0 ] . Entries . forEach ( e => {
112
+ var filterMps = propertiesFirstRequest . filter ( p => p . Key == e . RefinementName ) ;
113
+ if ( filterMps . length == 0 && excludeProperties . indexOf ( e . RefinementName ) < 0 )
114
+ {
115
+ selectProperties . push ( e . RefinementName ) ;
116
+ }
117
+ } ) ;
118
+ const queryAllMPBody : string = JSON . stringify ( {
119
+ request : {
120
+ ClientType : "ModernWebPart" ,
121
+ Querytext : `WorkId="${ workId } "` ,
122
+ RowLimit : 1 ,
123
+ SelectProperties :selectProperties
124
+ } } ) ;
125
+
126
+ const queryAllMPhttpClientOptions : ISPHttpClientOptions = {
127
+ body : queryAllMPBody ,
128
+ headers :headers
129
+ } ;
130
+
131
+ try
132
+ {
133
+ var allMPRes :any = await spHttpClient . post ( apiUrl , SPHttpClient . configurations . v1 , queryAllMPhttpClientOptions ) ;
134
+ if ( allMPRes . ok )
135
+ {
136
+ res = await allMPRes . json ( ) ;
137
+ }
138
+ else
139
+ {
140
+ console . error ( "Failed to call /_api/search/postquery when selecting all managed properties" ) ;
141
+ }
142
+ }
143
+ catch ( ex )
144
+ {
145
+ console . error ( ex ) ;
146
+ }
147
+ }
148
+ }
149
+ else
150
+ {
151
+ console . error ( "Failed to call /_api/search/postquery, ManagedProperties(filter=5000/0/*)" ) ;
152
+ }
153
+
154
+ // res.PrimaryQueryResult.RelevantResults.RowCount
155
+ // res.PrimaryQueryResult.RelevantResults.Table.Rows[0].Cells
156
+ // res.PrimaryQueryResult.RelevantResults.Table.Rows[0].Cells[0].Value
157
+ // res.PrimaryQueryResult.RelevantResults.Table.Rows[0].Cells[0].Key
158
+ propertiesFirstRequest . forEach ( e => {
159
+ var filterProrpties = res . PrimaryQueryResult . RelevantResults . Table . Rows [ 0 ] . Cells . filter ( pro => pro . Key == e . Key ) ;
160
+ if ( filterProrpties . length == 0 )
161
+ {
162
+ res . PrimaryQueryResult . RelevantResults . Table . Rows [ 0 ] . Cells . push ( e ) ;
163
+ }
164
+ } ) ;
165
+
166
+ return res ;
167
+ }
168
+
169
+ public static async GetCrawledProperties ( spHttpClient :SPHttpClient , webAbsoluteUrl :string , workId :string )
170
+ {
171
+ // Get https://chengc.sharepoint.com/_api/search/query?querytext='WorkId%3a%22xxxxx%22'&rowlimit=1&refiners='CrawledProperties(filter%3d5000%2f0%2f*)'&clienttype='ContentSearchRegular'
172
+ // let apiUrl = `${webAbsoluteUrl}/_api/search/query?querytext='WorkId="${workId}"'&rowlimit=1&refiners='CrawledProperties(filter=5000/0/*)'`;
173
+ let apiUrl = `${ webAbsoluteUrl } /_api/search/postquery` ;
174
+ const body : string = JSON . stringify ( {
175
+ request : {
176
+ ClientType : "ModernWebPart" ,
177
+ Querytext : `WorkId="${ workId } "` ,
178
+ RowLimit : 1 ,
179
+ Refiners :'CrawledProperties(filter=5000/0/*)'
180
+ } } ) ;
181
+ const headers : Headers = new Headers ( ) ;
182
+ headers . append ( 'Accept' , 'application/json;odata=nometadata' ) ;
183
+ headers . append ( 'Content-type' , 'application/json;charset=utf-8' ) ;
184
+ headers . append ( 'OData-Version' , '3.0' ) ;
185
+
186
+ const httpClientOptions : ISPHttpClientOptions = {
187
+ body : body ,
188
+ headers :headers
189
+ } ;
190
+
191
+ var res = await spHttpClient . post ( apiUrl , SPHttpClient . configurations . v1 , httpClientOptions ) ;
192
+ if ( res . ok )
193
+ {
194
+ res = await res . json ( ) ;
195
+ }
196
+ else
197
+ {
198
+ console . error ( "Failed to call /_api/search/postquery, CrawledProperties(filter=5000/0/*)" ) ;
199
+ }
200
+
201
+ // res.PrimaryQueryResult.RefinementResults.Refiners[0].Entries[0].RefinementName
202
+ return await res ;
203
+ }
204
+
205
+ public static async CallOtherDiagnosticsAPIS ( spHttpClient :SPHttpClient , webAbsoluteUrl :string , workId :string )
206
+ {
207
+ // /_api/search/query?querytext='workid:{0}'&properties='QueryIdentityDiagnostics:true'&property='EnableDynamicGroups:true'&TrimDuplicates=false
208
+ // /_api/search/query?querytext='workid:{0}'&properties='3SRouted:false,QueryIdentityDiagnostics:true'&property='EnableDynamicGroups:true'&TrimDuplicates=false
209
+ var apiTemplates = [ "{0}/_api/search/query?querytext='workid:{1}'&properties='QueryIdentityDiagnostics:true'&property='EnableDynamicGroups:true'&TrimDuplicates=false" ,
210
+ "{0}/_api/search/query?querytext='workid:{1}'&properties='3SRouted:false,QueryIdentityDiagnostics:true'&property='EnableDynamicGroups:true'&TrimDuplicates=false" ] ;
211
+ for ( var index = 0 ; index < apiTemplates . length ; index ++ )
212
+ {
213
+ var apiUrl = Text . format ( apiTemplates [ index ] , webAbsoluteUrl , workId ) ;
214
+ await spHttpClient . get ( apiUrl , SPHttpClient . configurations . v1 ) ;
215
+ }
216
+ }
72
217
}
0 commit comments