Collection was of a fixed size Error when Adding New User to SharePoint object

3.3k views Asked by At

I encountered the following error when adding a user to a SharePoint folder object:

Exception calling "Add" with "1" argument(s): "Collection was of a fixed size."

At D:\User\Script1.ps1:114 char:17 $FPfolder.RoleAssignments.Add($assignment)

  • CategoryInfo: NotSpecified: (:) [], MethodInvocationException
  • FullyQualifiedErrorId : NotSupportedException

Below is my code snippet:

ForEach ($FPfolderId in $FPfolderSplit)
{
    $query = New-Object Microsoft.SharePoint.SPQuery
    $query.ViewXml = "@<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>$FPfolderId</Value></Eq></Where></Query></View>"

    $FPfolder = $FPlist.GetItems($query)

    foreach($role in $FPfolder.RoleAssignments)  
    {
        if ($role.Member.Name.Equals($userToAction))
        {
            $FPfolder.BreakRoleInheritance($true)
            $account = $web.EnsureUser("User1")
            $assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($account)
            $FPfolder.RoleAssignments.Add($assignment)
            $FPfolder.Update()
        }
    }
}
1

There are 1 answers

0
Raf On BEST ANSWER

I have posted answer on your question here before

In your case $FPfolder is a collection of ListItems. You cannot add Role Assignment to a collection.

If you are sure that $FPfolder is a collection which contains only one element(folder) just add [0] after $FPlist.GetItems($query).

or you can rename $FPfolder to $FPfolders and add additional foreach loop to iterate SP folders:

ForEach ($FPfolderId in $FPfolderSplit)
{
    $query = New-Object Microsoft.SharePoint.SPQuery
    $query.ViewXml = "@<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>$FPfolderId</Value></Eq></Where></Query></View>"

    $FPfolders = $FPlist.GetItems($query)
    foreach($FPfolder in $FPfolders )
    {
        foreach($role in $FPfolder.RoleAssignments) 
        {
            if ($role.Member.Name.Equals($userToAction))
            {
                $FPfolder.BreakRoleInheritance($true)
                $account = $web.EnsureUser("User1")
                $assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($account)
                $FPfolder.RoleAssignments.Add($assignment)
                $FPfolder.Update()
            }
        }
    }
}