SharePoint Online CSOM PowerShell Script to create folders and add permissions

%3CLINGO-SUB%20id%3D%22lingo-sub-83981%22%20slang%3D%22en-US%22%3ESharePoint%20Online%20CSOM%20PowerShell%20Script%20to%20create%20folders%20and%20add%20permissions%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-83981%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20all!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EI%3C%2FSPAN%3E%3CSPAN%3Eam%20rather%20new%20to%20this%20place%3C%2FSPAN%3E%3CSPAN%3EI%20would%20like%20to%20ask%20a%20question%20about%20CSOM.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3EI'd%20like%20to%20set%20unique%20permissions%20for%20a%20particular%20folder%20in%20a%20particular%20document%2C%20%3C%2FSPAN%3E%3CSPAN%3Ethen%3C%2FSPAN%3E%3CSPAN%3EI%20found%20the%20following%20URL%20on%20the%20%3C%2FSPAN%3E%3CSPAN%3Einternet%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3E%3CSPAN%3E%E3%80%90%3C%2FSPAN%3E%3CSPAN%3ES%3C%2FSPAN%3E%3CSPAN%3EharePoint%20Online%20CSOM%20PowerShell%20Script%20to%20create%20folders%20and%20add%20permissions%3C%2FSPAN%3E%3CSPAN%3E%E3%80%91%3C%2FSPAN%3E%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CA%20href%3D%22Https%3A%2F%2Fsharepoint.stackexchange.com%2Fquestions%2F178984%2Fsharepoint-online-csom-powershell-script-to-create-folders-and-add-permissions%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EHttps%3A%2F%2Fsharepoint.stackexchange.com%2Fquestions%2F178984%2Fsharepoint-online-csom-powershell-script-to-create-folders-and-add-permissions%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAfter%20referring%20to%20and%20executing%20the%20inside%20command%2C%20the%20following%20error%20occurred.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EERROR%3A%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3EException%20calling%20%22ExecuteQuery%22%20with%20%220%22%20argument(s)%3A%20%22Cannot%20add%20a%20role%20assignment%20with%20empty%20role%20definition%20binding%20collection.%22%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F16690i0C1B737535D20DA0%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22ERROR.png%22%20title%3D%22ERROR.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20executed%20the%20below%20command%EF%BC%9A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%23%23%23%20Get%20the%20user%20credentials%0A%24credential%3DGet-Credential%0A%24username%3D%24credential.UserName%0A%24password%3D%24credential.GetNetworkCredential().Password%0A%24securePassword%20%3D%20ConvertTo-SecureString%20%24password%20-AsPlainText%20-Force%0A%26nbsp%3B%0A%23%23%23%20Input%20Parameters%0A%24url%20%3D%20'https%3A%2F%2FaareSharePoint.sharepoint.com%2Fsites%2Fmuhsite%2F'%0A%24usrName%20%3D%20'Aare%20Test'%0A%24email%20%3D%20'aare%40aareDomain.com'%0A%26nbsp%3B%0A%23%23%23%20References%0A%23%20Specified%20the%20paths%20where%20the%20dll's%20are%20located.%0AAdd-Type%20-Path%20'c%3A%5CProgram%20Files%5CCommon%20Files%5Cmicrosoft%20shared%5CWeb%20Server%20Extensions%5C15%5CISAPI%5CMicrosoft.SharePoint.Client.dll'%0AAdd-Type%20-Path%20'c%3A%5CProgram%20Files%5CCommon%20Files%5Cmicrosoft%20shared%5CWeb%20Server%20Extensions%5C15%5CISAPI%5CMicrosoft.SharePoint.Client.Runtime.dll'%0A%26nbsp%3B%0A%26nbsp%3B%0A%23%23%23%20CreateFolder%20Function%0Afunction%20CreateFolder()%0A%7B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%23%20Connect%20to%20SharePoint%20Online%20and%20get%20ClientContext%20object.%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24clientContext%20%3D%20New-Object%20Microsoft.SharePoint.Client.ClientContext(%24url)%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24credentials%20%3D%20New-Object%20Microsoft.SharePoint.Client.SharePointOnlineCredentials(%24username%2C%20%24securePassword)%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24clientContext.Credentials%20%3D%20%24credentials%0A%26nbsp%3B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%23%20Get%20the%20SharePoint%20web%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24web%3D%24clientContext.Web%3B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24clientContext.Load(%24web)%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24clientContext.ExecuteQuery()%0A%26nbsp%3B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%23%20Adds%20the%20folder%20that%20is%20located%20at%20the%20specified%20URL%20to%20the%20collection%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24folder1%3D%24web.Folders.Add('Library1%2F'%2B%24usrName)%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24folder2%3D%24web.Folders.Add('Library2%2F'%2B%24usrName)%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24clientContext.Load(%24folder1)%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24clientContext.Load(%24folder2)%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24clientContext.ExecuteQuery()%0A%26nbsp%3B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%23Get%20the%20role%20required%20and%20load%20it%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24role%20%3D%20%24web.RoleDefinitions.GetByName('Edit')%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24usrRole%20%3D%20New-Object%20Microsoft.SharePoint.Client.RoleDefinitionBindingCollection(%24clientContext)%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24usrRole.Add(%24role)%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24clientContext.Load(%24role)%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24clientContext.Load(%24usrRole)%0A%26nbsp%3B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%23Get%20the%20user%20from%20the%20%24web%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24usr%20%3D%20%24Web.EnsureUser('i%3A0%23.f%7Cmembership%7C'%2B%24email)%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Write-Host%20%24usrRole.Description%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24clientContext.Load(%24usr)%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24clientContext.ExecuteQuery()%0A%26nbsp%3B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%23%20Display%20the%20folder%20name%20and%20URL%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Write-Host%20-ForegroundColor%20Green%20'Folder%20Name%3A%20'%20%24folder1.Name%20'%20URL%3A%20'%24folder1.ServerRelativeUrl%3B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Write-Host%20-ForegroundColor%20Green%20'Folder%20Name%3A%20'%20%24folder2.Name%20'%20URL%3A%20'%24folder2.ServerRelativeUrl%3B%0A%26nbsp%3B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%23Break%20inheritance%20and%20remove%20existing%20permissions%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24folder1.ListItemAllFields.BreakRoleInheritance(%24false%2C%20%24true)%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24folder2.ListItemAllFields.BreakRoleInheritance(%24false%2C%20%24true)%0A%26nbsp%3B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%23%20Apply%20the%20permission%20roles%20to%20the%20list.%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24clientContext.Load(%24folder1.ListItemAllFields.RoleAssignments.Add(%24usr%2C%20%24usrRole))%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24clientContext.Load(%24folder2.ListItemAllFields.RoleAssignments.Add(%24usr%2C%20%24usrRole))%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24folder1.Update()%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24folder2.Update()%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24clientContext.ExecuteQuery()%0A%26nbsp%3B%0A%7D%0A%23Execute%20the%20function%0ACreateFolder%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHow%20can%20I%20resolve%20the%20above%20error%3F%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EThanks!%3C%2FP%3E%3CP%3ETobey.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Contributor

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-cr...

 

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."

ERROR.png

 

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.

0 Replies