none
powershell and list-item permissions

    Question

  • Hello,

    can somebody give me some examples using powershell and item level permissions in a list?
    We want to change permissions item-level based in a sharepoint list.
    Goal is to give some users or sharepoint groups specific permissions to only specific items in a sharepoint list.

    Thank you

    Regards

    Monday, January 24, 2011 9:24 PM

Answers

  • Hi Mark,

    Here's some code that might be of use:

    $site = get-spsite("http://joe7svaio")
    $web = $site.RootWeb
    $list = $web.Lists["MyList"]
    $listitem = $list.Items[0]
    
    if($listitem.HasUniqueRoleAssignments -eq $false) {
     $listitem.BreakRoleInheritance($true)
    }
    
    $user = $web.AllUsers["AD\Joel"]
    $roledef = $web.RoleDefinitions["Full Control"]
    $roleass = New-Object Microsoft.SharePoint.SPRoleAssignment($user)
    $roleass.RoleDefinitionBindings.Add($roledef)
    $listitem.RoleAssignments.Add($roleass)
    $listitem.Update()
    
    

    Hope this helps!


    joel
    Joel's SharePoint Architect Blog
    joelblogs.co.uk
    Want SharePoint Training?
    • Marked as answer by Marc Moennikes Tuesday, January 25, 2011 10:00 AM
    Monday, January 24, 2011 10:00 PM

All replies

  • Hi Mark,

    Here's some code that might be of use:

    $site = get-spsite("http://joe7svaio")
    $web = $site.RootWeb
    $list = $web.Lists["MyList"]
    $listitem = $list.Items[0]
    
    if($listitem.HasUniqueRoleAssignments -eq $false) {
     $listitem.BreakRoleInheritance($true)
    }
    
    $user = $web.AllUsers["AD\Joel"]
    $roledef = $web.RoleDefinitions["Full Control"]
    $roleass = New-Object Microsoft.SharePoint.SPRoleAssignment($user)
    $roleass.RoleDefinitionBindings.Add($roledef)
    $listitem.RoleAssignments.Add($roleass)
    $listitem.Update()
    
    

    Hope this helps!


    joel
    Joel's SharePoint Architect Blog
    joelblogs.co.uk
    Want SharePoint Training?
    • Marked as answer by Marc Moennikes Tuesday, January 25, 2011 10:00 AM
    Monday, January 24, 2011 10:00 PM
  • Thank you

    Regards

    Tuesday, January 25, 2011 10:01 AM
  • Hi Joel,

    I tested your scripts

    $list = $web.Lists["MyList"]
    $listitem = $list.Items[0]

    In my example I want to set item permission of "Site Pages"

    I cant index like $list = $web.Lists["Site Pages"]

    but I can use $list=$web.Lists[4], for this part, that is OK, The probelm is I cant use $listitem = $list.Items[0] to get the list item, It always return null value. Do you have any ideas what cause this problem?

    Tuesday, August 09, 2011 4:11 AM
  • you may check $list.Items.Count, which may return 0.
    Friday, September 02, 2011 12:31 PM
  • Hi David, 0 is not a valid ID. The first item that is created has the ID 1! If you want to iterate through all list items try "foreach".
    Thursday, July 12, 2012 7:11 AM