SPGroup 和SPUser的常用操作
操作 常用
2023-09-27 14:28:05 时间
http://www.cnblogs.com/gzh4455/archive/2012/03/26/2417854.html
private bool RemoveUserFromGroup(string sGoupName, string sUserLoginName) { bool res = false; try { SPWeb web = SPContext.Current.Web; SPGroup oGroup = web.SiteGroups[sGoupName]; SPUser oUser = GetSPUser(sUserLoginName); if (oUser != null) { web.AllowUnsafeUpdates = true; oGroup.RemoveUser(oUser); oGroup.Update(); res = true; web.AllowUnsafeUpdates = false; } } catch (Exception ex) { string sMessage = ex.Message; //throw; } return res; } private bool RemoveUserFromGroup(SPGroup oGroup, SPUser oUser) { bool res = false; try { SPWeb web = SPContext.Current.Web; if (oUser != null&&oGroup!=null) { web.AllowUnsafeUpdates = true; oGroup.RemoveUser(oUser); oGroup.Update(); res = true; web.AllowUnsafeUpdates = false; } } catch (Exception ex) { string sMessage = ex.Message; //throw; } return res; } private SPUser GetSPUser(string sLoginName) { SPUser oUser = null; try { if (!string.IsNullOrEmpty(sLoginName)) { oUser = SPContext.Current.Web.EnsureUser(sLoginName); } } catch (Exception ex) { string sMessage = ex.Message; } return oUser; } private void RemoveUser(string sLoginName) { SPUser oUser = GetSPUser(sLoginName); if (oUser!=null) { SPGroupCollection groups = oUser.Groups; if (groups!=null&&groups.Count>0) { foreach (SPGroup g in groups) { RemoveUserFromGroup(g, oUser); } } } } private bool AddUserIntoGroup(string sGroupName, string sUserLoginName) { bool res = false; try { SPWeb web = SPContext.Current.Web; web.AllowUnsafeUpdates = true; SPGroup oGroup = web.SiteGroups[sGroupName]; SPUser oUser = GetSPUser(sUserLoginName); if (oUser != null) { oGroup.AddUser(oUser); oGroup.Update(); res = true; } web.AllowUnsafeUpdates = false; } catch (Exception ex) { string sMessage = ex.Message; //throw; } return res; } private string FilterSPUserString(string str) { if (string.IsNullOrEmpty(str)) { return str; } if (str.IndexOf(";#") > 0) { str = str.Substring(str.LastIndexOf(";#") + 2); } return str; } private bool CreateSiteGroup(string sGroupName, string sGroupDescription) { bool res = false; using (SPWeb oWebsiteRoot = SPContext.Current.Site.RootWeb) { oWebsiteRoot.AllowUnsafeUpdates = true; SPGroupCollection collGroups = oWebsiteRoot.SiteGroups; string sLoginName = SPContext.Current.Web.CurrentUser.LoginName; SPUser oUser = oWebsiteRoot.Users[sLoginName]; SPMember oMember = oWebsiteRoot.Users[sLoginName]; collGroups.Add(sGroupName, oMember, oUser, "Description"); oWebsiteRoot.AllowUnsafeUpdates = false; res = true; } return res; } /// <summary> /// 给组赋权限 /// </summary> /// <param name="sGroupName"></param> /// <param name="sPermissionLever"></param> /// <returns></returns> private bool SetGroupPermission(string sGroupName, string sPermissionLever) { bool res = false; using (SPWeb oWebsiteRoot = SPContext.Current.Site.RootWeb) { oWebsiteRoot.AllowUnsafeUpdates = true; SPRoleAssignment roleAssignment = new SPRoleAssignment(oWebsiteRoot.SiteGroups[sGroupName]); roleAssignment.RoleDefinitionBindings.Add(oWebsiteRoot.RoleDefinitions[sPermissionLever]); oWebsiteRoot.Update(); oWebsiteRoot.AllowUnsafeUpdates = false; res = true; } return res; } private void DeleteSiteGroup(SPWeb web, string groupName) { web.AllowUnsafeUpdates = true; SPGroupCollection groups = web.SiteGroups; groups.Remove(groupName); web.Update(); web.AllowUnsafeUpdates = false; }
相关文章
- C++ 操作控制台(打开进程、关闭进程、执行命令)
- 指针的操作 p*++
- "多步操作产生错误,请检查每一步的状态值" 导致原因和排查过程
- es6常用数组操作及技巧汇总
- 数据库的创建和操作
- Allegro设置测试点离器件间距规则操作指导
- mysql查询当前时间某进程对某表的操作
- 机器学习中的线性代数:关于常用操作的新手指南
- UE4蓝图中的常用操作
- git checkout/git reset/git revert/git restore常用回退操作
- git常用操作
- Python办公自动化之 openpyxl 操作 Excel
- 14个Xcode中常用的快捷键操作
- git 常用操作
- Docker常用操作(CentOS 7)
- MATLAB——操作矩阵的常用函数
- Mysql数据库常用表结构操作语句
- 【AGC】云存储无法操作文件夹问题
- 【两周快速入门pr】熟稔常用操作,提高剪辑效率
- 狂神说笔记——SpringBoot操作数据库22-2
- Linux常用操作(超全)
- SQL Server常用操作(待续)
- Tkinter常用简单操作
- 操作JSON数据
- mongoose 常用数据库操作 查询
- layer.confirm确认取消的事件处理以及常用操作