Get All Users From Site Group in SharePoint using JSOM

The example in this topic show how to use JSOM to Get All Users From Site Group in SharePoint

  • var oUsers, clientContext;
  • function GetAllUsersFromGroup() {
  •     // You can optionally specify the Site URL here to get the context
  •     // If you don't specify the URL, the method will get the context of the current site
  •     // var clientContext = new SP.ClientContext("http://MyServer/sites/SiteCollection");
  •     clientContext = new SP.ClientContext();
  •  
  •     // Get the Site Group
  •     var oGroup = clientContext.get_web().get_siteGroups().getByName("New Group");
  •  
  •     // Get all the users from the group
  •     oUsers = oGroup.get_users();
  •  
  •     // Load group users
  •     clientContext.load(oUsers);
  •  
  •     // Execute the query to the server.
  •     clientContext.executeQueryAsync(onsuccess, onfailed);
  • }
  •  
  • function onsuccess() {
  •  
  •     // Iterate through each item
  •     var oEnumerator = oUsers.getEnumerator();
  •  
  •     while (oEnumerator.moveNext()) {
  •         var oUser = oEnumerator.get_current();
  •         console.log("User: " + oUser.get_title() + "\n");
  •     }
  • }
  •  
  • function onfailed(sender, args) {
  •     console.log('Failed' + args.get_message() + '\n' + args.get_stackTrace());
  • }

Thank you for reading this article. This code was tested in SharePoint 2013


© 2019 Code SharePoint