We have a person field that is connected to a SharePoint group. We need to display the users from the group in dropdown format so that it is easy to select. Is there a way that we can change the person field to display all users without entering the names? We would like to use person field since it is used in filtered to me view.
If I understand it well, you want to read users from SharePoint Group and show them in the dropdown?
It does not have to be complicated with PnP Javascript Library..
Get the ID of the SharePoint Group on SharePoint - for example, on my site on this url it looks like this:
https://{myTenant}/sites/Plumsail-Community/_layouts/15/people.aspx?MembershipGroupId=5
(Go to Site Settinfgs - Site Permission - Click on SharePoint Group) - and you will the the number at the end of the string (in this example - 5).
Now the script for the inspiration:
window.fd = fd;
window.pnp = pnp;
fd.spRendered(() => {
const groupId = 3;
// Initialize your SP setup, typically done inside an async function
// Ensure you've setup the SP context before calling this function
async function getUsersInSharePointGroup(groupId) {
try {
// Get the SharePoint group by ID
const group = pnp.sp.web.siteGroups.getById(groupId);
// Get all users in the group
const users = await group.users();
return users;
} catch (error) {
console.error("Error fetching users in SharePoint group:", error);
throw error; // Or handle it as per your application's error handling logic
}
}
getUsersInSharePointGroup(groupId)
.then((users) => {
// Process the users as needed
const userNames = users.map((user) => user.Title);
// Dropdown field is only present on the Form
fd.field("Users").options = userNames;
})
.catch((error) => {
console.error(error);
});
// Dropdown is shown on the form and when the field is changed, save it to your - for example - sharepoint field
fd.field("Users").$on("change", (val) => {
// This will store the selected User to SharePoint field - Be careful, this field must be present on the Form - but can be Disabled, more likely Hidden
fd.field("UserToStore").value = val;
});
});
When you get the users, then you can dynamically fill in the dropdown control/field with this data.
Result:
Dropdown is helper field above.
Field below is for storing selected User when the field above is changed.
You set the dropdown with "Title" (FullName) and when the dropdown is changed, you can check created object if the value is match from the object and draw the data from the object (for Ben chosen) you will get "ben@domain.com" and the you will use for the person field this piece of script: