Skip to content

Commit 826873c

Browse files
authored
Merge pull request #1081 from kadu-jr/dev
Issue #907 - Allow Site tab from FIlePicker to pick documents from a different site
2 parents 973799c + abe5ba8 commit 826873c

File tree

6 files changed

+48
-7
lines changed

6 files changed

+48
-7
lines changed

src/controls/filePicker/FilePicker.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ export class FilePicker extends React.Component<
5454
// Initialize file browser services
5555
this.fileBrowserService = new FileBrowserService(
5656
props.context,
57-
this.props.itemsCountQueryLimit
57+
this.props.itemsCountQueryLimit,
58+
this.props.webAbsoluteUrl
5859
);
5960
this.oneDriveService = new OneDriveService(
6061
props.context,
@@ -88,6 +89,12 @@ export class FilePicker extends React.Component<
8889
organisationAssetsEnabled: orgAssetsEnabled,
8990
selectedTab: this.getDefaultSelectedTabKey(this.props, orgAssetsEnabled),
9091
});
92+
if (!!this.props.context && !!this.props.webAbsoluteUrl){
93+
let webTitle = await this.fileBrowserService.getSiteTitle();
94+
this.setState({
95+
webTitle
96+
})
97+
}
9198
}
9299

93100
/**
@@ -213,6 +220,7 @@ export class FilePicker extends React.Component<
213220
fileBrowserService={this.fileBrowserService}
214221
includePageLibraries={this.props.includePageLibraries}
215222
defaultFolderAbsolutePath={this.props.defaultFolderAbsolutePath}
223+
webTitle={this.state.webTitle}
216224
{...linkTabProps}
217225
/>
218226
)}
@@ -224,6 +232,7 @@ export class FilePicker extends React.Component<
224232
key: "keyOrgAssets",
225233
}}
226234
fileBrowserService={this.orgAssetsService}
235+
webTitle={this.state.webTitle}
227236
{...linkTabProps}
228237
/>
229238
)}

src/controls/filePicker/IFilePickerProps.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,9 @@ export interface IFilePickerProps {
161161
* Specifies a default folder to be active in the Site Files tab
162162
*/
163163
defaultFolderAbsolutePath?: string;
164+
165+
/**
166+
* Specifies a default site in the Site Files tab
167+
*/
168+
webAbsoluteUrl?: string;
164169
}

src/controls/filePicker/IFilePickerState.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export interface IFilePickerState {
22
showFullNav?: boolean; // reserved for future use
33
panelOpen?: boolean;
44
selectedTab?: string;
5+
webTitle?: string;
56

67
organisationAssetsEnabled?: boolean;
78
}

src/controls/filePicker/SiteFilePickerTab/ISiteFilePickerTabProps.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ export interface ISiteFilePickerTabProps extends IFilePickerTab {
1414
* Specifies a default folder to be active in the Site Files tab
1515
*/
1616
defaultFolderAbsolutePath?: string;
17+
18+
/**
19+
* Title of the default site
20+
*/
21+
webTitle?: string;
1722

1823
includePageLibraries?: boolean;
1924
}

src/controls/filePicker/SiteFilePickerTab/SiteFilePickerTab.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default class SiteFilePickerTab extends React.Component<ISiteFilePickerTa
2626
// Add current site to the breadcrumb or the provided node
2727
const breadcrumbSiteNode: FilePickerBreadcrumbItem = this.props.breadcrumbFirstNode ? this.props.breadcrumbFirstNode : {
2828
isCurrentItem: false,
29-
text: props.context.pageContext.web.title,
29+
text: props.webTitle || props.context.pageContext.web.title,
3030
key: props.context.pageContext.web.id.toString(),
3131
};
3232
// add click event after defining breadcrumb so that it also applies to breadcrumb items passed to the component as properties

