Forum Discussion

Cardinal_Night's avatar
Cardinal_Night
Brass Contributor
Oct 16, 2020

How to use @pnp/sp to query a person column in an SP list to get the name

I have a web part that which needs to retrieve the Name property of a person column (a people picker) so I can populate state with it and subsequently populate a field. Here's the function that queries the item:

 

private _jeChange = (ev: React.FormEvent<HTMLDivElement>, option?: IDropdownOption, index?: number) => {
    this.setState({ 
      SelectedJE: option.text,
    
    }, () => { 
      const selJE = this.state.SelectedJE;
    
    if (selJE && selJE.length > 0) {
      
      let _item = this.state.MyListItems.find((item) => {
        return item.JobRef == selJE;
      });
      this.setState({
 
        JEDeptContact: _item.DeptContactId,

   }, () => {
     sp.web.lists.getByTitle("MyList").items.getById(_item.Id).select("DeptContact", "Lookup/Name", "Lookup/ID").expand("Lookup").get().then((item: any[]) => {
          console.log(item);
        });
      
        });
      }
    });
  }

 The _item.DeptContactId successfully populates the state with the Id of the user in the person column, but I want the Name not the Id, how would I resolve the Id to the Name? Do I need to use expand, if so how?

No RepliesBe the first to reply

Resources