SPGroup 和SPUser的常用操作

SPGroup 和SPUser的常用操作privateboolRemoveUserFromGroup(stringsGoupName,stringsUserLoginName){boolres=false;try{SPWebweb=SPContext.Current.Web;SPGroupoGroup

大家好,欢迎来到IT知识分享网。SPGroup

    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;
}

 判断当前用户是否存在该组

   public  bool IsUserInGroup(string groupName)
{
bool inGroup = false;
try
{
SPGroup group = SPContext.Current.Web.Groups[groupName];
inGroup = group.ContainsCurrentUser;
}
catch (Exception ex)
{
// SharePoint throws an exception if the group indexer
// is not a group the current user belongs to.
inGroup = false;
}
return inGroup;
}

 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/34051.html

(0)

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信