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

Added 'associatedsites' and 'hubsiteId' parameters to SitePicker to have option to show only sites associated with the hub. + infinite loop prevention when fetching sites #1357

Merged
merged 2 commits into from
Nov 15, 2022
Merged
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
21 changes: 21 additions & 0 deletions src/common/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { SPHttpClient } from '@microsoft/sp-http';
import { PageContext, SPField } from '@microsoft/sp-page-context';
import { ListViewAccessor } from "@microsoft/sp-listview-extensibility";
import { ISPField } from './SPEntities';
import { INavNodeInfo } from '@pnp/sp/navigation/types';

/**
* Customizer context interface.
Expand All @@ -27,3 +28,23 @@ export interface IFields {
export interface IProps {
context: IContext;
}

export interface IHubSiteData {
headerEmphasis: string;
hideNameInNavigation: boolean;
isNavAudienceTargeted: boolean;
isSameTenantInstance: boolean;
logoFileHash: number;
logoUrl: string;
megaMenuEnabled: boolean;
name: string;
navigation: INavNodeInfo[];
parentHubSiteId: string;
relatedHubSiteIds: string[];
requiresJoinApproval: boolean;
siteDesignId: string;
tenantInstanceId: string;
themeKey: string;
url: string;
usesMetadataNavigation: boolean;
}
11 changes: 9 additions & 2 deletions src/controls/sitePicker/ISitePicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export interface ISitePickerProps {
*/
multiSelect?: boolean;
/**
* Defines what entities are available for selection: site collections, sites, hub sites.
* Defines what entities are available for selection: site collections, sites, hub sites, sites inside hub.
*/
mode?: 'site' | 'web' | 'hub';
mode?: 'site' | 'web' | 'hub' | 'associatedsites';

/**
* Specifies if the options should be limited by the current site collections. Taken into consideration if selectionMode is set to 'web'
Expand Down Expand Up @@ -88,4 +88,11 @@ export interface ISitePickerProps {
* Applicable if mode is set to site or web.
*/
additionalQuery?: string;

/**
* If provided will be used in the search for all sites in a hub.
* Applicable if mode is set to associatedsites.
* If mode is set to associatedsites and no hubsiteId is provided, the current site's hub ID will be used.
*/
hubsiteId?: string;
}
22 changes: 15 additions & 7 deletions src/controls/sitePicker/SitePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as React from 'react';

import * as telemetry from '../../common/telemetry';
import { toRelativeUrl } from '../../common/utilities/GeneralHelper';
import { getAllSites, getHubSites, ISite } from '../../services/SPSitesService';
import { getAllSites, getHubSites, ISite, getAssociatedSites } from '../../services/SPSitesService';
import { ISitePickerProps } from './ISitePicker';

const styles = mergeStyleSets({
Expand Down Expand Up @@ -73,7 +73,8 @@ export const SitePicker: React.FunctionComponent<ISitePickerProps> = (props: Rea
className,
selectedSites,
trimDuplicates,
additionalQuery
additionalQuery,
hubsiteId
} = props;

const [isLoading, setIsLoading] = React.useState<boolean>();
Expand Down Expand Up @@ -233,11 +234,18 @@ export const SitePicker: React.FunctionComponent<ISitePickerProps> = (props: Rea
setFilteredSites([]);

let promise: Promise<ISite[]>;
if (mode === 'hub') {
promise = getHubSites(context);
}
else {
promise = getAllSites(context, mode !== 'site', limitToCurrentSiteCollection, trimDuplicates === true, additionalQuery);
switch (mode) {
case 'hub':
promise = getHubSites(context);
break;

case 'associatedsites':
promise = getAssociatedSites(context, trimDuplicates === true, hubsiteId);
break;

default:
promise = getAllSites(context, mode !== 'site', limitToCurrentSiteCollection, trimDuplicates === true, additionalQuery);
break;
}

promise.then(newSites => {
Expand Down
27 changes: 26 additions & 1 deletion src/services/SPSitesService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BaseComponentContext } from '@microsoft/sp-component-base';
import { SPHttpClient } from '@microsoft/sp-http';
import { IHubSiteData } from '../common/Interfaces';

export interface ISite {
/**
Expand Down Expand Up @@ -36,6 +37,7 @@ const getAllSitesInternal = async (ctx: BaseComponentContext, queryText: string,
let startRow = 0;
const rowLimit = 500;
let totalRows = 0;
let currentRows = 0;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const values: any[] = [];

Expand Down Expand Up @@ -69,8 +71,9 @@ const getAllSitesInternal = async (ctx: BaseComponentContext, queryText: string,
values.push(...relevantResults.Table.Rows);
totalRows = relevantResults.TotalRows;
startRow += rowLimit;
currentRows = relevantResults.Table.Rows?.length;

} while (values.length < totalRows);
} while (values.length < totalRows && currentRows !== 0);

// Do the call against the SP REST API search endpoint

Expand Down Expand Up @@ -161,3 +164,25 @@ export const getSiteWebInfo = async (ctx: BaseComponentContext, webUrl: string):
siteId: siteInfoResult.Id
};
}

export const getAssociatedSites = async (ctx: BaseComponentContext, trimDuplicates: boolean, hubSiteId?: string): Promise<ISite[]> => {
if (!hubSiteId){

const requestUrl = `${ctx.pageContext.site.absoluteUrl}/_api/web/HubsiteData`;
const response = await ctx.spHttpClient.get(requestUrl, SPHttpClient.configurations.v1);
const json = await response.json();

const hubsiteData: IHubSiteData = JSON.parse(json.value);

if (hubsiteData === null)
return [];

hubSiteId = hubsiteData.relatedHubSiteIds[0];

}

const queryText = `(contentclass:STS_Site DepartmentId:${hubSiteId})`;


return getAllSitesInternal(ctx, queryText, trimDuplicates);
};