Person field Dropdown

Hi,

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.

Hi @aseem,

Unfortunately, the Person field can't do that at the moment.

I think the best way to go about this would be to make a list of all people you need to display in a dropdown, then use a lookup field to choose one.

As an alternative, you could drop us a paid support request at support@plumsail.com, so we'd implement the feature for you.

Thanks Llia, will try that.

Hello @aseem ,

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:
image
Dropdown is helper field above.
Field below is for storing selected User when the field above is changed.
image

Regards
Stepan

3 Likes

This is very helpful. Thank you!

Hello @aseem ,

During the weekend I was thinking about the person field you wanted to use.
You can do it too because whenever you use this:

const userNames = users.map((user) => user.Title);

You can create key value pairs: (when the function is called and the users are returned)

let userObject = {
  "ben@domain.com": "Ben Jakotyč",
  "julia@domain.com": "Julia Roberts"
};

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:

fd.field("PersonInternalNameField").value = {value}

That will do the thing you will see the user in the "Person Field".

I hope I digged a bit deeper towards the conclusion.

Stepan

4 Likes

It did help. Thanks.