Wednesday, February 17, 2016

Programmatically assign specific permission in SharePoint

Programmatically assign specific permission in SharePoint



SharePoint site security helps manage permissions for different resources within a site by defining the levels of accessibility permissions for different peoples and groups. In SharePoint, always the top-level or the parent level permissions are inherited to it's child contents (e.g. a sub-site inheriting permissions from it's parent site collection).
In-order to create unique permission we need to break the inheriting parent permission and create new permission level for the SharePoint content. These permissions can be defined for specific users or groups.
Permissions to a specific user (list element) :
// assign a item to SPListItem object.SPListItem objLstitem = objLst.Items[0]; // objLst is the SPList object
// get the user by ID/Email
// objSpWeb is the SpWeb object

SPUser objUser = objSpWeb.SiteUsers.GetByEmail("
user@mydomain.com");

// Break inheriting parent permissions for this List Item.
objLstitem.BreakRoleInheritance(false);


SPRoleDefinitionCollection webroledefinitions = w.RoleDefinitions;
SPRoleAssignment roleassignment = new SPRoleAssignment(user);
roleassignment.RoleDefinitionBindings.Add(webroledefinitions["Read"]);
oListItem.RoleAssignments.Add(roleassignment);
oListItem.Update();
Permissions to a specific group (list):

list.BreakRoleInheritance(true); 
SPGroup groupAdmin = web.SiteGroups["IKM Manager"];  
SPRoleAssignment roleAssignmentAdmin = new SPRoleAssignment((SPPrincipal)groupAdmin);  
SPRoleDefinition roleAdmin = web.RoleDefinitions.GetByType(SPRoleType.Administrator);  
roleAssignmentAdmin.RoleDefinitionBindings.Add(roleAdmin);  
list.RoleAssignments.Add(roleAssignmentAdmin);  
list.Update();
Note that if you have to assigne pertmissions to multiple groups, you should update the element for each permission
 
//Définir les autorisations sur l'élement en cours
 item.BreakRoleInheritance(false);
 SPUser currentUser = web.CurrentUser;
 SPRoleAssignment RoleAssignementAdmin = new SPRoleAssignment(currentUser);
 SPRoleDefinition roleAdmin = web.RoleDefinitions.GetByType(SPRoleType.Editor);
                            RoleAssignementAdmin.RoleDefinitionBindings.Add(roleAdmin);
item.RoleAssignments.Add(RoleAssignementAdmin);
item.Update();

item = FichePersonnelle.GetItemById(item.ID);
//Attriburer le droit lecture seule pour le groupe Membre
SPGroup groupMember = web.SiteGroups["Plateforme collaborative RH - Membres"];
SPRoleAssignment roleAssignmentReader = new SPRoleAssignment((SPPrincipal)groupMember);
SPRoleDefinition roleReader = web.RoleDefinitions.GetByType(SPRoleType.Reader);
roleAssignmentReader.RoleDefinitionBindings.Add(roleReader);
item.RoleAssignments.Add(roleAssignmentReader);
item.Update();
item = FichePersonnelle.GetItemById(item.ID);
//Attriburer le droit lecture seule pour le groupe Visiteur
SPGroup groupvisitors = web.SiteGroups["Plateforme collaborative RH - Visiteurs"];
SPRoleAssignment roleAssignmentVisitors = new SPRoleAssignment((SPPrincipal)groupvisitors);
SPRoleDefinition roleVisitor = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                            roleAssignmentVisitors.RoleDefinitionBindings.Add(roleVisitor);
item.RoleAssignments.Add(roleAssignmentVisitors);
item.Update();
 
Note that you must run the code to do this with elevated Privileges to assign the role.
 Hope this help,
Oumaima

Sharepoint Designer 2013, XSLT List View Options ribbon option is not showing

Sharepoint Designer 2013, XSLT List View Options ribbon option is not showing I have an ordinary Wiki Page, also tried making an Article...