src/services/FileBrowserService.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import { GeneralHelper } from "../common/utilities/GeneralHelper";
66
export class FileBrowserService {
77
protected itemsToDownloadCount: number;
88
protected context: BaseComponentContext;
9+
protected siteAbsoluteUrl: string;
910

1011
protected driveAccessToken: string;
1112
protected mediaBaseUrl: string;
1213
protected callerStack: string;
1314

14-
constructor(context: BaseComponentContext, itemsToDownloadCount: number = 100) {
15+
constructor(context: BaseComponentContext, itemsToDownloadCount: number = 100, siteAbsoluteUrl?: string) {
1516
this.context = context;
17+
this.siteAbsoluteUrl = siteAbsoluteUrl || context.pageContext.web.absoluteUrl;
1618

1719
this.itemsToDownloadCount = itemsToDownloadCount;
1820
this.driveAccessToken = null;
@@ -27,7 +29,7 @@ export class FileBrowserService {
2729
public getListItems = async (listUrl: string, folderPath: string, acceptedFilesExtensions?: string[], nextPageQueryStringParams?: string, sortBy?: string, isDesc?: boolean): Promise<FilesQueryResult> => {
2830
let filesQueryResult: FilesQueryResult = { items: [], nextHref: null };
2931
try {
30-
let restApi = `${this.context.pageContext.web.absoluteUrl}/_api/web/GetList('${listUrl}')/RenderListDataAsStream`;
32+
let restApi = `${this.siteAbsoluteUrl}/_api/web/GetList('${listUrl}')/RenderListDataAsStream`;
3133

3234
// Do not pass FolderServerRelativeUrl as query parameter
3335
// Attach passed nextPageQueryStringParams values to REST URL
@@ -59,7 +61,7 @@ export class FileBrowserService {
5961
*/
6062
public getSiteMediaLibraries = async (includePageLibraries: boolean = false) => {
6163
try {
62-
const absoluteUrl = this.context.pageContext.web.absoluteUrl;
64+
const absoluteUrl = this.siteAbsoluteUrl;
6365
const restApi = `${absoluteUrl}/_api/SP.Web.GetDocumentAndMediaLibraries?webFullUrl='${encodeURIComponent(absoluteUrl)}'&includePageLibraries='${includePageLibraries}'`;
6466
const mediaLibrariesResult = await this.context.spHttpClient.get(restApi, SPHttpClient.configurations.v1);
6567

@@ -84,7 +86,7 @@ export class FileBrowserService {
8486
*/
8587
public getLibraryNameByInternalName = async (internalName: string): Promise<string> => {
8688
try {
87-
const absoluteUrl = this.context.pageContext.web.absoluteUrl;
89+
const absoluteUrl = this.siteAbsoluteUrl;
8890
const restApi = `${absoluteUrl}/_api/web/GetFolderByServerRelativeUrl('${internalName}')/Properties?$select=vti_x005f_listtitle`;
8991
const libraryResult = await this.context.spHttpClient.get(restApi, SPHttpClient.configurations.v1);
9092

@@ -155,6 +157,25 @@ export class FileBrowserService {
155157
return fieldName;
156158
}
157159

160+
/**
161+
* Gets the Title of the current Web
162+
* @returns SharePoint Site Title
163+
*/
164+
public getSiteTitle = async (): Promise<string> => {
165+
const restApi = `${this.siteAbsoluteUrl}/_api/web?$select=Title`;
166+
const webResult = await this.context.spHttpClient.get(restApi, SPHttpClient.configurations.v1);
167+
168+
if (!webResult || !webResult.ok) {
169+
throw new Error(`Something went wrong when executing request. Status='${webResult.status}'`);
170+
}
171+
if (!webResult || !webResult) {
172+
throw new Error(`Cannot read data from the results.`);
173+
}
174+
let webJson = await webResult.json();
175+
return webJson.Title;
176+
177+
}
178+
158179
/**
159180
* Executes query to load files with possible extension filtering
160181
* @param restApi
@@ -325,7 +346,7 @@ export class FileBrowserService {
325346
* Creates an absolute URL
326347
*/
327348
protected buildAbsoluteUrl = (relativeUrl: string) => {
328-
const siteUrl: string = GeneralHelper.getAbsoluteDomainUrl(this.context.pageContext.web.absoluteUrl);
349+
const siteUrl: string = GeneralHelper.getAbsoluteDomainUrl(this.siteAbsoluteUrl);
329350
return `${siteUrl}${relativeUrl.indexOf('/') === 0 ? '' : '/'}${relativeUrl}`;
330351
}
331352

0 commit comments

Comments
 (0)