views:

26

answers:

3
  1. I have a List to which users have contribute rights
  2. I have a Event Handler which changes the Item Level Permissions on adding or Updating events on list by 2.a CurrentlistItem.BreakRoleInheritance(true); 2.b and by adding users to that list

Now above does not work if user who is adding has rights of contribute but works if users has full control.

I think a solution could be to run these privileged calls by system admin user.

How can i solve this problem , Please guide, Thanks.

//Amit

+2  A: 

Have you tried RunWithElevatedPrivileges? Just remember to run as little code as possible within it.

knight0323
+1  A: 

Just running your code using RunWithElevatedPrivileges is not enough. Credential info is kept inside the SPSite object that is used directly or indirectly by most SPxxxx objects, including SPWeb, SPListItem etc. In order to modify item permissions you will have to recreate any SPSite, SPListItem, SPWeb you use inside RunWithElevatedPrivileges, using only the IDs provided by the event handler.

Panagiotis Kanavos
A: 

I tried RunWithElevatedPrivileges as suggested and did recreated all the SPxxxx items basically I did

public override void ItemAdded(SPItemEventProperties properties)
{
   ...
   ...
   ...
   ...
   //run with Elevated Privileges Now for Change in Permissions
   SPSecurity.RunWithElevatedPrivileges(delegate()
   {
     string url = web.Url;
     SPSite site = new SPSite(url);
     //create a new web object inside of the "elevated" block
     SPWeb web1 = site.OpenWeb();

     //only needed if not using the application master page 
     // or not usinga FormDigest control
     web.AllowUnsafeUpdates = true;


     SPList list = null;
     list = web1.Lists[properties.ListId];
     SPListItem currentlistitem = list.Items.GetItemById(properties.ListItemId);

            //currentlistitem.DoesUserHavePermissions
            ModifyPermissionOnListItem(listtitle, currentlistitem, web1, sUser);

        });
  }

And did Exactly What I was doing earlier but in Elevated Privileges section. Thanks knight0323,Panagiotis It did solved the my case.

Amit