Forum Discussion
Tobey Davies
Jun 30, 2017Brass Contributor
SharePoint Online CSOM PowerShell Script to create folders and add permissions
Hi all!
I am rather new to this place I would like to ask a question about CSOM.
I'd like to set unique permissions for a particular folder in a particular document, then I found the following URL on the internet.
【SharePoint Online CSOM PowerShell Script to create folders and add permissions】
Https://sharepoint.stackexchange.com/questions/178984/sharepoint-online-csom-powershell-script-to-create-folders-and-add-permissions
After referring to and executing the inside command, the following error occurred.
ERROR:
Exception calling "ExecuteQuery" with "0" argument(s): "Cannot add a role assignment with empty role definition binding collection."
I executed the below command:
### Get the user credentials
$credential=Get-Credential
$username=$credential.UserName
$password=$credential.GetNetworkCredential().Password
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
### Input Parameters
$url = 'https://aareSharePoint.sharepoint.com/sites/muhsite/'
$usrName = 'Aare Test'
$email = 'aare@aareDomain.com'
### References
# Specified the paths where the dll's are located.
Add-Type -Path 'c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
### CreateFolder Function
function CreateFolder()
{
# Connect to SharePoint Online and get ClientContext object.
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
# Get the SharePoint web
$web=$clientContext.Web;
$clientContext.Load($web)
$clientContext.ExecuteQuery()
# Adds the folder that is located at the specified URL to the collection
$folder1=$web.Folders.Add('Library1/'+$usrName)
$folder2=$web.Folders.Add('Library2/'+$usrName)
$clientContext.Load($folder1)
$clientContext.Load($folder2)
$clientContext.ExecuteQuery()
#Get the role required and load it
$role = $web.RoleDefinitions.GetByName('Edit')
$usrRole = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($clientContext)
$usrRole.Add($role)
$clientContext.Load($role)
$clientContext.Load($usrRole)
#Get the user from the $web
$usr = $Web.EnsureUser('i:0#.f|membership|'+$email)
Write-Host $usrRole.Description
$clientContext.Load($usr)
$clientContext.ExecuteQuery()
# Display the folder name and URL
Write-Host -ForegroundColor Green 'Folder Name: ' $folder1.Name ' URL: '$folder1.ServerRelativeUrl;
Write-Host -ForegroundColor Green 'Folder Name: ' $folder2.Name ' URL: '$folder2.ServerRelativeUrl;
#Break inheritance and remove existing permissions
$folder1.ListItemAllFields.BreakRoleInheritance($false, $true)
$folder2.ListItemAllFields.BreakRoleInheritance($false, $true)
# Apply the permission roles to the list.
$clientContext.Load($folder1.ListItemAllFields.RoleAssignments.Add($usr, $usrRole))
$clientContext.Load($folder2.ListItemAllFields.RoleAssignments.Add($usr, $usrRole))
$folder1.Update()
$folder2.Update()
$clientContext.ExecuteQuery()
}
#Execute the function
CreateFolder
How can I resolve the above error?
Thanks!
Tobey.
No RepliesBe the first to reply