Skip to content

Commit

Permalink
Merge pull request #1081 from kadu-jr/dev
Browse files Browse the repository at this point in the history
Issue #907 - Allow Site tab from FIlePicker to pick documents from a different site
  • Loading branch information
AJIXuMuK authored Jan 8, 2022
2 parents 973799c + abe5ba8 commit 826873c
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 7 deletions.
11 changes: 10 additions & 1 deletion src/controls/filePicker/FilePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export class FilePicker extends React.Component<
// Initialize file browser services
this.fileBrowserService = new FileBrowserService(
props.context,
this.props.itemsCountQueryLimit
this.props.itemsCountQueryLimit,
this.props.webAbsoluteUrl
);
this.oneDriveService = new OneDriveService(
props.context,
Expand Down Expand Up @@ -88,6 +89,12 @@ export class FilePicker extends React.Component<
organisationAssetsEnabled: orgAssetsEnabled,
selectedTab: this.getDefaultSelectedTabKey(this.props, orgAssetsEnabled),
});
if (!!this.props.context && !!this.props.webAbsoluteUrl){
let webTitle = await this.fileBrowserService.getSiteTitle();
this.setState({
webTitle
})
}
}

/**
Expand Down Expand Up @@ -213,6 +220,7 @@ export class FilePicker extends React.Component<
fileBrowserService={this.fileBrowserService}
includePageLibraries={this.props.includePageLibraries}
defaultFolderAbsolutePath={this.props.defaultFolderAbsolutePath}
webTitle={this.state.webTitle}
{...linkTabProps}
/>
)}
Expand All @@ -224,6 +232,7 @@ export class FilePicker extends React.Component<
key: "keyOrgAssets",
}}
fileBrowserService={this.orgAssetsService}
webTitle={this.state.webTitle}
{...linkTabProps}
/>
)}
Expand Down
5 changes: 5 additions & 0 deletions src/controls/filePicker/IFilePickerProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,9 @@ export interface IFilePickerProps {
* Specifies a default folder to be active in the Site Files tab
*/
defaultFolderAbsolutePath?: string;

/**
* Specifies a default site in the Site Files tab
*/
webAbsoluteUrl?: string;
}
1 change: 1 addition & 0 deletions src/controls/filePicker/IFilePickerState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface IFilePickerState {
showFullNav?: boolean; // reserved for future use
panelOpen?: boolean;
selectedTab?: string;
webTitle?: string;

organisationAssetsEnabled?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export interface ISiteFilePickerTabProps extends IFilePickerTab {
* Specifies a default folder to be active in the Site Files tab
*/
defaultFolderAbsolutePath?: string;

/**
* Title of the default site
*/
webTitle?: string;

includePageLibraries?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default class SiteFilePickerTab extends React.Component<ISiteFilePickerTa
// Add current site to the breadcrumb or the provided node
const breadcrumbSiteNode: FilePickerBreadcrumbItem = this.props.breadcrumbFirstNode ? this.props.breadcrumbFirstNode : {
isCurrentItem: false,
text: props.context.pageContext.web.title,
text: props.webTitle || props.context.pageContext.web.title,
key: props.context.pageContext.web.id.toString(),
};
// add click event after defining breadcrumb so that it also applies to breadcrumb items passed to the component as properties
Expand Down
31 changes: 26 additions & 5 deletions src/services/FileBrowserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import { GeneralHelper } from "../common/utilities/GeneralHelper";
export class FileBrowserService {
protected itemsToDownloadCount: number;
protected context: BaseComponentContext;
protected siteAbsoluteUrl: string;

protected driveAccessToken: string;
protected mediaBaseUrl: string;
protected callerStack: string;

constructor(context: BaseComponentContext, itemsToDownloadCount: number = 100) {
constructor(context: BaseComponentContext, itemsToDownloadCount: number = 100, siteAbsoluteUrl?: string) {
this.context = context;
this.siteAbsoluteUrl = siteAbsoluteUrl || context.pageContext.web.absoluteUrl;

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

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

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

Expand Down Expand Up @@ -155,6 +157,25 @@ export class FileBrowserService {
return fieldName;
}

/**
* Gets the Title of the current Web
* @returns SharePoint Site Title
*/
public getSiteTitle = async (): Promise<string> => {
const restApi = `${this.siteAbsoluteUrl}/_api/web?$select=Title`;
const webResult = await this.context.spHttpClient.get(restApi, SPHttpClient.configurations.v1);

if (!webResult || !webResult.ok) {
throw new Error(`Something went wrong when executing request. Status='${webResult.status}'`);
}
if (!webResult || !webResult) {
throw new Error(`Cannot read data from the results.`);
}
let webJson = await webResult.json();
return webJson.Title;

}

/**
* Executes query to load files with possible extension filtering
* @param restApi
Expand Down Expand Up @@ -325,7 +346,7 @@ export class FileBrowserService {
* Creates an absolute URL
*/
protected buildAbsoluteUrl = (relativeUrl: string) => {
const siteUrl: string = GeneralHelper.getAbsoluteDomainUrl(this.context.pageContext.web.absoluteUrl);
const siteUrl: string = GeneralHelper.getAbsoluteDomainUrl(this.siteAbsoluteUrl);
return `${siteUrl}${relativeUrl.indexOf('/') === 0 ? '' : '/'}${relativeUrl}`;
}

Expand Down

0 comments on commit 826873c

Please sign in to comment.