Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11508

Setting User Permission in SharePoint 2010

$
0
0

Hi,

     I have a SharePoint List which is created programmatically and has columns like Employee Name, Emp ID, Check in and Check Out time and Total Duration Of hours Worked. 

I want to set user permission for this list, like, for Eg, when John logs in, he has to see only his records. I can do this even without programming by going to advanced setting in SharePoint. But the problem with that is: There's a user called Benny who has created the whole list. So if i manually do it going to advanced setting and when John Logs in he will end up having no records of his in that list.

I can also create a view and use filters, but then again i guess all the users can change view and can view others records too.

Will the below given code help my requirement Or if any changes , Please guide me with this. And can anyone also help me like how do i call the function for the same? 

Any HELP would be greatly appreciated.

Thanks.

 

public void reAssignItemLevelPermissions(SPWeb web, SPListItem item, string userName)
 {
 try
 {
 if
 (!item.HasUniqueRoleAssignments)
 {
 item.BreakRoleInheritance(false);// this will detach the permission from the parent
 SPPrincipalInfo user = SPUtility.ResolvePrincipal(web, userName, SPPrincipalType.All, 

SPPrincipalSource.All, null, false);//to get the user to assign permissions to the item
 SPRoleDefinition RoleDefinition = web.RoleDefinitions.GetByType(SPRoleType.Reader);//the type of 

role assignment, can also use Contribute, full
 SPPrincipal member = null;

 if ((item.GetUserEffectivePermissions(userName) & SPBasePermissions.ViewListItems) == 0)
 {
   SPRoleAssignment RoleAssignment = newSPRoleAssignment(member);
   RoleAssignment.RoleDefinitionBindings.Add(RoleDefinition);
   item.RoleAssignments.Add(RoleAssignment);
 }
 
item.SystemUpdate();
 }
}
 catch (Exception exception)
 {
 throw exception;
 }
 
}

Neo Alpha


Viewing all articles
Browse latest Browse all 11508

Trending Articles