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 am using this code to break the permission.But i loss the listitem after refresh the page.Plz help me..

public override void ItemAdded(SPItemEventProperties properties)

        {

  SPSecurity.RunWithElevatedPrivileges(delegate()
            {

                using (SPSite site = new SPSite(properties.Web.Site.ID))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists["SampleEvent"];
                        SPUser user = web.CurrentUser;
                        SPListItem item = list.GetItemById(84);//i am getting the added item by hardcoding the id
                        if (!item.HasUniqueRoleAssignments)
                        {
                            item.BreakRoleInheritance(false);
                        }
                        item.Update();
                   }
                }
            });
         }
share|improve this question
    
What you lost... Item with id 84? –  Mihir Oct 30 '12 at 4:49
    
yes.i lost the list item 84 –  varna Oct 30 '12 at 5:13
    
plz help me...i want solution for this... –  varna Oct 30 '12 at 5:18
    
I tried with the same code what you mentioned. But I am able to see my item. My Item is not deleting –  Mihir Oct 30 '12 at 5:25
    
Change the item id to some other and try once and check for that also is it behaving same or not? –  Mihir Oct 30 '12 at 5:26
show 7 more comments

1 Answer

For a standard user your code will remove access item 84 (Hard coded????)

You're calling SPListItem.BreakRoleInheritance with the parameter false, which means break role inheritance without copying any permissions. The result of this will be that nobody has access to the item. Except for site collection admins (and persons granted access at web app level).

Usually you'll follow the call to BreakRoleInheritance with adding some permissions. See How to: Break Role Assignment Inheritance

Your code is similar to an priviledged user doing the following in the UI:

  1. Go to the list.
  2. Select Item 84.
  3. Click Item permissions.
  4. Click Stop Inheriting Permissions.
  5. Select all users/groups.
  6. Click Remove User Permissions

In the comment you state that users should only be allowed to see Items they've created themselves. If that's your requirement then you don't need an event receiver. Instead do the following:

  1. Go to the list
  2. In the ribbon select List Settings
  3. Click Advanced Settings
  4. Set Item-level permissions like this:
    enter image description here
  5. Click OK

The same can be achived programatically by setting SPList.ReadSecurity and SPList.WriteSecurity

If this isn't enough and you need specific rights to each item then read How to: Work with Roles and How to: Break Role Assignment Inheritance

share|improve this answer
    
Hi..can u plz give more explanation abt this. –  varna Oct 30 '12 at 9:40
    
I've adden a discription of what the similar actions in the UI would be. What are you trying to do? Which user(s) should have access to the item after you're done. And why item 84? –  Per Jakobsen Oct 30 '12 at 10:08
    
k...i understood what u trying to tel. –  varna Oct 30 '12 at 10:33
    
k...i understood what u trying to tel.My requirement is:I have one list.Consider 'A' and 'B' are the user.An listitem added by 'A' is not shown to 'B'.Likewise 'A' is not able to see the listitem which was added by 'B'. –  varna Oct 30 '12 at 10:38
    
Item 84 is..I am just taking one listitem and try to apply my requirement.Just for testing i took one listitem –  varna Oct 30 '12 at 10:43
show 14 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.