From 4abed31c49bd65da46d0d989e15b6d34e2cf2883 Mon Sep 17 00:00:00 2001 From: Gautam Sheth Date: Fri, 21 Sep 2018 15:24:05 +0530 Subject: [PATCH] PeoplePicker - Added REST API filter for principaltype and hiddenInUI and no metadata filter to reduce payload --- .../peoplepicker/PeoplePickerComponent.tsx | 156 ++++++++++-------- 1 file changed, 88 insertions(+), 68 deletions(-) diff --git a/src/controls/peoplepicker/PeoplePickerComponent.tsx b/src/controls/peoplepicker/PeoplePickerComponent.tsx index 38b7d29d5..da5049d01 100644 --- a/src/controls/peoplepicker/PeoplePickerComponent.tsx +++ b/src/controls/peoplepicker/PeoplePickerComponent.tsx @@ -44,8 +44,8 @@ export class PeoplePicker extends React.Component { - var stringVal = ""; + var stringVal: string = ""; + + let filtered: boolean = false; + if (this.props.groupName) { stringVal = `/_api/web/sitegroups/GetByName('${this.props.groupName}')/users`; } else { stringVal = "/_api/web/siteusers"; } + // filter for principal Type + var filterVal: string = ""; + if (this.props.principleTypes) { + filterVal = `${"?$filter="}${this.props.principleTypes.map(principalType => `(PrincipalType eq ${principalType})`).join(" or ")}`; + filtered = true; + } + + // filter for showHiddenInUI + if (this.props.showHiddenInUI) { + filterVal = filtered ? `${filterVal} and (IsHiddenInUI eq ${this.props.showHiddenInUI})` : `?$filter=IsHiddenInUI eq ${this.props.showHiddenInUI}`; + filtered = true; + } + const webAbsoluteUrl = this.props.webAbsoluteUrl || this.props.context.pageContext.web.absoluteUrl; // Create the rest API - const restApi = `${webAbsoluteUrl}${stringVal}`; + const restApi = filtered ? `${webAbsoluteUrl}${stringVal}${filterVal}` : `${webAbsoluteUrl}${stringVal}`; try { // Call the API endpoint - const items: IUsers = await this.props.context.spHttpClient.get(restApi, SPHttpClient.configurations.v1).then(resp => resp.json()); + const items: IUsers = await this.props.context.spHttpClient.get(restApi, SPHttpClient.configurations.v1, { + headers: { + 'Accept': 'application/json;odata.metadata=none' + } + }).then(resp => resp.json()); // Check if items were retrieved if (items && items.value && items.value.length > 0) { @@ -160,7 +180,7 @@ export class PeoplePicker extends React.Component - this._doesTextStartWith(item.text as string, filterText) - || this._doesTextContains(item.text as string, filterText) - || this._doesTextStartWith(item.secondaryText as string, filterText) - || this._doesTextContains(item.secondaryText as string, filterText)); + this._doesTextStartWith(item.text as string, filterText) + || this._doesTextContains(item.text as string, filterText) + || this._doesTextStartWith(item.secondaryText as string, filterText) + || this._doesTextContains(item.secondaryText as string, filterText)); } @@ -302,12 +322,12 @@ export class PeoplePicker extends React.Component 0; } @@ -332,23 +352,23 @@ export class PeoplePicker extends React.Component{this.props.titleText || strings.peoplePickerComponentTitleText} peoplePersonaMenu.text} - className={`'ms-PeoplePicker' ${this.props.peoplePickerCntrlclassName ? this.props.peoplePickerCntrlclassName : ''}`} - key={'normal'} - onValidateInput={this._validateInputPeople} - removeButtonAriaLabel={'Remove'} - inputProps={{ - 'aria-label': 'People Picker' - }} - selectedItems={this.state.selectedPersons} - itemLimit={this.props.personSelectionLimit || 1} - disabled={this.props.disabled} - onChange={this._onPersonItemsChange} /> + onResolveSuggestions={this._onPersonFilterChanged} + onEmptyInputFocus={this._returnMostRecentlyUsedPerson} + getTextFromItem={(peoplePersonaMenu: IPersonaProps) => peoplePersonaMenu.text} + className={`'ms-PeoplePicker' ${this.props.peoplePickerCntrlclassName ? this.props.peoplePickerCntrlclassName : ''}`} + key={'normal'} + onValidateInput={this._validateInputPeople} + removeButtonAriaLabel={'Remove'} + inputProps={{ + 'aria-label': 'People Picker' + }} + selectedItems={this.state.selectedPersons} + itemLimit={this.props.personSelectionLimit || 1} + disabled={this.props.disabled} + onChange={this._onPersonItemsChange} /> ); return (
- { - this.props.showtooltip ? ( - - {peoplepicker} - - ) : ( -
- {peoplepicker} -
- ) - } + { + this.props.showtooltip ? ( + + {peoplepicker} + + ) : ( +
+ {peoplepicker} +
+ ) + } - { - (this.props.isRequired && this.state.showmessageerror) && ( - - {this.props.errorMessage ? this.props.errorMessage : strings.peoplePickerComponentErrorMessage} - - ) - } + { + (this.props.isRequired && this.state.showmessageerror) && ( + + {this.props.errorMessage ? this.props.errorMessage : strings.peoplePickerComponentErrorMessage} + + ) + }
); }