0

I'm trying to add a SharePoint group to a specific list that exists on all the sites in a collection. I would think this would be easier but I'm having difficulties. Inheritance is already broken on the list.

Below is my code and then the error I'm getting.

$siteNumbers = "2210"
$listsOnSite = "QWERTY"

Foreach($wnl in $siteNumbers)
    {
    $rootWeb = (Get-SPWeb ("http://url/to/site/" + $wnl))


    Foreach($lst in $listsOnSite)


    {
    $list = $rootWeb.Lists[$lst]
    $group = $rootWeb.Groups["Global Read-Only QWERTY"]
    $role = $rootWeb.RoleDefinitions["Read"]

    $roleAssignment = New-Object Microsoft.SharePoint.SPRoleAssignment($group)
    $RoleAssignment.RoleDefinitionBindings.Add($role)
    $rootWeb.RoleAssignments.Add($RoleAssignment)
    $list.RoleAssignments.Add($roleAssignment)
    $list.Update()
    }
    $rootWeb.Update()
    }

New-Object : Constructor not found. Cannot find an appropriate constructor for type Microsoft.SharePoint.SPRoleAssignment. At line:9 char:33 + $roleAssignment = New-Object <<<< Microsoft.SharePoint.SPRoleAssignment( $group) + CategoryInfo : ObjectNotFound: (:) [New-Object], PSArgumentExce ption + FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.C
ommands.NewObjectCommand

Exception calling "Add" with "1" argument(s): "The permission level specified i s already added to the collection." At line:10 char:47 + $RoleAssignment.RoleDefinitionBindings.Add <<<< ($role) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException

You cannot call a method on a null-valued expression. At line:12 char:30 + $list.RoleAssignments.Add <<<< ($roleAssignment) + CategoryInfo : InvalidOperation: (Add:String) [], RuntimeExcept ion + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression. At line:13 char:17 + $list.Update <<<< () + CategoryInfo : InvalidOperation: (Update:String) [], RuntimeExc eption + FullyQualifiedErrorId : InvokeMethodOnNull

  • I was able to reproduce the "Constructor not found" exception by setting $group to null. Are you sure the "Global Read-Only QWERTY" group exists? – Jim Barntish Oct 24 '17 at 16:24
  • I'm positive. I ran a script to verify. 2210 contains the following groups ======================== Global Read-Only QWERTY Main Members Main Owners Main Visitors – NMJohns Oct 24 '17 at 17:03
  • 2
1

Use this

$roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment([Microsoft.SharePoint.SPPrincipal]$group) 

Instead of

$roleAssignment = New-Object Microsoft.SharePoint.SPRoleAssignment($group)

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.