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

how to set security for listitem programatically? My Requirements as follows: In list , while adding item to it.

I have two radio buttons.(i)view by every one (ii) view only by me...if i select 'view by every one' radiobutton, that items in the list should be able to view by everyone.if i select 'me only'.then that item ll viewed only by me .plz help me...is there any property to do this..thanx in advance

share|improve this question
add comment

3 Answers

Thre is no property to do this OOTB from simply adding an item into a SharePoint List. If you are comfortable in creating / writing code then an event receiver for the ItemAdded event which takes the value of the metadata selected (View By Everyone / Only Me) then breaks the inheritance of the SPList (Parent List) and sets the permissions explicitly. Should the "View by everyone" be selected then the handler will just ignore the permissions. If you have not done code before let me know and i could try to give you a simple code example.

share|improve this answer
    
Ya sure i wann it:) –  varna Oct 25 '12 at 8:50
    
Nadeem Below had beaten me to this :) thanks Nadeem :) Some additions below: –  Chris Pettigrew Oct 25 '12 at 8:56
add comment

You can use following code as a starting point:

        item.BreakRoleInheritance(false);
        SPUser user = web.CurrentUser;
        SPRoleAssignment roleAssignment = new SPRoleAssignment(user);
        roleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions["Full Control"]);
        item.RoleAssignments.Add(roleAssignment);

This will give the current user full control on the item.

share|improve this answer
    
how to set roledefinition for ly view..can u plz.. –  varna Oct 25 '12 at 9:07
add comment

Nadeem Below had beaten me to this :) thanks Nadeem :) Some additions below:

    public class YourClass: SPItemEventReceiver
     {
       public override void ItemAdded(SPItemEventProperties properties)
       {
          SPListItem item=properties.ListItem;
          item.BreakRoleInheritance(false);
          SPUser user = web.CurrentUser;
          SPRoleAssignment roleAssignment = new SPRoleAssignment(user);
          roleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions["Full Control"]);
          item.RoleAssignments.Add(roleAssignment);      
          item.Update(); 
         //must!!! or you `can use item.SystemUpdate() also based on your equirement
       }
     }
share|improve this answer
    
hi Chris Pettigrew...thanx for ur answer..i am newbie ...where should i have to add this code...In feature1.eventreceiver.cs???? i want more precisely....plz –  varna Oct 25 '12 at 9:25
    
Can you let me know if you are using 2010 or 2007 and what version of visual studio you are using so i can give you relevant instructions –  Chris Pettigrew Oct 25 '12 at 10:31
    
SharePoint 2010 ,visual studio 2010 –  varna Oct 25 '12 at 10:40
    
hi..i am using ItemAdding event receiver...i'm not able to get listitem by this code. SPListItem item=properties.ListItem; plz help me... –  varna Oct 26 '12 at 6:26
add comment

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.