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.
You need to take advantage of [SPQuery class](http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spquery.aspx). Using CAML query is much, much, much more faster then doing foreach.

Here is script you 'really' want:

```
$webUrl = "http://inside.national.com/Sales"
$listName = "2011Sales"
$ctname = "2011Sales"
$groupname = "Custom SP Group"

# Get web and list
$web = Get-SPWeb -Identity $webUrl
$list = $web.Lists[$listName]

# Build CAML query
$caml="<Where><Eq><FieldRef Name='ContentType'/><Value Type='Text'>{0}</Value></Eq></Where>" -f $ctname
$query = New-Object Microsoft.SharePoint.SPQuery
$query.Query = $caml
# Set scope to Recursive to include all items from folders and Document Sets
$query.ViewAttributes = "Scope='Recursive'"
# Get our data (only items with proper ct name)
$items = $list.GetItems($query)

# Set Role Assignment
$spGroup = $web.SiteGroups[$groupname]
$spRoleAssignment = New-Object Microsoft.SharePoint.SPRoleAssignment($spGroup)
$sproledefinition = $web.RoleDefinitions["Contribute"]
$sproleassignment.RoleDefinitionBindings.Add($sproledefinition)

foreach ($item in $items) {
    # This is the fastest way of deleting all permissions
    # First you restore inheritance
    # And then you break it
    $item.ResetRoleInheritance()
    $item.BreakRoleInheritance($false)

    # Add new permission
    $item.RoleAssignments.Add($spRoleAssignment)
}

```

This script will update all items of desired Content Type (including items/files in folders and files in Document Sets). However it will not change permissions of parent Folders or Document Sets.

I have tested this on my dev machine. Feel free to ask additional questions.