Skip to content

Commit 06eea89

Browse files
committed
moving method to get web info into SPSitesService
1 parent eb47604 commit 06eea89

File tree

5 files changed

+55
-53
lines changed

5 files changed

+55
-53
lines changed

src/controls/sitePicker/ISitePicker.ts

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,5 @@
11
import { BaseComponentContext } from '@microsoft/sp-component-base';
2-
3-
export interface ISite {
4-
/**
5-
* ID of the site
6-
*/
7-
id?: string;
8-
/**
9-
* Title
10-
*/
11-
title?: string;
12-
/**
13-
* Base URL
14-
*/
15-
url?: string;
16-
17-
/**
18-
* ID of the web
19-
*/
20-
webId?: string;
21-
22-
/**
23-
* ID of the hub site
24-
*/
25-
hubSiteId?: string;
26-
}
2+
import { ISite } from '../../services/SPSitesService';
273

284
export interface ISitePickerProps {
295
/**

src/controls/sitePicker/SitePicker.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import * as React from 'react';
1010

1111
import * as telemetry from '../../common/telemetry';
1212
import { toRelativeUrl } from '../../common/utilities/GeneralHelper';
13-
import { getAllSites, getHubSites } from '../../services/SPSitesService';
14-
import { ISite, ISitePickerProps } from './ISitePicker';
13+
import { getAllSites, getHubSites, ISite } from '../../services/SPSitesService';
14+
import { ISitePickerProps } from './ISitePicker';
1515

1616
const styles = mergeStyleSets({
1717
loadingSpinnerContainer: {

src/services/FilesSearchService.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { BaseComponentContext } from '@microsoft/sp-component-base';
22
import { SPHttpClient, HttpClientResponse } from "@microsoft/sp-http";
3-
import { ISearchResult, BingQuerySearchParams, IRecentFile, ISiteWebInfo } from "./FilesSearchService.types";
3+
import { ISearchResult, BingQuerySearchParams, IRecentFile } 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';
7+
import { getSiteWebInfo } from './SPSitesService';
78

89
/**
910
* Maximum file size when searching
@@ -56,7 +57,7 @@ export class FilesSearchService {
5657
let webId = this.context.pageContext.web.id.toString();
5758
let siteId = this.context.pageContext.site.id.toString();
5859
if (this.siteAbsoluteUrl !== this.context.pageContext.web.absoluteUrl) {
59-
const siteinfo = await this.getSiteInfos(this.siteAbsoluteUrl);
60+
const siteinfo = await getSiteWebInfo(this.context, this.siteAbsoluteUrl);
6061
webId = siteinfo.webId;
6162
siteId = siteinfo.siteId;
6263
}
@@ -296,21 +297,4 @@ export class FilesSearchService {
296297
const splitUrl = url.split('/');
297298
return splitUrl[0];
298299
}
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-
}
316300
}

src/services/FilesSearchService.types.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,3 @@ 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/services/SPSitesService.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
11
import { BaseComponentContext } from '@microsoft/sp-component-base';
2-
import { ISite } from '../controls/sitePicker/ISitePicker';
32
import { SPHttpClient } from '@microsoft/sp-http';
43

4+
export interface ISite {
5+
/**
6+
* ID of the site
7+
*/
8+
id?: string;
9+
/**
10+
* Title
11+
*/
12+
title?: string;
13+
/**
14+
* Base URL
15+
*/
16+
url?: string;
17+
18+
/**
19+
* ID of the web
20+
*/
21+
webId?: string;
22+
23+
/**
24+
* ID of the hub site
25+
*/
26+
hubSiteId?: string;
27+
}
28+
29+
export interface ISiteWebInfo {
30+
title: string,
31+
webId: string,
32+
siteId: string
33+
}
34+
535
const getAllSitesInternal = async (ctx: BaseComponentContext, queryText: string, trimDuplicates: boolean): Promise<ISite[]> => {
636
let startRow = 0;
737
const rowLimit = 500;
@@ -113,3 +143,21 @@ export const getHubSites = async (ctx: BaseComponentContext): Promise<ISite[]> =
113143

114144
return hubSites;
115145
};
146+
147+
export const getSiteWebInfo = async (ctx: BaseComponentContext, webUrl: string): Promise<ISiteWebInfo> => {
148+
const webInfo = await ctx.spHttpClient.get(`${webUrl}/_api/web?$select=id,Title`, SPHttpClient.configurations.v1);
149+
if (!webInfo || !webInfo.ok) {
150+
throw new Error(`[FileBrowser.getWebInfo]: Something went wrong when executing request. Status='${webInfo.statusText}'`);
151+
}
152+
const siteInfo = await ctx.spHttpClient.get(`${webUrl}/_api/site?$select=id`, SPHttpClient.configurations.v1);
153+
if (!siteInfo || !siteInfo.ok) {
154+
throw new Error(`[FileBrowser.getWebInfo]: Something went wrong when executing request. Status='${webInfo.statusText}'`);
155+
}
156+
const webInfoResult = await webInfo.json();
157+
const siteInfoResult = await siteInfo.json();
158+
return {
159+
title: webInfoResult.Title,
160+
webId: webInfoResult.Id,
161+
siteId: siteInfoResult.Id
162+
};
163+
}

0 commit comments

Comments
 (0)