SharePoint learning notes-ecmascript object model series-8. Group and user operations (1)

Source: Internet
Author: User

Here, we will summarize the common cases of using the ecmascript object model to operate on goup and user. Because there are many contents, we split them into two parts. The main content of this part is as follows:
1. Obtain all groups of the current SharePoint website
2. Obtain the title and Group of the current logon user.
3. Obtain all users in a specified group.
4. Obtain the specific information of all users in a specified group.
5. Obtain the specific information of all users of all groups.

The descriptions are as follows:

1. Obtain all groups of the current SharePoint website 

// Get all the site groups in SharePoint using ecmascript
VaR groupcollection;
Function getallsitegroups (){
VaR clientcontext = new sp. clientcontext ();
This. groupcollection = clientcontext. get_web (). get_sitegroups ();
Clientcontext. Load (groupcollection );
Clientcontext.exe cutequeryasync (function. createdelegate (this, this. onquerysucceededgetallsitegroups ),
Function. createdelegate (this, this. onqueryfailedgetallsitegroups ));

}

Function onquerysucceededgetallsitegroups (){
VaR groupname = 'site groups: \ n ';
VaR groupsenumerator = This. groupcollection. getenumerator ();
While (groupsenumerator. movenext ()){
VaR group = groupsenumerator. get_current ();
Groupname + = 'title: '+ group. get_title () + 'id:' + group. get_id () + '\ n ';
}
Alert (groupname );
}

Function onqueryfailedgetallsitegroups (sender, argS ){
Alert ('request failed. '+ args. get_message () +' \ n' + args. get_stacktrace ());
}

2. Obtain the title and Group of the current logon user.

// Retrieve curent loggedin user and the usres Group
VaR user;
VaR visitorsgroup;

Function retrievecurrlguserandgrp (){
VaR clientcontext = new sp. clientcontext ();
VaR groupcollection = clientcontext. get_web (). get_sitegroups ();
// Get the visitors group, assuming its ID is 4.
Visitorsgroup = groupcollection. getbyid (4 );
User = clientcontext. get_web (). get_currentuser ();
VaR usercollection = visitorsgroup. get_users ();
Usercollection. adduser (User );

Clientcontext. Load (User );
Clientcontext. Load (visitorsgroup );
Clientcontext.exe cutequeryasync (function. createdelegate (this, this. onquerysucceededretrievecurrlguserandgrp ),
Function. createdelegate (this, this. onqueryfailedretrievecurrlguserandgrp ));

}

Function onquerysucceededretrievecurrlguserandgrp (){
Alert (user. get_title () + "added to group" + visitorsgroup. get_title ());
}

Function onqueryfailedretrievecurrlguserandgrp (sender, argS ){
Alert ('request failed. '+ args. get_message () +' \ n' + args. get_stacktrace ());
}

3. Obtain all users in a specified group.

VaR siteurlretrieveallusersingroup = '/';
Function retrieveallusersingroup (groupid ){
// Debugger;
VaR clientcontext = new sp. clientcontext. get_current ();
// Var clientcontext = new sp. clientcontext (siteurlretrieveallusersingroup );
VaR collgroup = clientcontext. get_web (). get_sitegroups ();
VaR ogroup = collgroup. getbyid (groupid); // specify which group you want to retrieve
// Var ogroup = collgroup. getbytitle ('devptest visitor ');
This. colluser = ogroup. get_users ();
Clientcontext. Load (colluser );
Clientcontext.exe cutequeryasync (function. createdelegate (this, this. onquerysucceededretrieveallusersingroup ),
Function. createdelegate (this, this. onqueryfailedretrieveallusersingroup ));
}

Function onquerysucceededretrieveallusersingroup (){
// Debugger;
VaR userinfo = '';

VaR userenumerator = colluser. getenumerator ();
While (userenumerator. movenext ()){
VaR ouser = userenumerator. get_current ();
Userinfo = userinfo + '\ nuser title:' + ouser. get_title () +
'\ NID:' + ouser. get_id () +
'\ Nemail:' + ouser. get_email () +
'\ Nlogin name:' + ouser. get_loginname ();
}

Alert (userinfo );
}

Function onqueryfailedretrieveallusersingroup (sender, argS ){

Alert ('request failed. '+ args. get_message () +' \ n' + args. get_stacktrace ());
}

4. Obtain the specific information of all users in a specified group.

// Get user specific Ions
VaR siteurlspecificuserproperties = '/';

Function retrievespecificuserproperties (groupid ){
// Var clientcontext = new sp. clientcontext (siteurlspecificuserproperties );
VaR clientcontext = new sp. clientcontext. get_current ();
VaR collgroup = clientcontext. get_web (). get_sitegroups ();
VaR ogroup = collgroup. getbyid (groupid );
This. colluser = ogroup. get_users ();
Clientcontext. Load (colluser, 'include (title, loginname, email )');

Clientcontext.exe cutequeryasync (function. createdelegate (this, this. onquerysucceededretrievespecificuserproperties ),
Function. createdelegate (this, this. onqueryfailedretrievespecificuserproperties ));
}

Function onquerysucceededretrievespecificuserproperties (){

VaR userinfo = '';

VaR userenumerator = colluser. getenumerator ();
While (userenumerator. movenext ()){
VaR ouser = userenumerator. get_current ();
Userinfo + = '\ nuser:' + ouser. get_title () +
'\ Nemail:' + ouser. get_email () +
'\ Nlogin name:' + ouser. get_loginname ();
}

Alert (userinfo );
}

Function onqueryfailedretrievespecificuserproperties (sender, argS ){

Alert ('request failed. '+ args. get_message () +' \ n' + args. get_stacktrace ());
}

5. Obtain the specific information of all users of all groups.

// Retrieve all groups all users specific Ions
VaR siteurlallusersallgroupsspecificproperties = '/';
Function retrieveallusersallgroupsspecificproperties (){
// Var clientcontext = new sp. clientcontext (siteurlallusersallgroupsspecificproperties );
VaR clientcontext = new sp. clientcontext. get_current ();
This. collgroup = clientcontext. get_web (). get_sitegroups ();
Clientcontext. Load (collgroup, 'include (title, ID, users. Include (title, loginname ))');

Clientcontext.exe cutequeryasync (function. createdelegate (this, this. onquerysucceededretrieveallusersallgroupsspecificproperties ),
Function. createdelegate (this, this. onqueryfailedretrieveallusersallgroupsspecificproperties ));

}

Function onquerysucceededretrieveallusersallgroupsspecificproperties (){

VaR userinfo = '';

VaR groupenumerator = collgroup. getenumerator ();
While (groupenumerator. movenext ()){
VaR ogroup = groupenumerator. get_current ();
VaR colluser = ogroup. get_users ();
VaR userenumerator = colluser. getenumerator ();
While (userenumerator. movenext ()){
VaR ouser = userenumerator. get_current ();
Userinfo + = '\ ngroup ID:' + ogroup. get_id () +
'\ Ngroup title:' + ogroup. get_title () +
'\ Nuser:' + ouser. get_title () +
'\ Nlogin name:' + ouser. get_loginname ();
}
}

Alert (userinfo );
}

Function onqueryfailedretrieveallusersallgroupsspecificproperties (sender, argS ){

Alert ('request failed. '+ args. get_message () +' \ n' + args. get_stacktrace ());
}

 

 

 

 

 

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.