public void AddGroup(string groupName, string owner, string defaultUser, string description, UserObject.RoleDefinition role) { if (string.IsNullOrEmpty(groupName)) { return; } if (string.IsNullOrEmpty(owner)) { return; } if (string.IsNullOrEmpty(defaultUser)) { return; } try { using (SPWeb website = GetWebSite()) { SPUser sharepointOwner = GetUserObject(owner); if (sharepointOwner == null) { return; } SPUser sharepointDefaultUser = GetUserObject(defaultUser); if (sharepointDefaultUser == null) { return; } SPGroupCollection sharepointGroups = website.SiteGroups; sharepointGroups.Add(groupName, sharepointOwner, sharepointDefaultUser, description); AssignPermission(role, groupName, string.Empty); website.Update(); } } catch (Exception ex) { var log = new AppEventLog(AppException.ExceptionMessage(ex, "AddGroup", "ClsHelper")); log.WriteToLog(); } }
private void AssignPermission(UserObject.RoleDefinition role, string groupName, string userName) { try { using (SPWeb website = GetWebSite()) { SPRoleAssignment roleAssignment = null; if (string.IsNullOrEmpty(groupName)) { roleAssignment = new SPRoleAssignment(website.SiteUsers[userName]); } if (string.IsNullOrEmpty(userName)) { roleAssignment = new SPRoleAssignment(website.SiteGroups[groupName]); } SPRoleDefinition roleDefinition = null; switch (role) { case UserObject.RoleDefinition.FullControl: roleDefinition = website.RoleDefinitions["Full Control"]; break; case UserObject.RoleDefinition.Design: roleDefinition = website.RoleDefinitions["Design"]; break; case UserObject.RoleDefinition.ManageHierarchy: roleDefinition = website.RoleDefinitions["Manage Hierarchy"]; break; case UserObject.RoleDefinition.Approve: roleDefinition = website.RoleDefinitions["Approve"]; break; case UserObject.RoleDefinition.Contribute: roleDefinition = website.RoleDefinitions["Contribute"]; break; case UserObject.RoleDefinition.Read: roleDefinition = website.RoleDefinitions["Read"]; break; case UserObject.RoleDefinition.RestrictedRead: roleDefinition = website.RoleDefinitions["Restricted Read"]; break; case UserObject.RoleDefinition.LimitedAccess: roleDefinition = website.RoleDefinitions["Limited Access"]; break; case UserObject.RoleDefinition.ViewOnly: roleDefinition = website.RoleDefinitions["View Only"]; break; case UserObject.RoleDefinition.RecordsCenterSubmissionCompletion: roleDefinition = website.RoleDefinitions["Records Center Submission Completion"]; break; default: break; } if (roleAssignment != null) { roleAssignment.RoleDefinitionBindings.Add(roleDefinition); website.RoleAssignments.Add(roleAssignment); website.Update(); } } } catch (Exception ex) { var log = new AppEventLog(AppException.ExceptionMessage(ex, "AssignPermission", "ClsHelper")); log.WriteToLog(); } }