Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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)
        {

        }
    }
share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

Replace the segment of code with below code:

using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
{
    web.AllowUnsafeUpdates = true;

    SPList lstProject = web.Lists["SharedHeader"];
    //lstProject.BreakRoleInheritance(true);
    //lstProject.Update();

    lstProject = web.Lists["SharedHeader"];
    SPListItem lstItem = lstProject.Items.Add();
    loadToListItem(sharedHeader, ref lstItem, true);
    lstItem.Update();

    lstItem = lstProject.GetItemById(lstItem.ID);
    lstItem.BreakRoleInheritance(false);
    lstItem.Update();

    lstItem = lstProject.GetItemById(lstItem.ID);
    updatelistpermission(web, sharedHeader, lstItem, strStatus);
    lstItem.Update();

    web.AllowUnsafeUpdates = false;
    intResult = lstItem.ID;
    lstItem = null;
    lstProject = null;
}

When you are adding the Item until lstItem.Update() is not called, the Item is not made in the list... and thus you cannot set permissions on the Item which doesn't exist!

So first you need to call lstItem.Update to create the Item in the list and then get the new Item again by list.GetItemById() and then set the permissions on the Item!

UPDATE:
I have updated the code above!
Please follow this article

I hope this helps!

share|improve this answer
    
Thanks @Arsalan Adam Khatri. Now I'm getting following error "This operation is not allowed on an object that inherits permissions." Even I have added web.BreakRoleInheritance(true); –  Rishi Oct 31 '12 at 15:41
    
Rishi, I have updated the code above! –  Arsalan Adam Khatri Oct 31 '12 at 15:48
    
I've updated the code as you mentioned but still the same error Exception Details: System.InvalidOperationException: This operation is not allowed on an object that inherits permissions. –  Rishi Oct 31 '12 at 15:55
    
Can you check stop inheriting permissions on List and Web manually - and try running the code? –  Arsalan Adam Khatri Oct 31 '12 at 16:03
    
Permission of the list is already broken , It seems first part of the code where we are breaking the permission of the list and saving works. –  Rishi Oct 31 '12 at 16:07
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.