Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File picker site web support #1340

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/controls/filePicker/FilePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export class FilePicker extends React.Component<
);
this.fileSearchService = new FilesSearchService(
props.context,
this.props.bingAPIKey
this.props.bingAPIKey,
this.props.webAbsoluteUrl
);

this.state = {
Expand Down
34 changes: 29 additions & 5 deletions src/services/FilesSearchService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BaseComponentContext } from '@microsoft/sp-component-base';
import { SPHttpClient, HttpClientResponse } from "@microsoft/sp-http";
import { ISearchResult, BingQuerySearchParams, IRecentFile } from "./FilesSearchService.types";
import { ISearchResult, BingQuerySearchParams, IRecentFile, ISiteWebInfo } from "./FilesSearchService.types";
import { find } from "office-ui-fabric-react/lib/Utilities";
import { GeneralHelper } from "../common/utilities/GeneralHelper";
import type { IBingSearchResult } from '../controls/filePicker/WebSearchTab/IBingSearchResult';
Expand All @@ -18,10 +18,12 @@ const MAXRESULTS = 100;
export class FilesSearchService {
private context: BaseComponentContext;
private bingAPIKey: string;
private siteAbsoluteUrl: string;

constructor(context: BaseComponentContext, bingAPIKey: string) {
constructor(context: BaseComponentContext, bingAPIKey: string, siteAbsoluteUrl?: string) {
this.context = context;
this.bingAPIKey = bingAPIKey;
this.siteAbsoluteUrl = siteAbsoluteUrl || context.pageContext.web.absoluteUrl
}

/**
Expand Down Expand Up @@ -51,8 +53,13 @@ export class FilesSearchService {
*/
public executeRecentSearch = async (accepts?: string[]): Promise<IRecentFile[] | undefined> => {
try {
const webId = this.context.pageContext.web.id.toString();
const siteId = this.context.pageContext.site.id.toString();
let webId = this.context.pageContext.web.id.toString();
let siteId = this.context.pageContext.site.id.toString();
if (this.siteAbsoluteUrl !== this.context.pageContext.web.absoluteUrl) {
const siteinfo = await this.getSiteInfos(this.siteAbsoluteUrl);
webId = siteinfo.webId;
siteId = siteinfo.siteId;
}
const fileFilter = this._getFileFilter(accepts);

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}`;
Expand Down Expand Up @@ -96,7 +103,7 @@ export class FilesSearchService {
]
}
};
const searchApi = `${this.context.pageContext.web.absoluteUrl}/_api/search/postquery`;
const searchApi = `${this.siteAbsoluteUrl}/_api/search/postquery`;

const recentSearchDataResult = await this.context.spHttpClient.post(searchApi, SPHttpClient.configurations.v1, {
headers: {
Expand Down Expand Up @@ -289,4 +296,21 @@ export class FilesSearchService {
const splitUrl = url.split('/');
return splitUrl[0];
}
private getSiteInfos = async (absUrl: string): Promise<ISiteWebInfo> => {
const webInfo = await this.context.spHttpClient.get(absUrl + '/_api/web?$select=id,Title', SPHttpClient.configurations.v1);
if (!webInfo || !webInfo.ok) {
throw new Error(`[FileBrowser.getWebInfo]: Something went wrong when executing request. Status='${webInfo.statusText}'`);
}
const siteInfo = await this.context.spHttpClient.get(absUrl + '/_api/site?$select=id', SPHttpClient.configurations.v1);
if (!siteInfo || !siteInfo.ok) {
throw new Error(`[FileBrowser.getWebInfo]: Something went wrong when executing request. Status='${webInfo.statusText}'`);
}
const webInfoResult = await webInfo.json();
const siteInfoResult = await siteInfo.json();
return ({
title: webInfoResult.Title,
webId: webInfoResult.Id,
siteId: siteInfoResult.Id
})
}
}
6 changes: 6 additions & 0 deletions src/services/FilesSearchService.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,9 @@ export interface Thumbnail {
width: number;
height: number;
}

export interface ISiteWebInfo {
title: string,
webId: string,
siteId: string
}
4 changes: 2 additions & 2 deletions src/webparts/controlsTest/components/ControlsTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1702,8 +1702,8 @@ export default class ControlsTest extends React.Component<IControlsTestProps, IC
/>
<FilePicker
bingAPIKey="<BING API KEY>"
webAbsoluteUrl="https://aterentiev.sharepoint.com/sites/SPFxinTeamsDemo"
defaultFolderAbsolutePath={"https://aterentiev.sharepoint.com/sites/SPFxinTeamsDemo/Shared%20Documents/General"}
//webAbsoluteUrl="https://023xn.sharepoint.com/sites/test1"
//defaultFolderAbsolutePath={"https://aterentiev.sharepoint.com/sites/SPFxinTeamsDemo/Shared%20Documents/General"}
//accepts={[".gif", ".jpg", ".jpeg", ".bmp", ".dib", ".tif", ".tiff", ".ico", ".png", ".jxr", ".svg"]}
buttonLabel="Add File"
buttonIconProps={{ iconName: 'Add', styles: { root: { fontSize: 42 } } }}
Expand Down