SharePoint 服务器端对象模型操作用户组(创建/添加/删除)
2013-09-11 14:18
387 查看
摘要:几个操作SharePoint用户组的方法,已经测试通过,但是没有提升权限,如果没有权限的人操作,需要提升权限(提权代码附后)。大家需要的话,可以参考下,写在这里也给自己留个备份~~
//创建用户组
public static bool CreatSPGroup(string strGroupName, string strGroupDescription)
{
try
{
using (SPSite site = new SPSite(SiteUrl))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPUser defaultUser = web.SiteUsers.GetByID(Convert.ToInt32(defaultUserID));
web.SiteGroups.Add(strGroupName, defaultUser, null, strGroupDescription);
web.AllowUnsafeUpdates = false;
return true;
}
}
}
catch
{
return false;
}
}
//添加用户到用户组
public static bool AddUserToSPGroup(string strGroupName, string strLoginName, string strUserName, string strEmail, string notes)
{
try
{
using (SPSite site = new SPSite(SiteUrl))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPGroup cGroup = web.SiteGroups.GetByName(strGroupName);
cGroup.AddUser(strLoginName, strEmail, strUserName, notes);
web.AllowUnsafeUpdates = false;
return true;
}
}
}
catch
{
return false;
}
}
//从用户组删除用户
public static bool DelUserFromSPGroup(string strLoginName, string strGroupName)
{
try
{
using (SPSite site = new SPSite(SiteUrl))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPGroup cGroup = web.SiteGroups.GetByName(strGroupName);
cGroup.Users.Remove(strLoginName);
web.AllowUnsafeUpdates = false;
return true;
}
}
}
catch
{
return false;
}
}
//提升权限
SPSecurity.RunWithElevatedPrivileges (delegate()
{
//此处放置需要以系统账号身份运行的代码
});
特别注意:
1 如果代码要操作WSS的内容,必须创建新的SPSite和SPWeb实例,利用RunWithElevatedPrivilege
2 不能直接调用上下文对象(SPContext),上下文对象始终以当前用户身份运行
//创建用户组
public static bool CreatSPGroup(string strGroupName, string strGroupDescription)
{
try
{
using (SPSite site = new SPSite(SiteUrl))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPUser defaultUser = web.SiteUsers.GetByID(Convert.ToInt32(defaultUserID));
web.SiteGroups.Add(strGroupName, defaultUser, null, strGroupDescription);
web.AllowUnsafeUpdates = false;
return true;
}
}
}
catch
{
return false;
}
}
//添加用户到用户组
public static bool AddUserToSPGroup(string strGroupName, string strLoginName, string strUserName, string strEmail, string notes)
{
try
{
using (SPSite site = new SPSite(SiteUrl))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPGroup cGroup = web.SiteGroups.GetByName(strGroupName);
cGroup.AddUser(strLoginName, strEmail, strUserName, notes);
web.AllowUnsafeUpdates = false;
return true;
}
}
}
catch
{
return false;
}
}
//从用户组删除用户
public static bool DelUserFromSPGroup(string strLoginName, string strGroupName)
{
try
{
using (SPSite site = new SPSite(SiteUrl))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPGroup cGroup = web.SiteGroups.GetByName(strGroupName);
cGroup.Users.Remove(strLoginName);
web.AllowUnsafeUpdates = false;
return true;
}
}
}
catch
{
return false;
}
}
//提升权限
SPSecurity.RunWithElevatedPrivileges (delegate()
{
//此处放置需要以系统账号身份运行的代码
});
特别注意:
1 如果代码要操作WSS的内容,必须创建新的SPSite和SPWeb实例,利用RunWithElevatedPrivilege
2 不能直接调用上下文对象(SPContext),上下文对象始终以当前用户身份运行
相关文章推荐
- SharePoint 服务器端对象模型操作用户组(创建/添加/删除)
- SharePoint 服务器端对象模型操作用户组(创建/添加/删除)
- SharePoint 服务器端对象模型操作用户组(创建/添加/删除
- SharePoint服务器端对象模型 之 访问用户、用户组和权限(Part 1)
- SharePoint服务器端对象模型 之 使用LINQ进行数据访问操作(Part 1)
- SharePoint服务器端对象模型 之 使用LINQ进行数据访问操作(Part 4)
- SharePoint服务器端对象模型 之 访问用户、用户组和权限(Part 2)
- SharePoint服务器端对象模型 之 访问用户、用户组和权限(Part 3)
- SharePoint 服务器端对象模型操作文档库(上传/授权/查看权限)
- SharePoint 服务器端对象模型操作文档库(上传/授权/查看权限)
- SharePoint 服务器端对象模型 之 使用LINQ进行数据访问操作(Part 2)
- SharePoint 服务器端对象模型操作文档库(上传/授权/查看权限)
- SharePoint服务器端对象模型 之 使用LINQ进行数据访问操作(Part 3)
- linux 创建新用户添加到sudo用户组及基本安装操作等
- SharePoint【ECMAScript对象模型系列】-- 06. 使用ECMA操作列表项对象(list item)
- Sharepoint学习笔记—ECMAScript对象模型系列-- 6、使用ECMA操作列表项对象(list item)
- Unity 3D 动态创建对象,并渲染贴图,动态添加删除脚本
- SharePoint服务器端对象模型 之 访问网站和列表数据(Part 1)
- C# 对sharepoint 列表的一些基本操作,包括添加/删除/查询/上传文件给sharepoint list添加数据
- 每日学习心得:SharePoint 2013 自定义列表项添加Callout菜单项、文档关注、SharePoint服务端对象模型查询