Skip to content

Commit 1cd6499

Browse files
authored
Merge pull request pnp#1258 from PooLP/dev
PR : correction for Bing search image api endpoint
2 parents 2612e46 + 93546a7 commit 1cd6499

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

docs/documentation/docs/controls/FilePicker.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ The FilePicker component can be configured with the following properties:
8383
| context | BaseComponentContext | yes | Current context. |
8484
| accepts | string[] | no | Array of strings containing allowed files extensions. E.g. [".gif", ".jpg", ".jpeg", ".bmp", ".dib", ".tif", ".tiff", ".ico", ".png", ".jxr", ".svg"] |
8585
| required | boolean | no | Sets the label to inform that the value is required. |
86-
| bingAPIKey | string | no | Used to execute WebSearch. If not provided SearchTab will not be available. |
86+
| bingAPIKey | string | no | Used to execute WebSearch. If not provided SearchTab will not be available. The API key can be created on a Azure account ([Bing image search API](https://www.microsoft.com/en-us/bing/apis/bing-image-search-api)), a free version exist for 1000 query per month ([Pricing](https://www.microsoft.com/en-us/bing/apis/pricing)) |
8787
| disabled | boolean | no | Specifies if the picker button is disabled |
8888
| hidden | boolean | no | Specifies if the picker button is hidden (if hidden, panel visibility can still be controlled with isPanelOpen) |
8989
| itemsCountQueryLimit | number | no | Number of items to obtain when executing REST queries. Default 100. |

src/common/utilities/GeneralHelper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,8 @@ export const toRelativeUrl = (absoluteUrl: string): string => {
367367
if (!absoluteUrl) {
368368
return '';
369369
}
370-
371-
return absoluteUrl.replace(/^(?:\/\/|[^/]+)*\//, '/');
370+
const relativeUrl: string = new URL(absoluteUrl).pathname;
371+
return relativeUrl;
372372
};
373373

374374
export function sortString(a: string, b: string, isDesc: boolean): number {

src/services/FilesSearchService.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BaseComponentContext } from '@microsoft/sp-component-base';
2-
import { SPHttpClient } from "@microsoft/sp-http";
2+
import { SPHttpClient, HttpClientResponse } from "@microsoft/sp-http";
33
import { ISearchResult, BingQuerySearchParams, IRecentFile } from "./FilesSearchService.types";
44
import { find } from "office-ui-fabric-react/lib/Utilities";
55
import { GeneralHelper } from "../common/utilities/GeneralHelper";
@@ -143,15 +143,18 @@ export class FilesSearchService {
143143
}
144144

145145
// Submit the request
146-
const apiUrl: string = `https://www.bingapis.com/api/v7/images/search?appid=${this.bingAPIKey}&traffictype=Internal_monitor&q=${encodeURIComponent(query)}&count=${maxResults}&aspect=${aspect}&maxFileSize=${maxFileSize}&size=${size}&mkt=en-US&license=${license}`;
146+
const apiUrl: string = `https://api.bing.microsoft.com/v7.0/images/search?q=${encodeURIComponent(query)}&count=${maxResults}&aspect=${aspect}&maxFileSize=${maxFileSize}&size=${size}&mkt=en-US&license=${license}`;
147147

148-
const searchDataResponse: any = await this.context.httpClient.get(apiUrl, SPHttpClient.configurations.v1, {
148+
const searchDataResponse: HttpClientResponse = await this.context.httpClient.get(apiUrl, SPHttpClient.configurations.v1, {
149149
method: 'GET',
150-
mode: 'cors'
150+
mode: 'cors',
151+
headers: {
152+
'Ocp-Apim-Subscription-Key': this.bingAPIKey,
153+
}
151154
});
152155

153156
if (!searchDataResponse || !searchDataResponse.ok) {
154-
throw new Error(`Something went wrong when executing search query. Status='${searchDataResponse.statusMessage}'`);
157+
throw new Error(`Something went wrong when executing search query. Status='${searchDataResponse.statusText}'`);
155158
}
156159
const searchData = await searchDataResponse.json();
157160
if (!searchData || !searchData.value) {

src/services/OrgAssetsService.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,27 @@ export class OrgAssetsService extends FileBrowserService {
1313
public getListItems = async (listUrl: string, folderPath: string, acceptedFilesExtensions?: string[], nextPageQueryStringParams?: string): Promise<FilesQueryResult> => {
1414
let filesQueryResult: FilesQueryResult = { items: [], nextHref: null };
1515
try {
16+
1617
// Retrieve Lib path from folder path
17-
if (folderPath.charAt(0) !== "/") {
18-
folderPath = `/${folderPath}`;
18+
const isRootSite = this.context.pageContext.site.serverRelativeUrl === '/';
19+
20+
if (!isRootSite) {
21+
if (folderPath.charAt(0) !== '/') {
22+
folderPath = `/${folderPath}`;
23+
}
24+
25+
} else {
26+
if (folderPath.charAt(0) === '/') {
27+
folderPath = folderPath.substring(1);
28+
}
1929
}
30+
2031
// Remove all the rest of the folder path
21-
let libName = folderPath.replace(`${this._orgAssetsLibraryServerRelativeSiteUrl}/`, "");
22-
libName = libName.split("/")[0];
23-
// Buil absolute library URL
24-
const libFullUrl = this.buildAbsoluteUrl(`${this._orgAssetsLibraryServerRelativeSiteUrl}/${libName}`);
32+
let libName = folderPath.replace(`${this._orgAssetsLibraryServerRelativeSiteUrl}/`, '');
33+
libName = libName.split('/')[0];
34+
35+
// Build absolute library URL
36+
const libFullUrl = this.buildAbsoluteUrl(!isRootSite ? `${this._orgAssetsLibraryServerRelativeSiteUrl}/${libName}` : `${this._orgAssetsLibraryServerRelativeSiteUrl}${libName}`);
2537

2638
let queryStringParams: string = "";
2739
// Do not pass FolderServerRelativeUrl as query parameter

0 commit comments

Comments
 (0)