SOLVED

How to blank a People Picker (PnP, spfx, react)

%3CLINGO-SUB%20id%3D%22lingo-sub-1011135%22%20slang%3D%22en-US%22%3EHow%20to%20blank%20a%20People%20Picker%20(PnP%2C%20spfx%2C%20react)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1011135%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20an%20spfx%20form%20on%20a%20SharePoint%20Online%20site%20collection%20which%20reads%2Fwrites%20data%20to%20an%20SP%20list.%20I%20have%203%20people%20pickers%20which%20I've%20managed%20to%20submit%20(using%20a%20submit%20button)%20successfully%20to%20the%20list.%20I%20can%20also%20retrieve%20these%203%20people%20pickers%20from%20the%20list%20successfully.%20The%20problem%20is%20when%20I%20remove%20a%20user%20from%20any%20of%20the%20people%20pickers.%20The%20console%20gives%20me%20an%20error%20of%20Unable%20to%20get%20property%20'id'%20of%20undefined%20or%20null%20reference.%20I%20think%20I%20need%20something%20that%20can%20handle%20the%20removal%20of%20a%20user%20in%20the%20function%20but%20I%20can't%20think%20what.%20I%20subsequently%20want%20the%20user%20(using%20the%20form)%20to%20be%20able%20to%20remove%20the%20user%20from%20the%20PP%20and%20then%20click%20an%20update%20button%20to%20update%20and%20submit%20the%20change.%3C%2FP%3E%3CP%3EHere's%201%20of%20the%203%20people%20pickers%20in%20the%20render%20(no%20point%20putting%20all%203!)%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3E%3CPEOPLEPICKER%20context%3D%22%7Bthis.props.context%7D%22%20personselectionlimit%3D%22%7B1%7D%22%20groupname%3D%22%7B''%7D%22%3E%3C%2FPEOPLEPICKER%3E%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3CSPAN%3EHere%20are%20the%203%20(1%20per%20PP)%20functions%20for%20all%203%20of%20the%20people%20pickers.%20These%20functions%20are%20assigned%20(as%20you%20can%20see%20above)%20to%20the%20selectedItems%20property%20of%20the%20PP.%20I%20believe%20this%20is%20the%20'onChange'%20property%20of%20the%20PP%3A%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3E%20private%20_deptContGetPeoplePickerItems(items)%20%7B%0A%20%20%20%20if(items%20!%3D%3D%20null)%7B%0A%20%20%20%20this.setState(%7B%0A%20%20%20%20%20%20DeptContact%3A%20items%5B0%5D.id%2C%0A%20%20%20%20%20%20DeptContactPPDefaultItems%3A%20%5Bitems%5B0%5D.secondaryText%5D%2C%0A%20%20%20%20%20%20%0A%20%20%20%20%7D%2C%20()%3D%26gt%3B%7B%0A%20%20%20%20%20%20console.log(this.state.DeptContact%2B'this.state.DeptContactADD')%3B%0A%20%20%20%20%20%20console.log(this.state.DeptContactPPDefaultItems%2B'this.state.DeptContactPPDefaultItemsADD')%3B%0A%20%20%20%20%7D)%3B%0A%20%20%20%7D%20%0A%20%20%20if(items%20%3D%3D%3D%20null)%7B%0A%20%20%20%20%0A%20%20%20%20%20this.setState(%7B%0A%20%20%20%20%20%20DeptContact%3A%20items%5B0%5D.id%2C%0A%20%20%20%20%20%20DeptContactPPDefaultItems%3A%20%5Bitems%5B0%5D.secondaryText%5D%20%20%0A%20%20%20%20%20%7D%2C%20()%20%3D%26gt%3B%20%7B%0A%20%20%20%20%20%20console.log(this.state.DeptContact%2B'this.state.DeptContactNULLADD')%3B%0A%20%20%20%20%20%20console.log(this.state.DeptContactPPDefaultItems%2B'this.state.DeptContactPPDefaultItems%5B%5DADD')%3B%0A%20%20%20%20%20%7D)%3B%0A%20%20%20%7D%0A%20%20%7D%0A%0A%20%20private%20_linkAdvGetPeoplePickerItems(items)%20%7B%0A%20%20%20%20if(items%20!%3D%3D%20null)%7B%0A%20%20%20%20this.setState(%7B%0A%20%20%20%20%20%20LinkAdviser%3A%20null%2C%0A%20%20%20%20%20%20LinkAdviserPPDefaultItems%3A%20%5B%5D%0A%20%20%20%20%7D)%3B%0A%20%20%7D%20%0A%20%20if(items%20%3D%3D%3D%20null)%7B%0A%20%20%20%20this.setState(%7B%0A%20%20%20%20%20%20LinkAdviser%3A%20items%5B0%5D.id%2C%0A%20%20%20%20%20%20LinkAdviserPPDefaultItems%3A%20%5Bitems%5B0%5D.secondaryText%5D%0A%20%20%20%20%7D)%3B%0A%20%20%20%7D%0A%20%20%7D%0A%0A%20%20private%20_linkAdv2GetPeoplePickerItems(items)%20%7B%0A%20%20%20%20if(items%20%3D%3D%3D%20null)%7B%0A%20%20%20%20%20%20this.setState(%7B%0A%20%20%20%20%20%20%20%20LinkAdviser2%3A%20null%2C%0A%20%20%20%20%20%20%20%20LinkAdviser2PPDefaultItems%3A%20%5B%5D%0A%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%7D%20else%20if(items)%7B%0A%20%20%20%20this.setState(%7B%0A%20%20%20%20%20%20LinkAdviser2%3A%20items%5B0%5D.id%2C%0A%20%20%20%20%20%20LinkAdviser2PPDefaultItems%3A%20%5Bitems%5B0%5D.secondaryText%5D%0A%20%20%20%20%7D)%3B%0A%20%20%20%7D%0A%20%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAs%20you%20can%20see%20I've%20attempted%20to%20handle%20removal%20of%20a%20user%20from%20the%20PP%20with%20the%20if.%20I%20seem%20to%20think%20a%20lot%20can%20be%20handled%20with%20if's!%3C%2FP%3E%3CP%3EAs%20I've%20mentioned%20above%2C%20it%20says%20'Unable%20to%20get%20property%20'id'%20of%20undefined%20or%20null%20reference'%20when%20I%20remove%20a%20user%20from%20the%20PP.%20This%20error%20happens%20when%20I%20actually%20click%20the%20remove%20x%20on%20the%20PP%2C%20not%20during%20submission%20or%20update%20btw.%3C%2FP%3E%3CP%3EAnyone%20see%20what%20I'm%20missing%20here%3F%3C%2FP%3E%3CP%3ET%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1011472%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20blank%20a%20People%20Picker%20(PnP%2C%20spfx%2C%20react)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1011472%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F142793%22%20target%3D%22_blank%22%3E%40Thomas%20Pipkin%3C%2FA%3E%26nbsp%3B%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWithout%20trying%20it%20out%2C%20just%20looking%20at%20your%20code%2C%20I%20think%20the%20error%20you%20are%20getting%20may%20be%20from%20the%20following%20line%20on%20the%20%22onChange%22%20functions%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDeptContact%3A%20items%5B0%5D.id%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20have%20a%20%22if%22%20to%20check%20if%20the%20items%20object%20is%20null%2C%20but%20when%20the%20people%20picker%20is%20empty%2C%20I%20believe%20it%20returns%20an%20empty%20array.%20You%20can%20check%20list%20by%20adding%20a%20console.log%20or%20debugging%20before%20the%20if%20statement.%3C%2FP%3E%3CP%3EIf%20so%2C%20update%20your%20%22if%22%20condition%20to%20also%20validate%20that%20items.length%20%26gt%3B%200%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHope%20this%20helps%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1014447%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20blank%20a%20People%20Picker%20(PnP%2C%20spfx%2C%20react)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1014447%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F9907%22%20target%3D%22_blank%22%3E%40Joel%20Rodrigues%3C%2FA%3E%26nbsp%3BHi%20Joel%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYes%20you%20are%20right%20-%20but%20I'm%20struggling%20to%20find%20a%20way%20to%20evaluate%20it%20as%20blank.%20When%20removing%20the%20user%20from%20the%20PP%2C%20it's%20always%20evaluating%20as%20not%20null.%20This%20is%20what%20I'm%20trying%20now%20but%20it's%20still%20evaluating%20as%20not%20null%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Eprivate%20_deptContGetPeoplePickerItems(items)%20%7B%0A%20%20%20%20%20%20if(this.state.DeptContact%20!%3D%3D%20null)%7B%0A%20%20%20%20%20%20%20%20console.log('Dept%20Contact%20not%20null')%3B%0A%20%20%20%20this.setState(%7B%0A%20%20%20%20%20%20DeptContact%3A%20items%5B0%5D.id%2C%0A%20%20%20%20%20%20DeptContactPPDefaultItems%3A%20%5Bitems%5B0%5D.secondaryText%5D%2C%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%20%20%7D%20else%20if(this.state.DeptContact)%7B%0A%20%20%20%20%20%20%20%20console.log('Dept%20Contact%20is%20null')%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDo%20I%20have%20to%20use%20an%20async%20await%20so%20when%20the%20a%20user%20clicks%20to%20blank%20the%20pp%2C%20it%20waits%20for%20the%20state%26nbsp%3B%20to%20empty%2C%20then%20the%20if%20can%20run%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20so...how%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ET%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1014458%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20blank%20a%20People%20Picker%20(PnP%2C%20spfx%2C%20react)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1014458%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F9907%22%20target%3D%22_blank%22%3E%40Joel%20Rodrigues%3C%2FA%3E%26nbsp%3B-%20thanks%20Joel.%20That%20was%20one%20of%20those%20that%20has%20been%20bugging%20me%20for%20a%20while.%20I%20got%20close%20to%20your%20answer%20at%20one%20point%20but%20took%20the%20wrong%20direction%20and%20hit%20a%20dead%20end.%20Thanks%20again.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Contributor

