/// <summary> /// 在当前网站中创建用户组 /// </summary> /// <param name="strGroupName">用户组名称</param> /// <param name="strPermission">用户组权限</param> /// <param name="strDescription">用户组描述</param> private void CreateGroup(string strGroupName, string strPermission, string strDescription) { SPSecurity.RunWithElevatedPrivileges(delegate() { try { using (SPSite site = SPContext.Current.Site) { SPGroupCollection groups = site.RootWeb.SiteGroups; SPMember spMember = site.RootWeb.CurrentUser; SPUser spUser = site.RootWeb.CurrentUser; groups.Add(strGroupName,spMember,spUser, strPermission); AddPermission(site.RootWeb, strGroupName, strPermission); } } catch (Exception ex) { throw ex; } }); } /// <summary> /// 给指定用户组添加权限 /// </summary> /// <param name="spWeb">网站url</param> /// <param name="spGroupName">用户组名称</param> /// <param name="strPermission">权限名称</param> private void AddPermission(SPWeb spWeb,string spGroupName, string strPermission) { try { spWeb.AllowUnsafeUpdates = true; SPRoleAssignment roleAssignment = new SPRoleAssignment(spWeb.SiteGroups[spGroupName]); roleAssignment.RoleDefinitionBindings.Add(spWeb.RoleDefinitions[strPermission]); if (!spWeb.HasUniqueRoleAssignments) { spWeb.BreakRoleInheritance(false); } spWeb.RoleAssignments.Add(roleAssignment); spWeb.Update(); } catch (Exception ex) { throw ex; } finally { spWeb.AllowUnsafeUpdates = false; } }