Sp-dev-fx-controls-react: PeoplePicker Control needs property to initialize control with person(s) or group(s).

Created on 14 Jul 2018  路  21Comments  路  Source: pnp/sp-dev-fx-controls-react

Category

[x] Enhancement

[ ] Bug

[ ] Question

Version

Please specify what version of the library you are using: [ 1.6 ]

Expected / Desired Behavior / Question

I have a need for a property to initialize the peoplepicker control with a person or group.

Observed Behavior

I do not see how to do this with current set of properties.

Most helpful comment

Hi All, this functionality is implemented now. Added a property - defaultSelectedUsers = {["[email protected]", "[email protected]"]} , to which we can give an array of emails to select users.

If the users, don't exist, the users are added to the control by a random unique Id. They cannot be added later as they are not part of the Site users list. Please let me know if this looks ok.

@estruyf, fyi, the above is merged with the existing PR.

All 21 comments

@rchivert. could you please elaborate? Are you suggesting to prefill the control with a set of users on load? If you would like limit the search, use groupName to limit the search to a SharePoint group.

@AsishP Yes prefill the controller with an entity (eg. [email protected]). So you can use the controller for editing stored data etc without the need to show the current value another place.

@AsishP - echoing @phettz ...... yes.

@rchivert @phettz , sure. Will open up the selecteditems property which will allow an array of selected emails to prepopulate the control.

The only catch is how do we validate that the emails are valid users of this site. Or what happens if the users are not part of the Site Users list. Should we validate them and show error or still add in the control?

@AsishP, that sounds great

Hm, could use /_api/web/ensureuser if you open up for sending in LoginName(email in most cases)? Then they should be added to the Site Users list too if they don't exists.

If the user is not valid one could show a validation error and not return that object in the items returned by the selectedItems prop?

if (selectedItems) {
      // dont return not valid users here
      selectedItems(items);
 }

Just a thought, maybe there is a more clever way of doing it

Hmmm indeed. We need to consider the case where the user has left the tenant.... we still want to be able to pre-populate the peoplepicker, I'd think. But it would no longer allow an update... since user is no longer valid. I recommend looking at how the "classic" peoplepicker handles this scenario. Also, this React Peoplepicker has a nice initialization... without validating the users... so at least it can be pre-populated. But man oh man... it was a pain for me to figure out how to initialize the user : SharePointUserPersona[] arrary.

Agree, the control can only be used on new forms if you can't send it a user to preload. How would the edit form work if a user has already been entered?

@phettz , I don't know if we should add users to the site, if not present, only for prefilling the control. Love the idea of validation. We could do that?

@rchivert , the example you have mentioned is a web part so showing the property pane section is possible in that case. Peoplepicker is only a control, so how do you think of acheiving that functionality. Currently only thinking of an array of emails to preset users ? What additional features could be helpful?

@XopherDesign , the edit control will have a list of users prefilled when the control is loaded. The control initial state is empty right now. After the implementation, it would allow users to be prefilled if provided with selectedItemsproperty.

@AsishP , is the edit control in development or available now?

@XopherDesign, not available yet, starting development but should be done soon. I just have to cater for exceptions of user not present in the web scenario.

Hi All, this functionality is implemented now. Added a property - defaultSelectedUsers = {["[email protected]", "[email protected]"]} , to which we can give an array of emails to select users.

If the users, don't exist, the users are added to the control by a random unique Id. They cannot be added later as they are not part of the Site users list. Please let me know if this looks ok.

@estruyf, fyi, the above is merged with the existing PR.

Thanks @AsishP will be processed soon!

This enhancement is merged and planned to be released with the 1.7.0 release.

Thanks alot @AsishP, I had a requirement to fetch person/group field data from list like TO, CC, From.

I was wondering to get the solution, After reading your comment, i implemented perfectly this defaultSelectedUsers in people picker.

Here is the code snippet

