Answered by:
Adding permission to list using powershell

Question
-
I have created a site template with unique permissions on a number of lists.
However, saving as a site template loses all unique permissions, therefore I need a quick method of reapplying these permissions on new sites created from the template.
Does anyone know of a powershell script that would do this?
e.g. stop the list inheriting from parent, remove existing user permissions and then add new users with set permissions.
Any ideas?
Friday, February 18, 2011 1:50 PM
Answers
-
I found this thread, it refers to SharePoint 2007 but it still applies. http://social.msdn.microsoft.com/Forums/en/sharepointdevelopment/thread/7e50438c-2069-473e-80f4-8049aa8a4ecd
Add-PSSnapin Microsoft.SharePoint.Powershell
$web = Get-SPWeb http://sp-2010
$account = $web.EnsureUser("SHAREPOINT\mray")
$role = $web.RoleDefinitions["Contribute"]$list = $web.Lists["Shared Documents"]
$list.BreakRoleInheritance($true)
$assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($account)
$assignment.RoleDefinitionBindings.Add($role)
$list.RoleAssignments.Add($assignment)$list.Update()
$web.Dispose()Cheers
Riccardo
fino a quì tutto bene- Marked as answer by Wayne Fan Thursday, February 24, 2011 9:47 AM
Monday, February 21, 2011 11:05 AM
All replies
-
Hi,
Run following command to export the list using permissions
Export-SPWeb -Identity <Site URL> -Path <Path and file name> [-ItemUrl <URL of site, list, or library>] [-IncludeUserSecurity] [-IncludeVersions] [-NoFileCompression] [-GradualDelete] [-Verbose]
And on destination site run following command to import that list
Import-SPWeb -Identity <Site URL> -Path <Export file name> [-Force] [-NoFileCompression] [-Verbose]
Hope this helps
Regards, Pratik Vyas | SharePoint Consultant | http://sharepointpratik.blogspot.com/Friday, February 18, 2011 2:42 PM -
Create a new event receiver for Web Provisioned and run your custom code to apply unique permission to the list.
Web Provisioned
http://philwicklund.com/blog/Pages/Building-SharePoint-Event-Receivers-in-2010.aspx
AmitFriday, February 18, 2011 3:01 PM -
Exporting/importing lists isn't the answer as the site template has many lists and dependent pages.
I need the ability to create a site quickly, hence the site template, but also relink permissions.
Any ideas?
Friday, February 18, 2011 4:03 PM -
Hi Garry, with PowerShell you can do something similar to what you're asking for. The following script adds an user to a list assigning him the Contribute role. It only works when permission inheritance is stopped.
$web = Get-SPWeb http://yourweb
$account = $web.EnsureUser("DOMAIN\User")
$role = $web.RoleDefinitions["Contribute"]$list = $web.Lists["Shared Documents"]
$assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($account)
$assignment.RoleDefinitionBindings.Add($role)
$list.RoleAssignments.Add($assignment)$web.Dispose()
I started from this post http://get-spscripts.com/2011/02/add-sharepoint-or-ad-groupuser-to-all.html.
Cheers
Riccardo
fino a quì tutto beneSaturday, February 19, 2011 9:31 PM -
Thanks Ricardo,
the next question would be...'Is there a PowerShell script to stop inheritance on a list?'
Monday, February 21, 2011 9:08 AM -
I found this thread, it refers to SharePoint 2007 but it still applies. http://social.msdn.microsoft.com/Forums/en/sharepointdevelopment/thread/7e50438c-2069-473e-80f4-8049aa8a4ecd
Add-PSSnapin Microsoft.SharePoint.Powershell
$web = Get-SPWeb http://sp-2010
$account = $web.EnsureUser("SHAREPOINT\mray")
$role = $web.RoleDefinitions["Contribute"]$list = $web.Lists["Shared Documents"]
$list.BreakRoleInheritance($true)
$assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($account)
$assignment.RoleDefinitionBindings.Add($role)
$list.RoleAssignments.Add($assignment)$list.Update()
$web.Dispose()Cheers
Riccardo
fino a quì tutto bene- Marked as answer by Wayne Fan Thursday, February 24, 2011 9:47 AM
Monday, February 21, 2011 11:05 AM -
I am attempting to use Riccardo's code as follows...
$web = Get-SPWeb http://jes-moss-dev/Partnerships
$account = $web.SiteGroups["Executive Admins"]
$role = $web.RoleDefinitions["Contribute"]$list = $web.Lists["Partnership and Legal Documents"]
$assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($account)
$assignment.RoleDefinitionBindings.Add($role)
$list.RoleAssignments.Add($assignment)$web.Dispose()
But whenever I execute it in Windows Powershell ISE, I receive the following error...
You cannot call a method on a null-valued expression.
At C:\Documents and Settings\Administrator.JESMITH\My Documents\WindowsPowerShell\ModLibs.ps1:13 char:26
+ $list.RoleAssignments.Add <<<< ($assignment)
+ CategoryInfo : InvalidOperation: (Add:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNullI have no idea what I can possible be overlooking. Can you please provide some insight and/or guidance.
Thanks in advance!
Wednesday, July 6, 2011 10:47 PM -
Thanks a lot for your helpWednesday, October 22, 2014 6:14 PM