I'm trying to update item security while adding new item but getting following error. How to find the problem from this trace ?
Stack Trace: [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
updatelistpermission(SPWeb web, SharedSurveyHeader objEntity, SPListItem lstItem, String strStatus) +816
routine.<>c_DisplayClass3.b_0() +688
Microsoft.SharePoint.<>c_DisplayClass4.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
Here is my 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["SharedHeader"];
SPListItem lstItem = lstProject.Items.Add();
loadToListItem(sharedHeader, ref lstItem, true);
updatelistpermission(web, sharedHeader, lstItem, strStatus);
lstItem.Update();
web.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)
{
}
}