title: InvalidOperationException: Operation is not valid due to the current state of the object
body: <p>I'm trying to update item security while adding new item but getting following. How to find the problem from this trace ? </p>
  
  <pre><code>InvalidOperationException: Operation is not valid due to the current state of the object.]
     Microsoft.SharePoint.SPListItem.InitSecurity() +26790031
     Microsoft.SharePoint.SPListItem.get_SecurableObjectImpl() +16
     Microsoft.SharePoint.SPSecurableObject.get_RoleAssignments() +10
     Updateroutine.&lt;&gt;c__DisplayClass2.&lt;updatelistpermission&gt;b__0() +1955
     Microsoft.SharePoint.&lt;&gt;c__DisplayClass4.&lt;RunWithElevatedPrivileges&gt;b__2() +729
     Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode) +27947702
     Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param) +27648393
     Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode) +93
  </code></pre>
  
  <p>Here is my function,</p>
  
  <pre><code>private static void updatelistpermission(Header objEntity, SPListItem lstItem,string strStatus)
          {
  
              SPUserToken token = lstItem.Web.Site.SystemAccount.UserToken;
  
              try
              {
  
                  SPSecurity.RunWithElevatedPrivileges(delegate()
                  {
  
                      using (SPSite site = new SPSite(lstItem.Web.Site.ID, token))
                      {
                          site.AllowUnsafeUpdates = true;
                          using (SPWeb web = site.OpenWeb(lstItem.Web.ID))
                          {
                              web.AllowUnsafeUpdates = true;
                              lstItem.BreakRoleInheritance(false); 
  
                              SPRoleDefinition roleUserDefintion = null;
                              SPRoleDefinition roleSupervisorDefintion = null;
                              SPRoleDefinition roleOpManagerDefintion = null;
  
                              if (strStatus == "Active")
                              {
                                  roleUserDefintion = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
                                  roleSupervisorDefintion = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                                  roleOpManagerDefintion = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                              }
                              else if (strStatus == "Submitted")
                              {
                                  roleUserDefintion = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                                  roleSupervisorDefintion = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
                                  roleOpManagerDefintion = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
                              }
                              else if (strStatus == "Completed")
                              {
                                  roleUserDefintion = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                                  roleSupervisorDefintion = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                                  roleOpManagerDefintion = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                              }
  
                              SPUser uParticipant = Utility.convertStringToSPUser(Utility.getSPStringLookupFromListGLT(objEntity.Participant));
                              SPRoleAssignment roleUserAssignment = new SPRoleAssignment(uParticipant);
  
                              SPUser uSupervisor = Utility.convertStringToSPUser(Utility.getSPStringLookupFromListGLT(objEntity.Supervisor));
                              SPRoleAssignment roleSupervisorAssignment = new SPRoleAssignment(uSupervisor);
  
                              SPUser uOpManager = Utility.convertStringToSPUser(Utility.getSPStringLookupFromListGLT(objEntity.OpsManager));
                              SPRoleAssignment roleuOpManagerAssignment = new SPRoleAssignment(uOpManager);
  
                              roleUserAssignment.RoleDefinitionBindings.Add(roleUserDefintion);
                              lstItem.RoleAssignments.Add(roleUserAssignment);
  
                              roleSupervisorAssignment.RoleDefinitionBindings.Add(roleSupervisorDefintion);
                              lstItem.RoleAssignments.Add(roleSupervisorAssignment);
  
                              roleuOpManagerAssignment.RoleDefinitionBindings.Add(roleOpManagerDefintion);
                              lstItem.RoleAssignments.Add(roleuOpManagerAssignment);
  
                              lstItem.Update();
                              web.AllowUnsafeUpdates = false;
                          }
                      }
                 });
              }
              catch (SPException spex)
              {
                  SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("APP", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, spex.Message , "");
              }
          }
  </code></pre>
  
  <p><strong>New code suggested by @Dmitry Kozlov  but still the same error</strong> </p>
  
  <pre><code>try
                  {
                      {
                          SPUserToken token = SPContext.Current.Web.Site.SystemAccount.UserToken;
  
                          SPSecurity.RunWithElevatedPrivileges(delegate()
                          {
                              using (SPSite site = new SPSite(SPContext.Current.Web.Site.ID, token))
                              {
                                  site.AllowUnsafeUpdates = true;
  
                                  using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
                                  {
                                      web.AllowUnsafeUpdates = true;
                                      web.BreakRoleInheritance(true);
                                      web.AllowUnsafeUpdates = true;
  
                                      SPList lstProject = web.Lists["SharedSurveyHeader"];
                                      SPListItem lstItem = lstProject.Items.Add();
                                      loadToListItem(sharedHeader, ref lstItem, true);
                                      updatelistpermission(web, sharedHeader, lstItem, strStatus);
                                      lstItem.Update();
                                      objWeb.AllowUnsafeUpdates = false;
                                      intResult = lstItem.ID;
                                      lstItem = null;
                                      lstProject = null;
                                  }
                              }
                          });
  
  
                      }
  
                   }
  
  
  private static void updatelistpermission(SPWeb web, SharedSurveyHeader objEntity, SPListItem lstItem,string strStatus)
          {
               try
              {
  
                              SPRoleDefinition roleUserDefintion = null;
                              SPRoleDefinition roleSupervisorDefintion = null;
                              SPRoleDefinition roleOpManagerDefintion = null;
  
                              if (strStatus == "Active")
                              {
                                  roleUserDefintion = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
                                  roleSupervisorDefintion = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                                  roleOpManagerDefintion = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                              }
                              else if (strStatus == "Submitted")
                              {
                                  roleUserDefintion = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                                  roleSupervisorDefintion = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
                                  roleOpManagerDefintion = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
                              }
                              else if (strStatus == "Completed")
                              {
                                  roleUserDefintion = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                                  roleSupervisorDefintion = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                                  roleOpManagerDefintion = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                              }
  
                              SPUser uParticipant = Utility.convertStringToSPUser(Utility.getSPStringLookupFromListGLT(objEntity.Participant));
                              SPRoleAssignment roleUserAssignment = new SPRoleAssignment(uParticipant);
  
                              SPUser uSupervisor = Utility.convertStringToSPUser(Utility.getSPStringLookupFromListGLT(objEntity.Supervisor));
                              SPRoleAssignment roleSupervisorAssignment = new SPRoleAssignment(uSupervisor);
  
                              SPUser uOpManager = Utility.convertStringToSPUser(Utility.getSPStringLookupFromListGLT(objEntity.OpsManager));
                              SPRoleAssignment roleuOpManagerAssignment = new SPRoleAssignment(uOpManager);
  
                              roleUserAssignment.RoleDefinitionBindings.Add(roleUserDefintion);
                              lstItem.RoleAssignments.Add(roleUserAssignment);
  
                              roleSupervisorAssignment.RoleDefinitionBindings.Add(roleSupervisorDefintion);
                              lstItem.RoleAssignments.Add(roleSupervisorAssignment);
  
                              roleuOpManagerAssignment.RoleDefinitionBindings.Add(roleOpManagerDefintion);
                              lstItem.RoleAssignments.Add(roleuOpManagerAssignment);
  
              }
              catch (SPException spex)
              {
  
              }
          }
  </code></pre>