title: Set permissions within an event receiver(itemupdated)
body: <p>I've searched for a long time but every single solution I've found didn't work. 
  Here is the process:
  Create a Page (ListItem) -> Change the FieldValues -> CheckIn -> Error</p>
  
  <p>Error:
  System.ArgumentOutOfRangeException</p>
  
  <p>Code:</p>
  
  <pre><code>public override void ItemUpdated(SPItemEventProperties properties, bool isCheckedIn)
      {//FUNKTIONSFÄHIG
          EventFiringEnabled = false;
          if (isCheckedIn)
          {
              SetRights(properties);
          }
          else
          {
              return;
          }
          EventFiringEnabled = true;
      }
  
      public static void AssignPermissionsToItem(SPListItem item, SPPrincipal obj, SPRoleType roleType)
      {//FUNKTIONSFÄHIG
          SPRoleAssignment roleAssignment = new SPRoleAssignment(obj);
          SPRoleDefinition roleDefinition =        item.Web.RoleDefinitions.GetByType(roleType);
          roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
  
          item.RoleAssignments.Add(roleAssignment);
          //item.Update();
      }
  
      private static void SetRights(SPItemEventProperties properties) 
      {//FUNKTIONSFÄHIG
          SPListItem currentItem = properties.ListItem; 
          SPSecurity.RunWithElevatedPrivileges(delegate()
          {
              SPUserToken sysToken = null;
              using (SPSite site = new SPSite(properties.ListItem.Web.Site.ID))
              {
                  sysToken = site.UserToken;
  
                  using (SPSite impersonatedSite = new SPSite(properties.ListItem.Web.Site.ID, sysToken))
                  {
                      bool siteUnsafeUpdates = impersonatedSite.AllowUnsafeUpdates;
                      impersonatedSite.AllowUnsafeUpdates = true;
                      using (SPWeb web = impersonatedSite.OpenWeb(properties.ListItem.Web.ID))
                      {
                          SPUser user = web.CurrentUser;
                          SPList elevatedList = web.Lists[currentItem.ParentList.ID];
                          try
                          {
  
                              SPList RechteMapping = web.GetList(Configuration.ReadWebConfigAppSetting(web, "WikiRechteMappingList"));
                              String TaxonomieSpalte = Configuration.ReadWebConfigAppSetting(web, "TaxonomieSpalte");
                              String ADSpalte = Configuration.ReadWebConfigAppSetting(web, "ADSpalte");
                              bool webUnsafeUpdates = web.AllowUnsafeUpdates;
  
                              web.AllowUnsafeUpdates = true;
                              SPListItem li = elevatedList.GetItemById(currentItem.ID);
  
                              li.BreakRoleInheritance(false);
                              while (li.RoleAssignments.Count &gt; 0)
                              {
                                  li.RoleAssignments.Remove(0);
                              }
  
                              // Rechte für aktuellen Bearbeiter
                              AssignPermissionsToItem(li, user, SPRoleType.Contributor);
  
                              // Rechte für Ersteller/Autor
                              AssignPermissionsToItem(li, web.EnsureUser(li["Author"].ToString().Split('#')[1]), SPRoleType.Contributor);
  
                              // Rechte für letzen Bearbeiter
                              AssignPermissionsToItem(li, web.EnsureUser(li["Editor"].ToString().Split('#')[1]), SPRoleType.Contributor);
  
                              // Lese AD-Gruppe für Taxonomieauswahl Verantwortliche Abteilung
                              TaxonomyFieldValue verantwAbteilung = (TaxonomyFieldValue)li["Verantwortliche Abteilung"];
                              SPQuery rechteGruppeQuery = new SPQuery();
                              String queryPre = "&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name='" + TaxonomieSpalte + "' LookupId='True' /&gt;&lt;Value Type='Integer'&gt;";
                              String queryPost = "&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;";
  
                              String query = queryPre + verantwAbteilung.WssId + queryPost;
                              rechteGruppeQuery.Query = query;
                              SPListItemCollection rechte = RechteMapping.GetItems(rechteGruppeQuery);
  
                              // Setze Rechte für AD-gruppe aus Taxonomieauswahl Verantwortliche Abteilung
                              if (rechte.Count &gt; 0)
                              {
                                  user = web.EnsureUser(rechte[0][ADSpalte].ToString().Split('#')[1]);
                                  AssignPermissionsToItem(li, user, SPRoleType.Contributor);
                              }
  
                              // Lese AD-Gruppe für Taxonomieauswahl Betroffene Abteilung
                              TaxonomyFieldValueCollection betroffeneAbteilung = (TaxonomyFieldValueCollection)li["Betroffene Abteilung/gültig für"];
                              if (betroffeneAbteilung != null)
                              {
                                  foreach (TaxonomyFieldValue abtlg in betroffeneAbteilung)
                                  {
                                      // Setze Rechte für AD-gruppe aus Taxonomieauswahl Betroffene Abteilung
                                      query = queryPre + abtlg.WssId + queryPost;
                                      rechteGruppeQuery = new SPQuery();
                                      rechteGruppeQuery.Query = query;
                                      rechte = RechteMapping.GetItems(rechteGruppeQuery);
                                      if (rechte.Count &gt; 0)
                                      {
                                          user = web.EnsureUser(rechte[0][ADSpalte].ToString().Split('#')[1]);
                                          AssignPermissionsToItem(li, user, SPRoleType.Contributor);
                                      }
                                      query = null;
                                      rechte = null;
                                  }
                              }
                              web.AllowUnsafeUpdates = webUnsafeUpdates;
                              web.Dispose();
  
                          }
                          catch (Exception ex)
                          {
                              SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("EventReceiver_WikiBerechtigungen", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, ex.StackTrace);
                          }
                      }
                      impersonatedSite.AllowUnsafeUpdates = siteUnsafeUpdates;
                      impersonatedSite.Dispose();
                  }
              }
          });
      }
  </code></pre>
  
  <p>I've created my own Base-Class to prevent that the event fires two times(While saving the Page and CheckIn). </p>
  
  <p>I don't have a clue where I've made a mistake. Please help me :D </p>