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"]);