Partager via


Manage Web Part Permissions using PowerShell

 

PowerShell script to manage permissions of web parts in Web Part gallery.

# Get Web Object

$web = Get-SPWeb "Web URL"

# Get Web Part Gallery list

# SPListTemplateType.WebPartCatalog Enum value 113

$list = $web.GetCatalog(113)

$listItemCollections = $list.GetItems()

$listItemCollections | ForEach-Object {

$item = $listItemCollections | Where-Object {$_.Name -match "ContentQuery.webpart"}

}

$item.BreakRoleInheritance($true)

# Remove existing group permission

$group = $web.SiteGroups["Viewers"]

$item.RoleAssignments.Remove($group)

# Add user permission to the web part item

$account = $web.EnsureUser("domain\user")

$role = $web.RoleDefinitions["Contribute"]

$assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($account)

$assignment.RoleDefinitionBindings.Add($role)

$item.RoleAssignments.Add($assignment)

$item.Update()