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

Categories

Recent Posts

  • How to sort an array using Power Automate
    Learn how to sort an array using Power Automate. In this post I share a Power Platform solution containing two example Cloud Flows demonstrating how an array can be sorted. I also briefly explore performance results of running actions in parallel verses serial with some surprising results.
  • How to Add and Remove Accounts from Multi-Select SharePoint Person and Group Fields
    Learn how to use the SharePoint API and Power Automate to add and remove accounts from multi-select Person and Group fields in SharePoint. Our step-by-step guide includes detailed illustrations and examples, as well as an optional Power Platform solution to help you deploy the solution to your tenant. Improve your SharePoint workflow management today!
  • Navigating Microsoft Power Automate Licensing: Challenges, Considerations, and Compliance
    Navigate the complexities of Microsoft Power Automate licensing with insights on challenges, considerations, and compliance. Learn about potential risks, licensing rules, and best practices for organizations using Power Automate. Get guidance on avoiding license breaches, managing premium connectors, and ensuring compliance with Microsoft licensing requirements. Stay informed and take proactive steps to maintain licensing compliance in Power Automate, regardless of your location. Discover practical strategies to optimize your Power Automate licensing and avoid potential pitfalls.
  • SharePoint REST API: How to find the List or Library “__metadata” “type”
    SharePoint REST API is a powerful tool for updating lists and items in SharePoint, but determining the correct “__metadata” “type” for API calls can be confusing. While it’s typically “SP.Data.ListNameItem” based on the list’s URL, there are cases where it may be “SP.Data.ListNameListItem” in certain Office 365 tenants. In this article, we will explore how to find the correct “__metadata” “type” when updating lists or libraries in SharePoint, and also highlight the alternative approach of using the Microsoft Graph API. By understanding this crucial aspect of SharePoint REST API, readers can ensure successful updates and avoid potential errors.
  • “The Age of #AI has begun” said Bill Gates
    March 2023 has been an extraordinary month with the launch of GPT-4 on March 14th, closely followed by #Microsoft’s launch of Copilot preview on March 16th, and Bill Gates’ article, “The Age of AI has begun,” on March 21st.
%d bloggers like this: