I am rather new to this place but quite few previous posts helped me out a great deal while creating some PowerShell scripts. I have been trying to create a script that will create a couple of folders into my document library in my SharePoint Online site. I managed to make it create the folders and break the role inheritance with existing permissions wiped after creation. Although I am struggling to make it add the permissions as I end up receiving an error.
The error is as follows:
format-default : The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. + CategoryInfo : NotSpecified: (:) [format-default], CollectionNotInitializedException + FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException,Microsoft.PowerShell.Commands.FormatDefau ltCommand
I have read a couple of other posts saying that this error means that the specific collections are not loaded but as far as I can see I have loaded in everything I can to the $ClientContext
.
Any help finding this collection not initialized is greatly appreciated! Find the code below.
### 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 = '[email protected]'
### 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)
#Add permissions to the folders
$folder1.ListItemAllFields.RoleAssignments.Add($usr,$usrRole)
$folder2.ListItemAllFields.RoleAssignments.Add($usr,$usrRole)
$clientContext.Load($folder1)
$clientContext.Load($folder2)
$clientContext.ExecuteQuery()
}
#Execute the function
CreateFolder