Group (SharePoint group, domain group) contain current user

Source: Internet
Author: User
    Private   Bool Addusergrouping group (spweb, String Sgroupname, Int  Iuserid ){  Bool Res = False  ;  Try  {Web. allowunsafeupdates = True  ; Spgroup ogroup = Web. sitegroups [sgroupname]; spuser ouser =Web. allusers. getbyid (iuserid );  If (Ouser! = Null  ) {Ogroup. adduser (ouser); ogroup. Update (); Res = True  ;} Web. Update ();}  Catch  {}  Return  Res ;}  Private   Bool Createsitegroup (spweb rootweb,String Sgroupname, String  Sgroupdescription ){  Bool Res = False  ;  Try  {Rootweb. allowunsafeupdates = True  ; Spgroupcollection collgroups = Rootweb. sitegroups;  String Sloginname =Rootweb. currentuser. loginname; spuser ouser = Rootweb. siteusers [sloginname]; spmember omember = Rootweb. siteusers [sloginname]; collgroups. Add (sgroupname, omember, ouser, sgroupdescription); Res = True  ; Rootweb. Update ();}  Catch  (Exception ex) {response. Write (  "  Error:  " + Ex. Message + " <Br/>  "  );  Return   False  ;}  Return  Res ;}  Private   Void Deletesitegroup (spweb web, String  Groupname ){  Try  {Web. allowunsafeupdates = True ; Spgroupcollection groups = Web. sitegroups; groups. Remove (groupname); web. Update ();}  Catch  {}}  Private   Bool Iscontaincurrentuser ( Object  OBJ ){  Bool Result = False  ;  If (OBJ = Null ){  Return   False  ;} Spsite securitysite = Null  ; Spweb securityweb = Null  ; Spweb tempweb = Null  ;  Try  {Spfielduservaluecollection approverscollection; spweb Web =Spcontext. Current. Web; web. allowunsafeupdates = True  ;  Bool Groupflag = False  ;  String Groupname = "  GRP  " + Guid. newguid (). tostring ();  //  Response. Write ("groupname:" + groupname + "<br/> "); Spsecurity. runwithelevatedprivileges ( Delegate  (){  Using (Securitysite = New  Spsite (spcontext. Current. Site. ID )){  Using (Securityweb = Securitysite. rootweb) {groupflag = Createsitegroup (securityweb, groupname, ""  );  // Response. Write ("groupflag:" + groupflag. tostring () + "<br/> ");  }}}); Approverscollection = (Spfielduservaluecollection) OBJ;  Int Id = Spcontext. Current. Web. currentuser. ID;  Foreach (Spfielduservalue In  Approverscollection ){  If (Spfielduservalue. User = Null ){  //  Response. Write ("group" + spfielduservalue. lookupvalue + "<br/> ");                      If  (Web. sitegroups [spfielduservalue. lookupvalue]. containscurrentuser) {result = True  ;  Break  ;}}  Else  {  If (Spfielduservalue. lookupid = ID) {result = True  ;  Break  ;}  Else  {Addusermetadata group (securityweb, groupname, spfielduservalue. lookupid );}}}  //  System. Threading. thread. Sleep (500 ); Tempweb = Spcontext. Current. Site. openweb (); If (! Result ){  //  Validate domain user has current user                  If  (Groupflag ){  //  Response. Write (spcontext. Current. Web. sitegroups [groupname]. Users. Count );  Spgroup Group = Tempweb. sitegroups [groupname];  If (Group. containscurrentuser) {result = True  ;}}}  //  Delete Group  Deletesitegroup (securityweb, groupname );}  Catch  (Exception ex ){  //  Return false; Response. Write ( "  Error: " + Ex. Message );}  Finally  {  If (Securityweb! = Null  ) {Securityweb. Dispose (); securityweb = Null  ;}  If (Securitysite! = Null  ) {Securitysite. Dispose (); securitysite =Null  ;}  If (Tempweb! = Null  ) {Tempweb. Dispose (); tempweb = Null  ;}}  Return  Result ;} 

Usage: bool flag =! Iscontaincurrentuser (sourceitem ["Reader"]);

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.