I have an spfx form on a SharePoint Online site collection which reads/writes data to an SP list. I have 3 people pickers which I've managed to submit (using a submit button) successfully to the list. I can also retrieve these 3 people pickers from the list successfully. The problem is when I remove a user from any of the people pickers. The console gives me an error of Unable to get property 'id' of undefined or null reference. I think I need something that can handle the removal of a user in the function but I can't think what. I subsequently want the user (using the form) to be able to remove the user from the PP and then click an update button to update and submit the change.

Here's 1 of the 3 people pickers in the render (no point putting all 3!):

 

 

<PeoplePicker
         context={this.props.context}
         personSelectionLimit={1}
         groupName={''} // Leave this blank in case you want to filter from all users
         showtooltip={false}
         isRequired={false}
         disabled={false}
         selectedItems={this._deptContGetPeoplePickerItems}//This is the 'onChange'
         showHiddenInUI={false}
         defaultSelectedUsers={this.state.DeptContactPPDefaultItems ? this.state.DeptContactPPDefaultItems : []}//this is the value it means if this.state.PeoplePickerDefaultItems is truthy then show it, if not then don't
         principalTypes={[PrincipalType.User]}
         resolveDelay={1000}
         ensureUser={true}
    />

 

 

 Here are the 3 (1 per PP) functions for all 3 of the people pickers. These functions are assigned (as you can see above) to the selectedItems property of the PP. I believe this is the 'onChange' property of the PP:

 

 

 private _deptContGetPeoplePickerItems(items) {
    if(items !== null){
    this.setState({
      DeptContact: items[0].id,
      DeptContactPPDefaultItems: [items[0].secondaryText],
      
    }, ()=>{
      console.log(this.state.DeptContact+'this.state.DeptContactADD');
      console.log(this.state.DeptContactPPDefaultItems+'this.state.DeptContactPPDefaultItemsADD');
    });
   } 
   if(items === null){
    
     this.setState({
      DeptContact: items[0].id,
      DeptContactPPDefaultItems: [items[0].secondaryText]  
     }, () => {
      console.log(this.state.DeptContact+'this.state.DeptContactNULLADD');
      console.log(this.state.DeptContactPPDefaultItems+'this.state.DeptContactPPDefaultItems[]ADD');
     });
   }
  }

  private _linkAdvGetPeoplePickerItems(items) {
    if(items !== null){
    this.setState({
      LinkAdviser: null,
      LinkAdviserPPDefaultItems: []
    });
  } 
  if(items === null){
    this.setState({
      LinkAdviser: items[0].id,
      LinkAdviserPPDefaultItems: [items[0].secondaryText]
    });
   }
  }

  private _linkAdv2GetPeoplePickerItems(items) {
    if(items === null){
      this.setState({
        LinkAdviser2: null,
        LinkAdviser2PPDefaultItems: []
      });
    } else if(items){
    this.setState({
      LinkAdviser2: items[0].id,
      LinkAdviser2PPDefaultItems: [items[0].secondaryText]
    });
   }
  }

 

 

