0

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
  • $FPfolder is a collection. You cannot add Role Assignment to a collection
    – Raf
    Dec 9, 2019 at 10:12

2 Answers 2

0

you can you something like that

function GrantUserpermission($spweb,$spfolder, $userName){

    if($spweb.SiteUsers[$userName]){
        $assignment=new-object Microsoft.SharePoint.SPRoleAssignment($spweb.SiteUsers[$userName])
        $assignment.RoleDefinitionBindings.Add($spweb.RoleDefinitions["Contribute"])
        $spfolder.BreakRoleInheritance("true")
        $spfolder.RoleAssignments.Add($assignment);
        Write-Host "Permission provided for user ", $userName
    }else{
        Write-Host "User ""$userName"" was not found in this web!"
    }
}
0

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

It will looks like:

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)[0]

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.