Problem
I needed to grant permission for an Entra Id application to access SharePoint Online and OneDrive folder. I didn't want to grant the broadest Sites.ReadWrite.All permission to all site collections, nor the second broadest permission Sites.Selected to specific site collection. Instead I needed to go very granular, so limit access to specific folder and use the Files.SelectedOperations.Selected permission.
Backgound: Granting permission to whole site collection
1. Get the site collection
First I queried the site (collection) ID for my SharePoint site collection:
HTTP GET https://graph.microsoft.com/v1.0/sites/xyz.sharepoint.com:/sites/jussi
HTTP GET https://graph.microsoft.com/v1.0/sites/[SITE_ID]/permissions
2. Add permissions
HTTP POST https://graph.microsoft.com/v1.0/sites/[SITE_ID]/permissions
with the following payload in the body section of Graph Explorer:
3. Removing permissions
You can remove the permissions too, but for that you need the permission ID. You got the permission ID from the result of the query you used when you added the permission earlier, but in case you missed it, you can list the permissions:
HTTP GET https://graph.microsoft.com/v1.0/sites/[SITE_ID]/permissions
Permission ID is the long non-GUID string like this:
So in order to remove the permission:
HTTP DELETE https://graph.microsoft.com/v1.0/sites/[SITE_ID]/permissions/[PERMISSION_ID]
Granting permission to folder level
Alright, I removed the site collection level permissions and started to grant folder level permissions.
1. Get the drive ID for the library
https://graph.microsoft.com/v1.0/sites/[SITE_ID]/drives
Drive ID is the non-GUID string like:
2. Get the folder ID from the drive by querying all the children items of the folder
HTTP GET https://graph.microsoft.com/v1.0/sites/[SITE_ID]/drives/[DRIVE_ID]/root/children
So, what I now got was the id 01QHBNZNLH6DKOS3GO3FHKHKIC6AGODYML of the folder MyTemplates inside the library Shared Documents, that's the one.
3. Add permissions
HTTP POST https://graph.microsoft.com/v1.0/sites/[SITE_ID]/drives/[DRIVE_ID]/items/[FOLDER_ID]/permissions
using same payload as earlier
╯︿╰ I get "Invalid request" error:
What is this? Documentation is rather brief. There was a great article by Vasil that discusses permissions that indicated that this should (or at least was) possible, so I started testing further, and finally found a solution.
Solution
When granting application permissions to folder objects (probably same goes for files and lists), the payload you send is slightly different. You don't use grantedToIdentities array, but instead grantedTo object:
NOTE! This payload doesn't work when granting permission on a site level.
No comments:
Post a Comment