Common spgroup and spuser operations

Source: Internet
Author: User
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;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.