office365sharepoint-onlinesharepoint-rest-api

Pass multiple PrincipleIDs to the roleassignments/addroleassignment endpoint inside SharePoint Online REST API


I have a cloud flow, which get a semi-colon separated string containing Office 365 security groups names. Then I build an array after splitting the string.

After that for each group name I get the group PrincipleID, then i add the group to the item permission with Read role. Sometimes this array contains 5 group names. So I will assign each group to the list item separately, which might not be the best approach. So I am looking to pass all the 5 group PrincipleIDs at once to SharePoint.

So is there a way to pass a list of groups' principle Ids to the roleassignments/addroleassignment end point, instead of just passing single Principe ID? keeping in mind that all groups will have Read.

Here is my current flow:-

enter image description here

enter image description here

so is there a way to pass multiple Principleds inside this Uri:-

_api/web/lists('*ListName*')/items(*ItemNumber*)/roleassignments/addroleassignment(PrincipalId=*PrincipleID*,roleDefId=1073741826)

EDIT

I tried this inside Power Automate Send Http Request to SharePoint to doa Batch request:-

enter image description here

but i got this error:-

{
  "status": 404,
  "message": "Cannot find resource for the request $batch.\r\nclientRequestId: 38ee4283-cbe9-4544-b58e-20be4dff6677\r\nserviceRequestId: 60405aa1-c0f6-a000-176a-18a93b116d0f",
  "source": "https://**.sharepoint.com/_api/$batch",
  "errors": [
    "-1",
    "Microsoft.SharePoint.Client.ResourceNotFoundException"
  ]
}

EDIT-2

After i changed the main method from GET to POST as follow:-

enter image description here

i got this error:-

A supported MIME type could not be found that matches the content type of the response. None of the supported type(s) 'multipart/mixed' matches the content type 'application/json; charset=utf-8'.

and here is the Body text i am passing:-

--batch_1234
Content-Type: multipart/mixed; boundary=changeset_1234

--changeset_1234
Content-Type: application/http
Content-Transfer-Encoding: binary

POST https://**.sharepoint.com/_api/web/lists/GetByTitle('GroupNamePrincipleID')/items(1)/roleassignments/addroleassignment(PrincipalId=2047,roleDefId=1073741826) HTTP/1.1

--changeset_1234
Content-Type: application/http
Content-Transfer-Encoding: binary

POST https://**.sharepoint.com/_api/web/lists/GetByTitle('GroupNamePrincipleID')/items(2)/roleassignments/addroleassignment(PrincipalId=2047,roleDefId=1073741826) HTTP/1.1

--changeset_1234
Content-Type: application/http
Content-Transfer-Encoding: binary

POST https://**.sharepoint.com/_api/web/lists/GetByTitle('GroupNamePrincipleID')/items(3)/roleassignments/addroleassignment(PrincipalId=2047,roleDefId=1073741826) HTTP/1.1

--changeset_1234--
--batch_1234--

EDIT-3

I added Content-Type: application/json, but still the same error on the Content-Types

--batch_1234
Content-Type: multipart/mixed; boundary="changeset_1234"
Host: https://**.sharepoint.com
Content-Transfer-Encoding: binary

--changeset_1234
Content-Type: application/json
Content-Type: application/http
Content-Transfer-Encoding: binary

POST https://**.sharepoint.com/_api/web/lists/GetByTitle('GroupNamePrincipleID')/items(1)/roleassignments/addroleassignment(PrincipalId=2047,roleDefId=1073741826) HTTP/1.1

--changeset_1234
Content-Type: application/json
Content-Type: application/http
Content-Transfer-Encoding: binary

POST https://**.sharepoint.com/_api/web/lists/GetByTitle('GroupNamePrincipleID')/items(2)/roleassignments/addroleassignment(PrincipalId=2047,roleDefId=1073741826) HTTP/1.1

--changeset_1234
Content-Type: application/json
Content-Type: application/http
Content-Transfer-Encoding: binary

