I'm working on a SharePoint Webpart and I use the People Picker Field, to get the names of people to my List. People Picker works fine, i get the name, I can push all my items to my List, except the items from People Picker field. When i enter some name, I get the name on the console in Chrome/EDGE/Firefox, but the Array stays 0. And here ist my #1 problem: [[Prototype]]: Array(0)
Here is my code
import { PeoplePicker } from "@pnp/spfx-controls-react/lib/PeoplePicker"
export default class Test extends React.Component<
ITestProps,
IPeoplePickerState,
{}
> {
constructor(props: ITestProps, state: IPeoplePickerState){
super(props);
this.addListItem = this.addListItem.bind(this);
this.getPeoplePicker = this.getPeoplePicker.bind(this);
this.state = { user:[] };
}
public render(): React.ReactElement<ITestProps> {
return (
<section>
<div>
<PeoplePicker context={this.props.context} data-automation-id="addSelectedUsers" placeholder=" Name:" personSelectionLimit={3} ensureUser={true} onChange={this. getPeoplePicker}></PeoplePicker><br></br>
</div>
<div>
<DefaultButton className={styles.btn1} id='btn' onClick={this.addListItem} > Eintragen </DefaultButton>
</div>
</section>
);
}
public getPeoplePicker(items: any[]){
console.log('Items:', items);
let selectedUsers: any[] = [];
items.map((item) => {
selectedUsers.push(item.id);
});
this.setState({user: selectedUsers});
this.setState({user: selectedUsers});
}
public addListItem(): void {
pnp.sp.web.lists.getByTitle("Test123")
.items.add({
'Person': { results: this.state.user },
})
.then(i => console.log(i))
.then(()=> alert("Vielen Dank!"));
}
}
I tried 2 solutions for "getPeoplePicker()", but no one works right.
public getPeoplePicker(items: any[]){
console.log('Items:', items);
let selectedUsers: any[] = [];
items.map((item) => {
selectedUsers.push(item.id);
});
this.setState({user: selectedUsers});
}
and
public getPeoplePicker(items: any[]){
console.log('Items:', items);
let selectedUsers = [];
for(let item in items){
selectedUsers.push(items[item].id);
}
this.setState({user: selectedUsers});
}
I get always the same alert.