简体   繁体   English

将新用户添加到 SharePoint 对象时,集合具有固定大小错误

[英]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:将用户添加到 SharePoint 文件夹对象时遇到以下错误:

Exception calling "Add" with "1" argument(s): "Collection was of a fixed size."使用“1”个参数调用“Add”的异常:“集合的大小是固定的。”

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

  • CategoryInfo: NotSpecified: (:) [], MethodInvocationException CategoryInfo: NotSpecified: (:) [], MethodInvocationException
  • FullyQualifiedErrorId : NotSupportedException FullQualifiedErrorId : 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.在您的情况下, $FPfolder是 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) .如果您确定$FPfolder是一个仅包含一个元素(文件夹)的集合,只需在$FPlist.GetItems($query)之后添加[0]

or you can rename $FPfolder to $FPfolders and add additional foreach loop to iterate SP folders:或者您可以将$FPfolder重命名$FPfolder $FPfolders并添加额外的 foreach 循环来迭代 SP 文件夹:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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