SharePoint server-side object model action user groups: Create, add, and delete operations

Source: Internet
Author: User
Tags bool object model

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/

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.