简体   繁体   中英

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

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()
        }
    }
}

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()
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM