ECMAScript object Model group and user Action tutorial

Source: Internet
Author: User
Tags object model

Here is a summary of the use of the ECMAScript object model to manipulate goup and user's common situation, because more content, so split into two parts, this part of the main contents are as follows:
1, get the current SharePoint site All group
2. Get the title and group of the current logged-on user
3. Get all users under the specified group
4. Get specific information for all users under the specified group
5, get all the group's specific information for all users

Described separately are as follows:

1, get the current SharePoint site All group

The code is as follows Copy Code

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.executequeryasync (this, this.onquerysucceededgetallsitegroups), function.createdelegate
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. Get the title and group of the current logged-on user

The code is as follows Copy Code

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 it 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.executequeryasync (this, this.onquerysucceededretrievecurrlguserandgrp), function.createdelegate
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. Get all users under the specified group

The code is as follows Copy Code

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.executequeryasync (this, this.onquerysucceededretrieveallusersingroup), function.createdelegate
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. Get specific information for all users under the specified group

The code is as follows Copy Code

Get User Specific informations
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.executequeryasync (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, get all the group's specific information for all users

The code is as follows Copy Code

//retrieve All groups all users specific informations
    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.executequeryasync (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.