SOLVED

How to set a people field in a list, e.g. “convert” AccountName to Lookup ID?

%3CLINGO-SUB%20id%3D%22lingo-sub-87641%22%20slang%3D%22en-US%22%3EHow%20to%20set%20a%20people%20field%20in%20a%20list%2C%20e.g.%20%E2%80%9Cconvert%E2%80%9D%20AccountName%20to%20Lookup%20ID%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-87641%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20trying%20to%20build%20an%20SPFX%20WebPart%20with%20React%20Office%20UI%20Fabric.%20I%20have%20a%20SharePoint%20list%20with%20a%20People%20field%20called%20%3CU%3EAssignedTo%3C%2FU%3E.%20The%20%3CU%3Elookup%20ID%3C%2FU%3E%20values%20are%20like%20%3CU%3E12%3C%2FU%3E%2C%20%3CU%3E20%3C%2FU%3E%2C%20and%20so%20on.%20When%20I%20select%20a%20person%20with%20the%20%3CU%3EPeoplePicker%3C%2FU%3E%2C%20I%20get%20%3CU%3EAccountName%3C%2FU%3E%20like%20%3CU%3Ei%3A0%23.f%7Cmembership%7Cbob%40somewhere.org.nz%3C%2FU%3E%20and%20a%20%3CU%3EGUID%3C%2FU%3E%20like%20%3CU%3Eff53b41e-d11a-435c-9cd1-d708e71ee7c7%3C%2FU%3E.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20think%20the%20%3CU%3Elookup%20IDs%3C%2FU%3E%20like%20%3CU%3E12%3C%2FU%3E%20refer%20to%20the%20%3CU%3EUserInfo%3C%2FU%3E%20list.%20I%20have%20seen%20calls%20to%20%3CU%3Eweb.EnsureUser%3C%2FU%3E%2C%20in%20other%20SharePoint%20projects%2C%20but%20the%20Web%20object%20(%3CU%3Ethis.context.pageContext.web%3C%2FU%3E)%20in%20SPFx%20doesn't%20seem%20to%20have%20those%20methods.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAll%20the%20People%20we%20try%20to%20use%20are%20Office%20365%20users.%20How%20do%20I%20%3CEM%3E%22convert%22%3C%2FEM%3E%20the%20information%20from%20the%20%3CU%3EPeoplePicker%3C%2FU%3E%20to%20the%20%3CU%3Elookup%20IDs%3C%2FU%3E%2C%20the%20%3CU%3E%3CSTRONG%3EPeople%20column%3C%2FSTRONG%3E%3C%2FU%3E%20in%20the%20list%20is%20using%3F%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-87641%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAPIs%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-87661%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20set%20a%20people%20field%20in%20a%20list%2C%20e.g.%20%E2%80%9Cconvert%E2%80%9D%20AccountName%20to%20Lookup%20ID%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-87661%22%20slang%3D%22en-US%22%3E%3CP%3EA%20call%20to%20%60api%2Fweb%2Fensureuser%60%20using%20the%20SPHttpClient%20is%20needed.%20It%20ensures%20the%20requested%20user%20is%20loaded%20into%20the%20SharePoint%20site%20userinfo.%20Using%20the%20given%20%60AccountName%60%20like%20%60i%3A0%23.f%7Cmembership%7Cbob%40somewhere.org.nz%60%20from%20the%20search%20as%20parameter.%20The%20returned%20SPUser%20contains%20the%20%60Id%60%20field%20for%20the%20Lookup%20column.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20implemented%20the%20following%20method%3A%3C%2FP%3E%3CPRE%3E%20%20public%20EnsureUser(userName%3A%20string)%3A%20Promise%26lt%3BISPUser%26gt%3B%20%7B%0A%20%20%20%20console.log(%22SharePointDataProvider.EnsureUser(%20%5C%22%22%20%2B%20userName%20%2B%20%22%5C%22%20)%22)%3B%0A%20%20%20%20var%20data%20%3D%20%7BlogonName%3A%20userName%7D%3B%0A%0A%20%20%20%20return%20this._webPartContext.spHttpClient.post(%0A%20%20%20%20%20%20this._ensureUserUrl%2C%20%0A%20%20%20%20%20%20SPHttpClient.configurations.v1%2C%0A%20%20%20%20%20%20%7B%20body%3A%20JSON.stringify(data)%20%7D%20).then(%0A%20%20%20%20%20%20%20%20(value%3A%20SPHttpClientResponse)%20%3D%26gt%3B%20%7B%0A%20%20%20%20%20%20%20%20%20%20console.log(%22SharePointDataProvider.EnsureUser%20FullFill%3A%20statusText%3A%5C%22%22%20%2B%20value.statusText%20%2B%20%22%5C%22%22%20)%3B%0A%20%20%20%20%20%20%20%20%20%20return%20value.json()%3B%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20(error%3A%20any)%20%3D%26gt%3B%20console.log(%22SharePointDataProvider.EnsureUser%20Rejected%3A%20%22%20%2B%20error%20)%0A%20%20%20%20%20%20).then((json%3A%20ISPUser)%20%3D%26gt%3B%20%7B%0A%20%20%20%20%20%20%20%20%20%20console.log(%22SharePointDataProvider.EnsureUser%20FullFill%3A%20Id%3A%22%20%2B%20json.Id%20%2B%22%20LoginName%3A%5C%22%22%20%2B%20json.LoginName%20%2B%20%22%5C%22%22%20)%3B%0A%20%20%20%20%20%20%20%20return%20json%3B%0A%20%20%20%20%20%20%7D)%3B%0A%20%20%7D%3C%2FPRE%3E%3CP%3EMy%20mini%20ISPUser%20interface%3A%3C%2FP%3E%3CPRE%3Einterface%20ISPUser%0A%7B%0A%20%20%20%20Email%3A%20string%3B%0A%20%20%20%20Id%3A%20number%3B%0A%20%20%20%20LoginName%3A%20string%3B%0A%20%20%20%20Title%3A%20string%3B%0A%7D%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
New Contributor

I am trying to build an SPFX WebPart with React Office UI Fabric. I have a SharePoint list with a People field called AssignedTo. The lookup ID values are like 12, 20, and so on. When I select a person with the PeoplePicker, I get AccountName like i:0#.f|membership|bob@somewhere.org.nz and a GUID like ff53b41e-d11a-435c-9cd1-d708e71ee7c7.

 

I think the lookup IDs like 12 refer to the UserInfo list. I have seen calls to web.EnsureUser, in other SharePoint projects, but the Web object (this.context.pageContext.web) in SPFx doesn't seem to have those methods.

 

All the People we try to use are Office 365 users. How do I "convert" the information from the PeoplePicker to the lookup IDs, the People column in the list is using? 

1 Reply
best response confirmed by Dennis Kuhn (New Contributor)
Solution

A call to `api/web/ensureuser` using the SPHttpClient is needed. It ensures the requested user is loaded into the SharePoint site userinfo. Using the given `AccountName` like `i:0#.f|membership|bob@somewhere.org.nz` from the search as parameter. The returned SPUser contains the `Id` field for the Lookup column.

 

I implemented the following method:

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

My mini ISPUser interface:

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