As you can see I've attempted to handle removal of a user from the PP with the if. I seem to think a lot can be handled with if's!

As I've mentioned above, it says 'Unable to get property 'id' of undefined or null reference' when I remove a user from the PP. This error happens when I actually click the remove x on the PP, not during submission or update btw.

Anyone see what I'm missing here?

T

 

 

4 Replies

Hi @Cardinal_Night ,

 

Without trying it out, just looking at your code, I think the error you are getting may be from the following line on the "onChange" functions

 

DeptContact: items[0].id,

 

You have a "if" to check if the items object is null, but when the people picker is empty, I believe it returns an empty array. You can check list by adding a console.log or debugging before the if statement.

If so, update your "if" condition to also validate that items.length > 0

 

Hope this helps

 

@Joel Rodrigues Hi Joel,

 

Yes you are right - but I'm struggling to find a way to evaluate it as blank. When removing the user from the PP, it's always evaluating as not null. This is what I'm trying now but it's still evaluating as not null:

 

private _deptContGetPeoplePickerItems(items) {
      if(this.state.DeptContact !== null){
        console.log('Dept Contact not null');
    this.setState({
      DeptContact: items[0].id,
      DeptContactPPDefaultItems: [items[0].secondaryText],
    
        });
      } else if(this.state.DeptContact){
        console.log('Dept Contact is null');
      }
    }

 

Do I have to use an async await so when the a user clicks to blank the pp, it waits for the state  to empty, then the if can run?

 

If so...how?

 

T

Best Response confirmed by Cardinal_Night (Contributor)
Solution

@Cardinal_Night 

 

I think you should be evaluating the items object and not the state as you are using that function to set the state

Have you tried something like:

 

if(items !== null && items.length > 0){

@Joel Rodrigues - thanks Joel. That was one of those that has been bugging me for a while. I got close to your answer at one point but took the wrong direction and hit a dead end. Thanks again.