How to Add and Remove Accounts from Multi-Select SharePoint Person and Group Fields

Send an HTTP request PATCH Account (person)

The following article describes how to add and remove an account from a multi-select SharePoint ‘Person and Group’ field using the SharePoint API and Power Automate. To illustrate this, I have created two Cloud Flows.

The ‘PeopleField-AddPerson‘ is an instant cloud flow that implements a manual trigger that prompts for an Item ID and email address of the account to add to the item’s ‘People’ field. This process works for both Azure AD Member and Guest accounts.

PeopleField-AddPerson cloud flow

The ‘PeopleField-RemovePerson‘ is an instant cloud flow that implements a manual trigger that prompts for an Item ID and email address of the account to remove from the ‘People’ field. This process works for both Azure AD Member and Guest accounts. If the account has not previously been added to the SharePoint Site, then the Flow cancels itself.

PeopleField-RemovePerson cloud flow

This solution is for demonstration purposes only and not production-ready. If you would like to improve the error handling of the flows, then check out my article on ‘Error Handling using a Power Automate Child Flow‘.

Deploy the Solution

This is optional, and you can skip to the next section. However, you can download the Power Platform solution from my GitHub Repo if you would like to deploy the solution to your tenant.

The ‘Sandpit: Update SharePoint People Field’ Power Platform solution contains two cloud flows, two connection references, and two environment variables.

Before installing the Power Platform solution, start by creating a ‘Blank list’ and set its properties to:

  • Name: SandpitUSPF
  • Description: Sandpit: Update SharePoint People Field

This sets a ‘nice’ URL before renaming it to Sandpit-USPF, which is easier to read.

Next, add a new field using the ‘modern’ UI select Person and click ‘Next’, and set the properties to:

  • Name: People
  • Tick: Allow selection of Groups
  • Expand: More options:
    • Allow multiple selection: Yes
    • Add to all content types: Yes
SharePoint list 'Sandpit-USPF'

When deploying the solution, you will be prompted to set the connection reference and environment variables.

The Detail

The SharePoint REST API endpoint used to add and remove a ‘person’ from a ‘Person or Group’ field is the same; the difference is in preparation. In the ‘Add Person‘ flow, we POST to the ‘/_api/web/ensureuser‘ endpoint passing the account’s logonName. This does two things: first, it checks whether the specified login name belongs to a valid user in the site, and secondly, if the user doesn’t exist, adds the user to the site before returning the user’s ID. Now that we have the user’s ID, we can add it to the array that we PATCH the item’s People field with.

{
  "logonName": "@{first(outputs('Search_for_users_(V2)_|_By_EmailAddress')?['body/value'])?['UserPrincipalName']}"
}
PeopleField-AddPerson: Ensure User step
/_api/web/ensureuser

In the ‘Remove People‘ flow, we attempt to GET the email address of the account from the hidden SiteUsers list. If the account is found, then we remove the ID using a filter array before we PATCH the item’s People field with the remaining people.

_api/web/SiteUsers/getByEmail('@{variables('EmailAddress')}')?$select=Id
PeopleField-RemovePerson: Get item from SiteUsers list usng getByUser()

And as promised, both add and remove use the same step to update the people field.

{
  "__metadata": {
    "type": "@{body('Parse_JSON_|_Item')?['d']?['__metadata']?['type']}"
  },
  "PeopleId": {
    "__metadata": {
      "type": "Collection(Edm.Int32)"
    },
    "results": @{body('Filter_array_|_Remove_Person_ID_From_Array')}
  }
}
PATCH the Person and Group field
/_api/web/lists(guid'@{parameters('Sandpit-USPF-SPList (sjl_SandpitUSPFSPList)')}')/items(@{variables('ItemID')})

Headers:

X-HTTP-Method: MERGE
accept: application/json;odata=verbose
content-type: application/json;odata=verbose
If-Match: *

One addition I did make to these two flows was to dynamically reference the list’s “__metadata” “type” within the ‘Compose | Update Account Body‘ step. I was able to do this because I had requested the item at the top of the flow.

@{body('Parse_JSON_|_Item')?['d']?['__metadata']?['type']}

Conclusion

In conclusion, using the SharePoint API and Power Automate, it is possible to add and remove accounts from a multi-select Person and Group field in SharePoint. The process is simple and can be achieved using the REST API endpoints. The ‘PeopleField-AddPerson’ and ‘PeopleField-RemovePerson’ Cloud Flows provided in this article illustrate how to implement this process.

It is important to note that this solution is for demonstration purposes only and may need additional error handling if implemented in a production environment. However, with the Power Platform solution provided, it is easy to deploy and test in your tenant.

Overall, this solution can save time and effort when managing SharePoint lists with multiple users and groups. With the help of the SharePoint API and Power Automate, adding and removing accounts from a multi-select Person and Group field can be achieved efficiently and effectively.

Useful Links