POST https://mohannadghawi.sharepoint.com/_api/web/lists/GetByTitle('GroupNamePrincipleID')/items(3)/roleassignments/addroleassignment(PrincipalId=2047,roleDefId=1073741826) HTTP/1.1

--changeset_1234--
--batch_1234--

EDIT-4 This worked for me :-

--batch_1234
Content-Type: multipart/mixed; boundary="changeset_1234"

--changeset_1234
Content-Type: application/http
Content-Transfer-Encoding: binary

POST https://**.sharepoint.com/_api/web/lists/GetByTitle('GroupNamePrincipleID')/items(1)/roleassignments/addroleassignment(PrincipalId=2047,roleDefId=1073741826) HTTP/1.1

--changeset_1234
Content-Type: application/http
Content-Transfer-Encoding: binary

POST https://**.sharepoint.com/_api/web/lists/GetByTitle('GroupNamePrincipleID')/items(2)/roleassignments/addroleassignment(PrincipalId=2047,roleDefId=1073741826) HTTP/1.1

--changeset_1234
Content-Type: application/http
Content-Transfer-Encoding: binary

POST https://**.sharepoint.com/_api/web/lists/GetByTitle('GroupNamePrincipleID')/items(3)/roleassignments/addroleassignment(PrincipalId=2047,roleDefId=1073741826) HTTP/1.1

--changeset_1234--

--batch_1234--

Solution

  • Yes, with SharePoint Online REST API you can batch multiple requests in one call. For example, you can call multiple GET to read data, or multiple POST or DELETE to edit or delete items on a SharePoint list.

    References:

    Sample from Microsoft Learn:

    POST https://fabrikam.sharepoint.com/_api/$batch HTTP/1.1
    Content-Type: multipart/mixed; boundary=batch_e3b6819b-13c3-43bb-85b2-24b14122fed1
    
    --batch_e3b6819b-13c3-43bb-85b2-24b14122fed1
    Content-Type: application/http
    Content-Transfer-Encoding: binary
    
    GET https://fabrikam.sharepoint.com/_api/Web/lists/getbytitle('Composed%20Looks')/items?$select=Title HTTP/1.1
    
    --batch_e3b6819b-13c3-43bb-85b2-24b14122fed1
    Content-Type: application/http
    Content-Transfer-Encoding: binary
    
    GET https://fabrikam.sharepoint.com/_api/Web/lists/getbytitle('User%20Information%20List')/items?$select=Title HTTP/1.1
    
    --batch_e3b6819b-13c3-43bb-85b2-24b14122fed1--
    

    In Power Automate, with the action Send an HTTP request to SharePoint you can achieve what you are requesting. The prerequisite is to concatenate your ids with a Select or Compose (+ join method).

    The parameters should be something like that:

    Body:

    --batch_1234
    Content-Type: multipart/mixed; boundary=changeset_1234
    
    --changeset_1234
    Content-Type: application/http
    Content-Transfer-Encoding: binary
    
    POST https://your-tenant.sharepoint.com/sites/your-site/_api/web/lists/GetByTitle('Your-List')/items(123)/roleassignments/addroleassignment(PrincipalId=1,roleDefId=1073741826) HTTP/1.1
    
    --changeset_1234
    Content-Type: application/http
    Content-Transfer-Encoding: binary
    
    POST https://your-tenant.sharepoint.com/sites/your-site/_api/web/lists/GetByTitle('Your-List')/items(123)/roleassignments/addroleassignment(PrincipalId=2,roleDefId=1073741826) HTTP/1.1
    
    --changeset_1234
    Content-Type: application/http
    Content-Transfer-Encoding: binary
    
    POST https://your-tenant.sharepoint.com/sites/your-site/_api/web/lists/GetByTitle('Your-List')/items(123)/roleassignments/addroleassignment(PrincipalId=3,roleDefId=1073741826) HTTP/1.1
    
    --changeset_1234--
    --batch_1234--
    

    Screenshots of the results (in this sample, I give 3 permissions to the same user, but you can give the same permission to 3 users, it's working the same way) Flow definition Flow result SharePoint result

    Usefull article: Writing batch SharePoint API calls in Power Automate