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>)
{
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(";#") > )
{
str = str.Substring(str.LastIndexOf(";#") + );
}
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;
}

SPGroup 和SPUser的常用操作的更多相关文章

  1. SharePoint 2013 新功能探索 之 SPGroup、SPUser 事件处理程序 还要继续改进

    曾几何时,想要获取SPGroup Add及SPUserAdd事件,在网上查找各种方法,都没有找到相对应的解决办法,如今在VS 2013 Preview版本 提供了 SPSecurityEventRec ...

  2. 【三】用Markdown写blog的常用操作

    本系列有五篇:分别是 [一]Ubuntu14.04+Jekyll+Github Pages搭建静态博客:主要是安装方面 [二]jekyll 的使用 :主要是jekyll的配置 [三]Markdown+ ...

  3. php模拟数据库常用操作效果

    test.php <?php header("Content-type:text/html;charset='utf8'"); error_reporting(E_ALL); ...

  4. Mac OS X常用操作入门指南

    前两天入手一个Macbook air,在装软件过程中摸索了一些基本操作,现就常用操作进行总结, 1关于触控板: 按下(不区分左右)            =鼠标左键 control+按下        ...

  5. mysql常用操作语句

    mysql常用操作语句 1.mysql -u root -p   2.mysql -h localhost -u root -p database_name 2.列出数据库: 1.show datab ...

  6. nodejs配置及cmd常用操作

    一.cmd常用操作 1.返回根目录cd\ 2.返回上层目录cd .. 3.查找当前目录下的所有文件dir 4.查找下层目录cd window 二.nodejs配置 Node.js安装包及源码下载地址为 ...

  7. Oracle常用操作——创建表空间、临时表空间、创建表分区、创建索引、锁表处理

    摘要:Oracle数据库的库表常用操作:创建与添加表空间.临时表空间.创建表分区.创建索引.锁表处理 1.表空间 ■  详细查看表空间使用状况,包括总大小,使用空间,使用率,剩余空间 --详细查看表空 ...

  8. python 异常处理、文件常用操作

    异常处理 http://www.jb51.net/article/95033.htm 文件常用操作 http://www.jb51.net/article/92946.htm

  9. byte数据的常用操作函数[转发]

    /// <summary> /// 本类提供了对byte数据的常用操作函数 /// </summary> public class ByteUtil { ','A','B',' ...

随机推荐

  1. LuoguP4383 [八省联考2018]林克卡特树lct

    LuoguP4383 [八省联考2018]林克卡特树lct https://www.luogu.org/problemnew/show/P4383 分析: 题意等价于选择\(K\)条点不相交的链,使得 ...

  2. ACM学习历程——ZOJ 3822 Domination (2014牡丹江区域赛 D题)(概率,数学递推)

    Description Edward is the headmaster of Marjar University. He is enthusiastic about chess and often ...

  3. [POI 2018] Plan Metra

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5100 [算法] 首先分两类考虑 : 1. 1 -> N的路径不经过其它节点 , ...

  4. 【Facebook】等差子序列个数

    题目: 给定一整数数列,问数列有多少个子序列是等差数列. 即对于包含N个数的数列A,A(0),A(1),……,A(N-1),有多少组(P(0),P(1),……,P(k))满足0<=P(0)< ...

  5. 洛谷【P1009】阶乘之和

    题目传送门:https://www.luogu.org/problemnew/show/P1009 高精度加法:https://www.cnblogs.com/AKMer/p/9722610.html ...

  6. 上海-北京间通过Azure Storage的RA-GRS类型的存储账户进行快速复制

    Azure的Blob存储分成多种类型,目前主要有: 其中RA-GRS可以在上海-北京两个数据中心间同步数据.并且,在第二个数据中心可以只读的方式读取这个存储账户中的Blob内容. 虽然GRS采用的是准 ...

  7. 2008上交:Day of Week

    题目描述: We now use the Gregorian style of dating in Russia. The leap years are years with number divis ...

  8. (cdh)hive 基础知识 名词详解及架构

    过程 启动 hive 之后出现的 CLI 是查询任务的入口,CLI 提交任务给 Driver Driver 接收到任务后调用 Compiler,Executor,Optimizer 将 SQL 语句转 ...

  9. CSS之边距合并

    外边距合并指的是,当两个垂直外边距相遇时,它们将形成一个外边距.合并后的外边距的高度等于两个发生合并的外边距的高度中的较大者. 情况1:当一个元素出现在另一个元素上面时,第一个元素的下外边距与第二个元 ...

  10. mysql数据库int(5)以及varchar(20)长度表示的是什么?

    在mysql5.x版本的数据库中: int类型数据的字节大小是固定的4个字节: 但是int(5)和int(11)区别在于,显示的数据位数一个是5位一个是11位,在开启zerofill(填充零)情况下, ...