i am stuck to an issue where i have a List ID - list of Ids i have to find the LoginName of SPUser or SPGroup with the id in the ListID

i tried the following but it threws exception as it did not find user when group id is there

```
              foreach (var id in ID)
                {
                    SPUser spUser = web.SiteUsers.GetByID(Convert.ToInt32(id));
                    if (spUser != null)
                    {
                        lstUsers.Add(spUser.LoginName);
                         continue;
                    }
                    SPGroup spGroup = web.SiteGroups.GetByID(Convert.ToInt32(id ));
                    if (spGroup != null)
                    {
                        lstGroups.Add(spGroup.LoginName);
                         continue;

                    }

                }

```

please suggest what to do !!!
['https://sharepoint.stackexchange.com/questions/19778', 'https://sharepoint.stackexchange.com', 'https://sharepoint.stackexchange.com/users/2310/']
After breaking the inheritance of an item (item,list,web) you have to call `update()` on that object to make sure the settings are saved to the database. Once these are saved you can start adding/changing the permissions for that item.

BreakRoleInheritance(false) will not copy the permissions of the parent to the new permissionset, supplying true will copy the permissions.

**Updates** (see also Vedran Rasols [answer](https://sharepoint.stackexchange.com/questions/19771/powershell-item-level-permission-by-content-type/19792#19792))

* Using `foreach(item in list.items)` is not recommended, use an SPQuery to select the items and properties you really want, otherwise you will [fetch all data from the DB](https://web.archive.org/web/20120729085025/http://blog.dynatrace.com:80/2009/01/13/sharepoint-only-request-data-that-you-really-need/). See also this [MSDN Best Practices Article](https://docs.microsoft.com/en-us/previous-versions/office/developer/sharepoint-services/bb687949%28v=office.12%29#WorkingWithFoldersLists)!
* In theory you shouldn't have to call `update()`, but in some strange way in helped solving this issue.

**bonus:** Note that when breaking inheritance on lists [the AllowUnsafeUpdate property of the parent web reverts to false](https://www.wictorwilen.se/Post/BreakRoleInheritance-and-AllowUnsafeUpdates.aspx), which can be annoying.
Here is the working code in case someone needs it.

```
$webUrl = "http://inside.national.com/Sales"
$web = Get-SPWeb $webUrl
$list = $web.Lists["2011Sales"]
$ct = "2011 Marketing"
$spgroup = "Custom SP Group"
$rd="Contribute"

foreach ($item in $list.items) {
    If ($item.ContentType.Name -eq $ct)
     {  
        $item.ResetRoleInheritance() # Not sure if this line is needed
        $item.BreakRoleInheritance($false) # I tried $true but did not work, so leave it at $false
        $item.SystemUpdate() # Very Important

        $group = $web.AllUsers[$spgroup]
        $roledef = $web.RoleDefinitions[$rd]
        $roleass = New-Object Microsoft.SharePoint.SPRoleAssignment($web.SiteGroups[$spgroup])
        $roleass.RoleDefinitionBindings.Add($roledef)
        $item.RoleAssignments.Add($roleass)
        $item.SystemUpdate() #Update item without changing the updated date
        Write-Host $item.Name " permission applied"
     }
}
$web.dispose()

```