Several ways to manipulate a SharePoint user group have been tested through, but no elevated permissions, if the person does not have permission to operate, you need to elevate the rights (the right to the code appended). We need to, you can refer to, write here also give yourself a backup ~ ~
Create user group 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; }//Add user to user group public static bool Addusertospgroup (string strgroupname, Strin G 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; Remove user public static bool Deluserfromspgroup from user group (string strloginname, str ing Strgroupname) {try {using (SPSite site = new SPSite (site
URL)) { 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;
}//elevated permission Spsecurity.runwithelevatedprivileges (delegate () {//Here to place code that needs to be run as a system account });
Special attention:
1 If your code is to manipulate the content of WSS, you must create a new SPSite and SPWeb instance, using the Runwithelevatedprivilege
2 cannot call the context object directly (SPContext), the context object always runs as the current user
Author: cnblogs Lin Yu
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/web/sharepoint/