1
1
import { BaseComponentContext } from '@microsoft/sp-component-base' ;
2
2
import { SPHttpClient , HttpClientResponse } from "@microsoft/sp-http" ;
3
- import { ISearchResult , BingQuerySearchParams , IRecentFile } from "./FilesSearchService.types" ;
3
+ import { ISearchResult , BingQuerySearchParams , IRecentFile , ISiteWebInfo } from "./FilesSearchService.types" ;
4
4
import { find } from "office-ui-fabric-react/lib/Utilities" ;
5
5
import { GeneralHelper } from "../common/utilities/GeneralHelper" ;
6
6
import type { IBingSearchResult } from '../controls/filePicker/WebSearchTab/IBingSearchResult' ;
@@ -18,10 +18,12 @@ const MAXRESULTS = 100;
18
18
export class FilesSearchService {
19
19
private context : BaseComponentContext ;
20
20
private bingAPIKey : string ;
21
+ private siteAbsoluteUrl : string ;
21
22
22
- constructor ( context : BaseComponentContext , bingAPIKey : string ) {
23
+ constructor ( context : BaseComponentContext , bingAPIKey : string , siteAbsoluteUrl ?: string ) {
23
24
this . context = context ;
24
25
this . bingAPIKey = bingAPIKey ;
26
+ this . siteAbsoluteUrl = siteAbsoluteUrl || context . pageContext . web . absoluteUrl
25
27
}
26
28
27
29
/**
@@ -51,8 +53,13 @@ export class FilesSearchService {
51
53
*/
52
54
public executeRecentSearch = async ( accepts ?: string [ ] ) : Promise < IRecentFile [ ] | undefined > => {
53
55
try {
54
- const webId = this . context . pageContext . web . id . toString ( ) ;
55
- const siteId = this . context . pageContext . site . id . toString ( ) ;
56
+ let webId = this . context . pageContext . web . id . toString ( ) ;
57
+ let siteId = this . context . pageContext . site . id . toString ( ) ;
58
+ if ( this . siteAbsoluteUrl !== this . context . pageContext . web . absoluteUrl ) {
59
+ const siteinfo = await this . getSiteInfos ( this . siteAbsoluteUrl ) ;
60
+ webId = siteinfo . webId ;
61
+ siteId = siteinfo . siteId ;
62
+ }
56
63
const fileFilter = this . _getFileFilter ( accepts ) ;
57
64
58
65
const queryTemplate : string = `((SiteID:${ siteId } OR SiteID: {${ siteId } }) AND (WebId: ${ webId } OR WebId: {${ webId } })) AND LastModifiedTime < {Today} AND -Title:OneNote_DeletedPages AND -Title:OneNote_RecycleBin${ fileFilter } ` ;
@@ -96,7 +103,7 @@ export class FilesSearchService {
96
103
]
97
104
}
98
105
} ;
99
- const searchApi = `${ this . context . pageContext . web . absoluteUrl } /_api/search/postquery` ;
106
+ const searchApi = `${ this . siteAbsoluteUrl } /_api/search/postquery` ;
100
107
101
108
const recentSearchDataResult = await this . context . spHttpClient . post ( searchApi , SPHttpClient . configurations . v1 , {
102
109
headers : {
@@ -289,4 +296,21 @@ export class FilesSearchService {
289
296
const splitUrl = url . split ( '/' ) ;
290
297
return splitUrl [ 0 ] ;
291
298
}
299
+ private getSiteInfos = async ( absUrl : string ) : Promise < ISiteWebInfo > => {
300
+ const webInfo = await this . context . spHttpClient . get ( absUrl + '/_api/web?$select=id,Title' , SPHttpClient . configurations . v1 ) ;
301
+ if ( ! webInfo || ! webInfo . ok ) {
302
+ throw new Error ( `[FileBrowser.getWebInfo]: Something went wrong when executing request. Status='${ webInfo . statusText } '` ) ;
303
+ }
304
+ const siteInfo = await this . context . spHttpClient . get ( absUrl + '/_api/site?$select=id' , SPHttpClient . configurations . v1 ) ;
305
+ if ( ! siteInfo || ! siteInfo . ok ) {
306
+ throw new Error ( `[FileBrowser.getWebInfo]: Something went wrong when executing request. Status='${ webInfo . statusText } '` ) ;
307
+ }
308
+ const webInfoResult = await webInfo . json ( ) ;
309
+ const siteInfoResult = await siteInfo . json ( ) ;
310
+ return ( {
311
+ title : webInfoResult . Title ,
312
+ webId : webInfoResult . Id ,
313
+ siteId : siteInfoResult . Id
314
+ } )
315
+ }
292
316
}
0 commit comments