SharePoint REST API - Granting access to a User fails with '400 Bad Request'

Millennium Falcon 1 Reputation point
2022-12-20T16:18:29.14+00:00

I am using the following REST API URI to grant access on a given folder to a given user. It was working earlier but failing now -

<hostName>/_api/web/GetFolderByServerRelativeUrl('<pathToFolder>')/ListItemAllFields/roleAssignments/addroleassignment(principalid=<principalIdVariable>,roleDefId=1073741827)

I was able to verify that the principalId of the user exists.

There are other REST API URIs that I have been using which are working without any issues like - creating SP folders, getting files in a given SP Folder, granting access on a given Folder to a SP user group.
But the granting access to a user has been failing.

Please advise what could be going wrong in this case?

Microsoft 365 and Office SharePoint For business Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2022-12-21T02:38:34.55+00:00

    Hi @Millennium Falcon
    When a new SharePoint object is created, by default, Permission inheritance occurs. All SharePoint objects will be created within the context of a hierarchical tree. Unless the inheritance structure is broken, all SharePoint objects inherit permissions from its parent in the hierarchy.

    Permission inheritance enables user to make the assignment of permission just once, and have that permission trickle down to all sites, lists, libraries, folders and items that inherit permissions from its parent. However as part of security management there are scenarios where we need to implement unique permissions to a particular site or list. We will need break the inheritance using the BreakRoleInheritance method of REST API.

    Please try to use following api first

    /_api/web/GetFolderByServerRelativeUrl('<pathToFolder>')/ListItemAllFields/breakroleinheritance(true)  
    

    Then you can use the api to add roleassignment

    Here is a similar issue with yours. Please make a reference
    https://sharepoint.stackexchange.com/questions/113317/manage-file-permissions-using-rest-api/113322#113322


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.