Replace People Picker in a Form with Office365Groups.ListGroupMembers
Note
For the purpose of this tutorial, the following controls have these names:
People Picker display name in SharePoint: "Single Choice People Field"
Canvas App Form name: "Form1"
People Picker Combo Box: "DataCardValue2"
Set up
1. Ensure you have added the Office 365 Groups Connector into your App.
2. Insert an EditForm control into your App.
3. The DataCards for each field should be automatically populated, but if not, ensure you have added them into your Form.
4. Unlock the People Picker Datacard
5. In the Items property of the People Picker Combo Box, enter:
Office365Groups.ListGroupMembers(
"Your Office 365 Group ID",
{'$top': 99} //number of group members to retrieve (from 1 to 999, default is 100).
).value
6. Set the DisplayFields property of the Combo Box to:
["displayName"]
7. Configure the Layout property to Single
8. Insert a Button control into the screen and on the OnSelect property, enter:
SubmitForm(Form1)
9. Add another Button control into the screen and on the OnSelect property, enter:
NewForm(Form1)
Sample image of our basic form:
Optional steps
The following steps are optional. They have been added here so we can test the solution works:
1. Add a Gallery control into the Screen
2. Set the Items property to your data source
3. On the OnSelect property of the Gallery, enter:
Set(
varRecord,
ThisItem
);
EditForm(Form1)
4. In the Item property of the Form, enter:
varRecord
5. On the OnSucess property of the Form, enter:
Set(
varRecord,
Self.LastSubmit
)
Saving the selected group member or selected group members
In the Update property of the DataCard (not the DataCardValue), enter:
{
DisplayName: "",
Claims: "i:0#.f|membership|" & Lower(DataCardValue2.Selected.mail),
Department: "",
Email: "",
JobTitle: "",
Picture: ""
}
For a Mult-Select People field, use:
ForAll(
DataCardValue2.SelectedItems As _selections,
{
DisplayName: _selections.displayName,
Claims: "i:0#.f|membership|" & Lower(_selections.mail),
Department: "",
Email: _selections.mail,
JobTitle: "",
Picture: ""
}
)
Explanation:
A complex field type requires a record, and different complex field types will require different attributes in the record. A People Picker field is also a complex field type, and it requires a record value with the following attributes in the schema:
- Claims
- Department
- DisplayName
- Job Title
- Picture
Simply displaying the Display Name in the People Picker Combo Box will not save the selected user into SharePoint, because as mentioned, a Combo Box is supposed to pass a record, and the SharePoint People field requires a record containing the 6 attributes noted above.
Notice above that we can get away with passing empty for every attribute except for the Claims token, which must have a value.
Returning the saved user back into the Combo box
For a Single-Select People field, in the Default property of the DataCard (not DataCardValue2), remove:
ThisItem.'Single Choice People Field'
And enter:
First(
Filter(
Office365Groups.ListGroupMembers("7c53b69a-189a-4a52-b4b2-639a2c277b37").value,
displayName = ThisItem.'Single Choice People Field'.DisplayName
)
)
For a Mult-Select People field, use the following on the DefaultSelectedItems property of the Combobox:
If(
!IsBlank(ThisItem.'Multi Choice People Field'),
ForAll(
ThisItem.'Multi Choice People Field' As _selections,
First(
Filter(
Office365Groups.ListGroupMembers(
"7c53b69a-189a-4a52-b4b2-639a2c277b37",
{'$top': 999}
).value,
mail = _selections.Email
)
)
)
)
Explanation:
We cannot use ThisItem here. Because we changed the Items property of the Combo Box, it is no longer expecting a record which follows the same schema as the AD table. Instead, it is expecting the schema of the Office365Groups.ListGroupMembers table.
In order to return the correct record for the selected user, we apply a filter function to the table where the displayName field is equal to the DisplayName field in the AD table.
Optional final step for a single select people picker
Suppose we want to return the logged in user if the Form is in New Mode, but otherwise return the existing saved user if the Form is in Edit Mode.
To achieve this, in the DefaultSelectedItems property of the Combo Box, enter:
If(
Form1.Mode = FormMode.New,
{displayName: Office365Users.MyProfileV2().displayName},//Office 365 Users displays the display name in the convention the Office 365 Group connector expects
Parent.Default
)
------------------------------------------------------------------------------------------------------------------------------
Thank you.
If you liked this blog, please give it a Thumbs Up.
Imran-Ami Khan
*This post is locked for comments