.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Retrieving all users from all SharePoint groups of a site collection using ECMA script

Posted By:Makarand Kulkarni       Posted Date: May 12, 2014    Points: 200    Category: SharePoint    URL: http://www.dotnetspark.com  

This article explains how to retrieve all users from all SharePoint groups using ECMA script.
 

With the use of ECMA Script we can add/edit/delete or manipulate SharePoint data. We are going to see how to get all users from SharePoint groups in following article:

//declare site url first,
var siteUrl = '/sites/MySiteCollection';

then write the following lines of code,

ExecuteOrDelayUntilScriptLoaded(getUserInfo, 'SP.js');

function getUserInfo()
{
  var clientContext = new SP.clientContext(siteUrl);
  this.collGroup = clientContext.get_web().get_siteGroups();
  client.Context.load(collGroup);
  client.load(collGroup, 'Include(Users)');
  clientContext.executeQueryAsync(Function.createDelegate(this, this.OnSucceeded), Function.createDelegate(this, this.OnFailed));
}

on sucess of the above code 'OnSucceeded' function will get executed. Otherwise 'OnFailed' function will get executed.

function OnSucceeded()
{
 var userInfo = "";
 var groupEnum = collGroup.getEnumerator();
 while(groupEnum.moveNext())
 {
   var groupObj = groupEnum.getCurrent();
   var userCollection = groupObj.get_users();
   var userEnum = userCollection.getEnumerator();
   while(userEnum.moveNext())
   {
      var userObj = userEnum.get_current();
      this.userInfo += '\n GroupID: '+ groupObj.get_id()
                       + '\n Group Title: ' + groupObj.get_title()
                       + '\n User: ' + userObj.get_loginName();
   }
 }
 alert(userInfo);
}

function OnFailed(sender, args);
{
  alert('Request failed. ' + args.get_message() + '\n args.get_stackTrace()');
}
  

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend