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 have this code below and I am trying to add a sharepoint group to an item in a list in itemUpdated event handler. the problem is that the update never happens. The problem is with this line of code properties.ListItem.RoleAssignments.Add(rAssignment);

When I debug my code that's where the debugger stops and finishes. So it does not reach the update method. So no group has been added to the item's permission list.

When I remove it, (properties.ListItem.RoleAssignments.Add(rAssignment)), the update happens but I have to refresh the page twice to see the update in the title, for example.

Any idea how can I sovle this?

Here is the code that I am using, together with a class that handles the event firing. I removed the class too but nothing happend.

public override void ItemUpdated(SPItemEventProperties properties)
        {


  base.ItemUpdated(properties);
            using (DisableEventFiring scope = new DisableEventFiring())
            {


               SPWeb web = properties.ListItem.Web;

               SPGroup group = web.SiteGroups["HRTR"];
                SPPrincipal principal = (SPPrincipal)group;
                SPRoleAssignment rAssignment = new SPRoleAssignment(principal);

               properties.ListItem.BreakRoleInheritance(true);
                properties.ListItem.Web.AllowUnsafeUpdates = true;
                properties.ListItem["Title"] = "A cool new title!" + DateTime.Now;
                properties.ListItem.RoleAssignments.Add(rAssignment);

               properties.ListItem.Update();
                properties.ListItem.Web.AllowUnsafeUpdates = false;

           }

}

-----------

    class DisableEventFiring : SPItemEventReceiver, IDisposable 
    {
         // Boolean to hold the original value of the EventFiringEnabled property          
         bool  _originalValue;
         public DisableEventFiring()          
        {              
            // Save off the original value of EventFiringEnabled               
            _originalValue = base.EventFiringEnabled;               
            // Set EventFiringEnabled to false to disable it              
            base .EventFiringEnabled = false ;          
        }           
        public  void  Dispose()          
        {              
            // Set EventFiringEnabled back to its original value               
            base.EventFiringEnabled = _originalValue;          
        }
     }
share|improve this question

3 Answers 3

You have not added any role definition for the role assignment. Below is a sample code

    base.EventFiringEnabled = false;

SPSecurity.RunWithElevatedPrivilages(() =>
{
using(SPSite site = new SPSite(properties.Web.Url))
{
   using(SPWeb web = site.OpenWeb())
   {
      SPList list = web.Lists[properties.List.Title]
      SPListItem item = list.GetItemById(properties.ListItemId);

      if(!item.HasUniqueRoleAssignments)
      {
          item.BreakRoleInheritance(true);
      }

      SPGroup group = web.SiteGroups["TestGroup"];
      SPRoleAssignment role = new SPRoleAssignment(group);
      SPRoleDefinition definition = item.ParentWeb.RoleDefinitions["Contribute"];
      role.RoleDefinitionBindings.Add(definition);

      item.RoleAssignments.Add(role);
      item.Update()
   }
 }
});

base.EventFiringEnabled = true;
share|improve this answer
    
Good catch on not actually assigning any role. Your code sample won't quite work though, as you need to re-load the item from a context created inside the RunWithElevatedPrivileges block for the elevated privileges to take effect. –  lgaud Apr 1 '13 at 19:53
    
You are right. Fixed the example above. –  sssreddy Apr 1 '13 at 20:27

Could give you some suggestion.

1) Try out if you get some exception using try catch in the line

properties.ListItem.RoleAssignments.Add(rAssignment);

2) Instead of using ListItem.Update(); try to use ListItem.SystemUpdate(false);

3) In the EventReceiver use the following method to update

Comment these lines available in the EventReceiver

       base.ItemUpdated(properties);
       base.ItemUpdating(properties);
       base.EventFiringEnabled = false;

for updating use the following method

        base.EventFiringEnabled = false;
        try
        {
            ListItem.SystemUpdate(false);
        }
        finally
        {
            base.EventFiringEnabled = true;
        }
share|improve this answer
    
Hi Prasath, I used the try/catch block and it gives me Object reference not set to an instance of an object. –  Helm Ifort Apr 1 '13 at 11:30
    
it is giving me an error with httpContext.Current.Session and it is the same: Object not set to an instance on an object. –  Helm Ifort Apr 1 '13 at 11:32

Unless all of the users of the list have the ability to modify permissions, you will need to use SPSecurity.RunWithElevatedPrivileges or some other method to get higher permissions. Inside that method, you need to re-load the site/web/ListItem for the higher permissions to take effect.

Also - I doubt it's causing a problem, but making a DisableEventFiring class is more something you do to get that functionality somewhere other than an event receiver. Inside an event receiver you can just call the properties directly.

share|improve this answer

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.