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 addusergroupgroup (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>
/// Grant Group Permissions
/// </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, string groupname)
{
Web. allowunsafeupdates = true;
Spgroupcollection groups = web. sitegroups;
Groups. Remove (groupname );
Web. Update ();
Web. allowunsafeupdates = false;
}
Determine whether the current user exists in this group
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;
}