如何使用@ pnp / sp查询SP列表中的人员列以获取名称

时间:2020-10-16 09:36:38

标签: spfx

我有一个Web部件,该Web部件需要检索人员列(人员选择器)的Name属性,以便可以用它填充状态并随后填充字段。这是查询项目的函数:

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);
        });
      
        });
      }
    });
  }

_item.DeptContactId成功在状态列中使用用户的ID填充了状态,但是我希望使用Name而不是Id,如何将ID解析为Name?我需要使用expand来获取名称吗?如果可以,怎么办? 我已经读过这篇文章,但不知道在哪里使用扩展: https://pnp.github.io/pnpjs/sp/items/

1 个答案:

答案 0 :(得分:0)

找到了:

if(_item.DeptContactId){
  sp.web.getUserById(_item.DeptContactId).select("Title", "DeptContact/Title").expand("DeptContact").get().then(r => {
      this.setState({
         JEDeptContact: r.Title,
      });
    });