Forum Discussion
How to filter a spPnP people picker
LeonPavesic Hi,
I've just tried adding that and it's showing as:
I'm running:
├── @fluentui/react-components@9.31.0
├── @microsoft/teamsfx-cli@1.2.6
├── @pnp/logging@3.18.0
├── @pnp/sp@3.18.0
├── @pnp/spfx-controls-react@3.15.0
└── react-icons@4.11.0
Hi CardinalNight.,
you're defenitely facing some issues with the code I provided earlier. The code I shared was a general example of how to create a custom people picker filter. However, the specific version of the @pnp/spfx-controls-react library you're using might have some differences in how filters are applied.
To filter a PeoplePicker based on a particular domain, you can try the following code:
import { PeoplePicker, PrincipalType } from '@pnp/spfx-controls-react/lib/PeoplePicker';
import { BaseComponent } from '@microsoft/sp-component-base';
export class YourComponent extends BaseComponent<{}, {}> {
constructor(props: {}) {
super(props);
}
render() {
return (
<PeoplePicker
context={this.context}
personSelectionLimit={1}
principalTypes={[PrincipalType.User]}
resolveDelay={1000}
ensureUser={true}
defaultSelectedUsers={[]}
onChange={(items) => this.handlePeoplePickerChange(items)}
onResolveSuggestions={(filterText, currentPersonas) => this.filterPeople(filterText, currentPersonas)}
/>
);
}
handlePeoplePickerChange = (items: any[]) => {
// Handle the selected items here
}
filterPeople = (filterText: string, currentPersonas: any[]): Promise<any[]> => {
// Implement your custom filtering logic here
// You can use the filterText and currentPersonas to filter the results
// Return a Promise with the filtered results
const filteredResults = /* Your filtering logic here */;
return Promise.resolve(filteredResults);
}
}
Remember to adjust the code according to your unique needs and the version of the library you're working with, as there may be some differences in the library's API across versions.
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.
If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic
(LinkedIn)
- CardinalNightSep 27, 2023Copper Contributor
LeonPavesic Hi Leon,
I very much appreciate your help.
I've installed many different versions of
@pnp/spfx-controls-react
but non seem to have:
onResolveSuggestions
onResolveSuggestions shows:
Property 'onResolveSuggestions' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<PeoplePicker> & Readonly<IPeoplePickerProps> & Readonly<...>'.ts(2322)
No versions seems to have PeoplePickerFilter for that matter.
Can you tell me which version you are using?
- LeonPavesicSep 27, 2023Silver Contributor
Hi CardinalNight,
The code I provided in my previous response was for version 4.0.0 of the @pnp/spfx-controls-react library.
The onResolveSuggestions prop was introduced in version 4.0.0 of the library, and the PeoplePickerFilter class was introduced in version 3.0.0 of the library.
If you are using a version of the library that is older than 4.0.0, you will not be able to use the onResolveSuggestions prop or the PeoplePickerFilter class.
To filter a PeoplePicker by a certain domain using an older version of the library, you can use the following code:
import { PeoplePicker, PrincipalType } from '@pnp/spfx-controls-react'; class YourComponent extends React.Component { constructor(props) { super(props); this.filterPeople = this.filterPeople.bind(this); } render() { return ( <PeoplePicker context={this.context} personSelectionLimit={1} principalTypes={[PrincipalType.User]} resolveDelay={1000} ensureUser={true} defaultSelectedUsers={[]} onChange={(items) => this.handlePeoplePickerChange(items)} > <PeoplePickerFilter> {(filterText, currentPersonas) => this.filterPeople(filterText, currentPersonas)} </PeoplePickerFilter> </PeoplePicker> ); } handlePeoplePickerChange(items) { // Handle the selected items here } filterPeople(filterText, currentPersonas) { // Implement your custom filtering logic here // You can use the filterText and currentPersonas to filter the results // Return an array of the filtered results const filteredResults = currentPersonas.filter((persona) => { const domain = '@domain.com'; return persona.email.endsWith(domain); }); return filteredResults; } }
This code will filter the people picker results to only include users whose email addresses end in @domain.com.
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic (LinkedIn)
- arafhNov 09, 2023Copper Contributor
I installed @pnp/spfx-controls-react version 4.0.0-beta.6059091 and I also am not seeing the onResolveSuggestions property or PeoplePickerFilter class. Where did you get that code from?