Skip to content

Commit ae09a23

Browse files
petkirAJIXuMuK
authored andcommitted
Add webAbsoluteUrl Support for RecentFilesTab Tab
1 parent b002c1e commit ae09a23

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

src/controls/filePicker/FilePicker.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ export class FilePicker extends React.Component<
6767
);
6868
this.fileSearchService = new FilesSearchService(
6969
props.context,
70-
this.props.bingAPIKey
70+
this.props.bingAPIKey,
71+
this.props.webAbsoluteUrl
7172
);
7273

7374
this.state = {

src/services/FilesSearchService.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BaseComponentContext } from '@microsoft/sp-component-base';
22
import { SPHttpClient, HttpClientResponse } from "@microsoft/sp-http";
3-
import { ISearchResult, BingQuerySearchParams, IRecentFile } from "./FilesSearchService.types";
3+
import { ISearchResult, BingQuerySearchParams, IRecentFile, ISiteWebInfo } from "./FilesSearchService.types";
44
import { find } from "office-ui-fabric-react/lib/Utilities";
55
import { GeneralHelper } from "../common/utilities/GeneralHelper";
66
import type { IBingSearchResult } from '../controls/filePicker/WebSearchTab/IBingSearchResult';
@@ -18,10 +18,12 @@ const MAXRESULTS = 100;
1818
export class FilesSearchService {
1919
private context: BaseComponentContext;
2020
private bingAPIKey: string;
21+
private siteAbsoluteUrl: string;
2122

22-
constructor(context: BaseComponentContext, bingAPIKey: string) {
23+
constructor(context: BaseComponentContext, bingAPIKey: string, siteAbsoluteUrl?: string) {
2324
this.context = context;
2425
this.bingAPIKey = bingAPIKey;
26+
this.siteAbsoluteUrl = siteAbsoluteUrl || context.pageContext.web.absoluteUrl
2527
}
2628

2729
/**
@@ -51,8 +53,13 @@ export class FilesSearchService {
5153
*/
5254
public executeRecentSearch = async (accepts?: string[]): Promise<IRecentFile[] | undefined> => {
5355
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+
}
5663
const fileFilter = this._getFileFilter(accepts);
5764

5865
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 {
96103
]
97104
}
98105
};
99-
const searchApi = `${this.context.pageContext.web.absoluteUrl}/_api/search/postquery`;
106+
const searchApi = `${this.siteAbsoluteUrl}/_api/search/postquery`;
100107

101108
const recentSearchDataResult = await this.context.spHttpClient.post(searchApi, SPHttpClient.configurations.v1, {
102109
headers: {
@@ -289,4 +296,21 @@ export class FilesSearchService {
289296
const splitUrl = url.split('/');
290297
return splitUrl[0];
291298
}
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+
}
292316
}

src/services/FilesSearchService.types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,9 @@ export interface Thumbnail {
6868
width: number;
6969
height: number;
7070
}
71+
72+
export interface ISiteWebInfo {
73+
title: string,
74+
webId: string,
75+
siteId: string
76+
}

src/webparts/controlsTest/components/ControlsTest.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,8 +1703,8 @@ export default class ControlsTest extends React.Component<IControlsTestProps, IC
17031703
/>
17041704
<FilePicker
17051705
bingAPIKey="<BING API KEY>"
1706-
webAbsoluteUrl="https://aterentiev.sharepoint.com/sites/SPFxinTeamsDemo"
1707-
defaultFolderAbsolutePath={"https://aterentiev.sharepoint.com/sites/SPFxinTeamsDemo/Shared%20Documents/General"}
1706+
//webAbsoluteUrl="https://023xn.sharepoint.com/sites/test1"
1707+
//defaultFolderAbsolutePath={"https://aterentiev.sharepoint.com/sites/SPFxinTeamsDemo/Shared%20Documents/General"}
17081708
//accepts={[".gif", ".jpg", ".jpeg", ".bmp", ".dib", ".tif", ".tiff", ".ico", ".png", ".jxr", ".svg"]}
17091709
buttonLabel="Add File"
17101710
buttonIconProps={{ iconName: 'Add', styles: { root: { fontSize: 42 } } }}

0 commit comments

Comments
 (0)