SPFx 如何在列表中设置人员字段,例如"convert" AccountName 到查找 ID?

SPFx how to set a people field in a list, e.g. "convert" AccountName to Lookup ID?

我正在尝试使用 React Office UI Fabric 构建 SPFX WebPart。我有一个 SharePoint 列表,其中包含一个名为 AssignedTo 的人员字段。 ID 值类似于 12、20 等。当我 select 一个人使用 PeoplePicker 时,我得到 AccountName 喜欢 i:0#.f|membership|bob@somewhere.org.nz 和 GUID 喜欢 ff53b41e-d11a-435c-9cd1-d708e71ee7c7.

如何"convert"从PeoplePicker到ID的信息,列表中的People Field正在使用?

PeoplePicker 按如下方式检索人员:

private searchPeople(terms: string, results: IPersonaProps[]): IPersonaProps[] | Promise<IPersonaProps[]> {
  return new Promise<IPersonaProps[]>((resolve, reject) =>
  this.props.spHttpClient.get(`${this.props.siteUrl}/_api/search/query?querytext='*${terms}*'&rowlimit=20&sourceid='b09a7990-05ea-4af9-81ef-edfab16c4e31'`,
    SPHttpClient.configurations.v1,
    {
      headers: {
        'Accept': 'application/json;odata=nometadata',
        'odata-version': ''
      }
    })
    .then((response: SPHttpClientResponse): Promise<{ PrimaryQueryResult: IPeopleDataResult }> => {
      return response.json();
    })
    .then((response: { PrimaryQueryResult: IPeopleDataResult }): void => {
      let relevantResults: any = response.PrimaryQueryResult.RelevantResults;
      let resultCount: number = relevantResults.TotalRows;
      let people = [];
      if (resultCount > 0) {
        relevantResults.Table.Rows.forEach( (row) => {
          let persona: IPersonaProps = {};
          row.Cells.forEach( (cell) => {

            if (cell.Key === 'PictureURL')
            {
              persona.imageUrl = cell.Value;
            }
            else if (cell.Key === 'PreferredName')
            {                  
              persona.primaryText = cell.Value;
              persona.imageInitials = cell.Value.charAt(0) + (cell.Value.lastIndexOf(" ") > 0 ? cell.Value.charAt(cell.Value.lastIndexOf(" ") + 1) : "");
            }
            else if (cell.Key === 'AccountName')
            {                  
              persona.itemID = cell.Value; // e.g.: "i:0#.f|membership|bob@somewhere.org.nz"
            }
            else if (cell.Key === 'UserProfile_GUID')
            {                  
              persona.itemID = cell.Value; // e.g.: "ff53b41e-d11a-435c-9cd1-d708e71ee7c7"
            }
          });
          people.push(persona);
        });
      }
      resolve(people);
    }, (error: any): void => {
      reject(this._peopleList = []);
    }));
}

我得到如下列表项:

private _getItems(requester: SPHttpClient): Promise<IInteractionLogItem[]> {
  const queryString: string = `?$select=Id,Title,Client/Id,Client/Title,Client/EMail,Client/MobilePhone,` +
  `$expand=Client`;
const queryUrl: string = this._listItemsUrl + queryString;

return requester.get(queryUrl, SPHttpClient.configurations.v1)
  .then((response: SPHttpClientResponse) => {
    return response.json();
  })
  .then((json: { value: IInteractionLogItem[] }) => {
    return json.value.map((interactionLog: IInteractionLogItem) => {
      const returnItem: IInteractionLogItem = 
      { 
        Id: interactionLog.Id, 
        Title: interactionLog.Title, 
        Client: interactionLog.Client,
      };

      returnItem.CreatedBy.Picture = this._getPictureUrl(returnItem.CreatedBy.EMail);

      if( returnItem.Client )
      {
        returnItem.Client.Picture = this._getPictureUrl(returnItem.Client.EMail);
      }

      return returnItem;
    });
  });
}

非常感谢任何帮助:-)

需要使用 SPHttpClient 调用 api/web/ensureuser。它确保将请求的用户加载到 SharePoint 站点用户信息中。使用给定的 AccountName,例如搜索中的 i:0#.f|membership|bob@somewhere.org.nz 作为参数。返回的 SPUser 包含 Lookup 列的 Id 字段。我实现了以下方法:

public EnsureUser(userName: string): Promise<ISPUser> {
  console.log("SharePointDataProvider.EnsureUser( \"" + userName + "\" )");
  var data = {logonName: userName};

  return this._webPartContext.spHttpClient.post(
    this._ensureUserUrl, 
    SPHttpClient.configurations.v1,
    { body: JSON.stringify(data) } ).then(
      (value: SPHttpClientResponse) => {
        console.log("SharePointDataProvider.EnsureUser FullFill: statusText:\"" + value.statusText + "\"" );
        return value.json();
      },
      (error: any) => console.log("SharePointDataProvider.EnsureUser Rejected: " + error )
    ).then((json: ISPUser) => {
        console.log("SharePointDataProvider.EnsureUser FullFill: Id:" + json.Id +" LoginName:\"" + json.LoginName + "\"" );
      return json;
  });
}

我的迷你 ISP 用户界面:

interface ISPUser
{
    Email: string;
    Id: number;
    LoginName: string;
    Title: string;
}

如果您看到类似

的内容
id:i:0#.f|membership|bob@somewhere.org.nz

但想要身份证号码

id:18

添加

ensureUser={true}

到您的 peoplePicker 组件