Next, let's continue. Here we describe the operations on user and group as follows:
6. Add a specified user to a specified group
7. Obtain the owner of a specified group
8. Add the current logon user to the specified group.
9. Determine whether the current logon user has the editpermission permission
10. Determine whether the current logon user is in a specific group
The descriptions are as follows:
6. Add a specified user to a specified group
VaR siteurl = '/';
Function addusertosharepointgroup (groupid ){
// Var clientcontext = new sp. clientcontext (siteurl );
VaR clientcontext = new sp. clientcontext. get_current ();
VaR collgroup = clientcontext. get_web (). get_sitegroups ();
VaR ogroup = collgroup. getbyid (groupid );
VaR usercreationinfo = new sp. usercreationinformation ();
Usercreationinfo. set_email ('helpdesk @ glstar. com. au );
Usercreationinfo. set_loginname ('glstar \ helpdesk ');
Usercreationinfo. set_title ('helpdesk ');
This. ouser = ogroup. get_users (). Add (usercreationinfo); // Add User into group
// Var userinfo = '\ nuser:' + ouser. get_title () +
// '\ Nemail:' + ouser. get_email () +
// '\ Nlogin name:' + ouser. get_loginname ();
// Alert (userinfo );
Clientcontext. Load (ouser );
Clientcontext.exe cutequeryasync (function. createdelegate (this, this. onquerysucceededaddusertosharepointgroup ),
Function. createdelegate (this, this. onqueryfailedaddusertow.pointgroup ));
}
Function onquerysucceededaddusertow.pointgroup (){
Alert (this. ouser. get_title () + "added .");
}
Function onqueryfailedaddusertow.pointgroup (sender, argS ){
Alert ('request failed. '+ args. get_message () +' \ n' + args. get_stacktrace ());
}
7. Obtain the owner of a specified group
// Get the group owner name in SharePoint 2010 using ecmascript
VaR group;
Function getgroupownername (groupid ){
VaR clientcontext = new sp. clientcontext ();
VaR groupcollection = clientcontext. get_web (). get_sitegroups ();
Group = groupcollection. getbyid (groupid );
Clientcontext. Load (group );
Clientcontext.exe cutequeryasync (function. createdelegate (this, this. onquerysucceededgetgroupownername ),
Function. createdelegate (this, this. onqueryfailedgetgroupownername ));
}
Function onquerysucceededgetgroupownername (){
Alert ("grouptitle:" + group. get_title () + "\ ngroupownertitle:" + group. get_ownertitle ());
}
Function onqueryfailedgetgroupownername (sender, argS ){
Alert ('request failed. '+ args. get_message () +' \ n' + args. get_stacktrace ());
}
8. Add the current logon user to the specified group.
// Adds the current user to the specific group on the current website
VaR currentuser;
VaR visitorsgroup;
Function addusertospecificgroupincurrweb (groupid ){
VaR clientcontext = new sp. clientcontext ();
VaR groupcollection = clientcontext. get_web (). get_sitegroups ();
Visitorsgroup = groupcollection. getbyid (groupid );
Currentuser = clientcontext. get_web (). get_currentuser ();
VaR usercollection = visitorsgroup. get_users ();
Usercollection. adduser (currentuser );
Clientcontext. Load (currentuser );
Clientcontext. Load (visitorsgroup );
Clientcontext.exe cutequeryasync (function. createdelegate (this, this. onquerysucceededaddusertospecificgroup ),
Function. createdelegate (this, this. onqueryfailedaddusertospecificgroup ));
}
Function onquerysucceededaddusertospecificgroup (){
Alert (currentuser. get_title () + "added to group" + visitorsgroup. get_title ());
}
Function onqueryfailedaddusertospecificgroup (sender, argS ){
Alert ('request failed. '+ args. get_message () +' \ n' + args. get_stacktrace ());
}
9. Determine whether the current logon user has the editpermission permission
// Check current user has edit permission
VaR thecurrentuser;
VaR theweb;
Function checkifuserhaseditpermissions (){
// Debugger;
VaR context = new sp. clientcontext. get_current ();
Theweb = context. get_web ();
Thecurrentuser = theweb. get_currentuser ();
Context. Load (thecurrentuser );
Context. Load (theweb, 'inclutivebasepermission ');
Context.exe cutequeryasync (function. createdelegate (this, this. onsuccessmethodcheckeditpermissions ),
Function. createdelegate (this, this. onfailuremethodcheckeditpermissions ));
}
Function onsuccessmethodcheckeditpermissions (){
// Debugger;
If (theweb. get_inclutivebasepermissions (). Has (sp. permissionkind. editlistitems )){
// User has edit Permissions
Alert (thecurrentuser. get_loginname () + "has edit permission on current website ");
}
Else {
Alert ("No edit permission ");
}
}
Function onfailuremethodcheckeditpermissions (){
Alert ("failed to check permission ");
}
10. Determine whether the current logon user is in a specific group
VaR isinthisgroupflag = false;
// The below checks if the user exists in the group
Function checkifcurrentuserisingroup (groupid ){
VaR context = sp. clientcontext. get_current ();
// I go to parent site if I'm in a subsite!
VaR sitecoll = context. get_site ();
VaR web = sitecoll. get_rootweb ();
VaR groupcollection = web. get_sitegroups ();
// Get the our group's ID
VaR _ Group = groupcollection. getbyid (groupid); // ID of the group that we are checking
VaR users = _ group. get_users (); // get all users of the Group
Context. Load (_ group );
Context. Load (users );
This. _ users = users;
This. _ currentuser = web. get_currentuser (); // get current user
Context. Load (this. _ currentuser );
Context.exe cutequeryasync (function. createdelegate (this, this. checkusersucceededuserisingroup ),
Function. createdelegate (this, this. checkuserfaileduserisingroup ));
}
// The below checks if user is the member of the specified group
Function checkusersucceededuserisingroup (){
// Debugger;
Isinthisgroupflag = false;
If (this. _ users. get_count ()> 0 ){
VaR _ usersenum = This. _ users. getenumerator ();
While (_ usersenum. movenext ()){
VaR user = _ usersenum. get_current ();
If (user. get_loginname () = This. _ currentuser. get_loginname ()){
// Debugger;
Isinthisgroupflag = true;
}
If (isinthisgroupflag ){
Alert (user. get_loginname () + "exist in the checked group" + isinthisgroupflag. tostring ());
}
Else {
Alert (user. get_loginname () + "not exist in the checked group ");
}
}
}
}
Function checkuserfaileduserisingroup (){
Alert ('failed ');
}
Reprinted: http://www.cnblogs.com/wsdj-ITtech/archive/2012/06/09/2426504.html