getItemspnp = (id: number) => {

  var reactHandler = this; 
  pnp.setup({
    spfxContext: this.props.context
  }); 
pnp.sp.web.lists.getByTitle("EmailAlert").items.getById(id).select("Title","ListName","Subject","Body","To/EMail","To/ID","User/EMail","User/ID","CC/ID","CC/EMail").expand("To","User","CC").get().then((item: any) => {

    let ccEMAils = item.CC && item.CC.map((item)=>{return item.EMail});         
    let toEMails = item.To && item.To.map((item)=>{return item.EMail});

    let toIDs = item.To && item.To.map((item)=>{return item.ID.toString()});
    let ccIDs = item.CC && item.CC.map((item)=>{return item.ID.toString()});

    console.log("ccEMAils===============:", ccEMAils);   
    console.log("toEMails===============:", toEMails);   

    reactHandler.setState({
      SiteUrl: item.Title,
      ListName: item.ListName,
      Subject: item.Subject,
      Body: item.Body,
      defaultSelectedUserFrom: item.User?[item.User.EMail]:[],
      defaultSelectedUsersCC: ccEMAils? [...ccEMAils]:[],
      defaultSelectedUserTo: toEMails?[...toEMails]:[],
      From: item.User?item.User.ID.toString():"",
      CC: ccIDs?[...ccIDs]:[],
      To: toIDs?[...toIDs]:[]
    });
});

}


context={this.props.context}
titleText="From"
personSelectionLimit={1}
groupName={""} // Leave this blank in case you want to filter from all users
showtooltip={true}
isRequired={true}
disabled={false}
selectedItems={this._getPeoplePickerItemsFrom}
**defaultSelectedUsers={this.state.defaultSelectedUserFrom?this.state.defaultSelectedUserFrom:null}**
showHiddenInUI={false}
principalTypes={[PrincipalType.User]}
resolveDelay={1000}
ensureUser={true} />

Tagging onto an old thread here in the hopes that someone here has already solved my problem...

How should the defaultSelectedUsers be constructed to initialize groups? I'm using the PeoplePicker control to select AD groups, and no combination of defaultSelectedUsers values appear to set the default state of the people picker for an edit scenario.

It correctly stores the values when I pick a group from the list:

image

But when reloading the form, I just get a blank people picker even though defaultSelectedUsers is "properly" set per the documentation...

image

image

Is there maybe some voodoo I need to do to modify the "loginName" reported by the original selection in order to make it correctly find the group for default initialization?

Hi All,

I am also facing the above mentioned issue. Couldn't initialize user field with other than user name like sharepoint group, security group or distribution list name.

We have a client requirement to allow people picker to resolve all type of entries.

Kindly help on this.

Thanks

Hey Everyone,

Same here. The defaultSelectedUsers should be updated to something like defaultSelectedUsersGroups.
Ideally it would make the most sense to accept the same array format as the output from selectedItems. That way you can store the data and you can load the data, but right now it takes a string array which isn't flexible to handle the different principal types.

Thanks

@webdes03 Did you find any workaround to show the security groups using defaultSelectedUsers attribute?

@webdes03 Did you find any workaround to show the security groups using defaultSelectedUsers attribute?

Sort of, but not really... I ended up just implementing a TagPicker control and throwing away the PeoplePicker because I couldn't get the PeoplePicker to work.

@Sarah4x @unnieayilliath @webdes03

Setting "principalTypes" to include PrincipalType.SecurityGroup and then specifying the name of the security group in the defaultSelectedUsers does the trick?

image

Above approach works for me. Thanks @brianpmccullough for sharing. However, I wonder how it works with the group name but not with the group item id. Group id more likely qualifies to be unique identifier than group name :-).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AriGunawan picture AriGunawan  路  8Comments

enti333 picture enti333  路  7Comments

Arknev picture Arknev  路  3Comments

christopher-walker picture christopher-walker  路  7Comments

thespooler picture thespooler  路  6Comments