1

I modified the code here:

How to get all users From Owners Group in SharePoint 2013 for a subsite

to search for groups named "xxx Members" and their members.

To the code, I added:

function AddGroupNameRowToTable(GroupName)
{   
     $("#myUsersTable").append("<tr>" +
                               "<td colspan=2>" + GroupName + "</td>" +
                               "</tr>"); 
}

and a call to it at the end of function ExecuteOnSuccess:

GroupName = item.get_title();
if (GroupName.indexOf(" Members") >= 0)
      {
          //alert(GroupName); 
          AddGroupNameRowToTable(GroupName);
          GetData(GroupName);

The problem is, I get:

Gardeners Members
Horticulturists Members
Fred     Fred@xxxcom
Jed      Jed@xxxcom
Lucy     Lucy@xxxcom
Ron      Ron@xxxcom
Tilly    Tilly@xxxcom

I want to see this (spacing and dash lines for clarity):

Gardeners Members
-----------------
Fred     Fred@xxxcom
Jed      Jed@xxxcom

Horticulturists Members
-----------------------
Lucy     Lucy@xxxcom
Ron      Ron@xxxcom
Tilly    Tilly@xxxcom

It looks like the functions are executing asynchronously, such that the groups get listed first.

I don't know how to get the results I want. The SP object model and coding style are new to me.

Thanks

2
  • what if you add some "width" attribute to your td tags on the AddGroupNameRowToTable function? And if you want the "-------" spacer add a second row, <tr><td> tags This is html format problem not sharepoint problem.
    – Marco
    Commented Dec 27, 2017 at 21:13
  • Marco, thanks, but this question is not about the formatting. What I want to solve is the ordering of the output, per groups and members. Commented Dec 27, 2017 at 21:21

2 Answers 2

0

The GetData function in your code is async so you need to add the function AddGroupNameRowToTable(GroupName) in its success handler. The sample code snippet is below for reference.

function GetData(groupName){
    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups/getbyname('" + groupName +"')/users",
        type: "GET",
        dataType: "json",
        headers: {
            Accept: "application/json;odata=verbose", 
        },
        success: function(data){ 
            AddGroupNameRowToTable(groupName); 
            getUsersFromGroupSuccessHandler(data) 
        },
        error: getUsersFromGroupErrorHandler
    });    
}
function getUsersFromGroupSuccessHandler(data) 
{
    checkUsers = data.d.results.length;
    var Owners = [];

    if (checkUsers == 0){
        alert ("There are no results for this");
    }
    else{
        $.each(data.d.results, function(index, item){
            var OwnerName = item.Title;
            var OwnerEmail= item.Email;

            AddRowToTable(OwnerName,OwnerEmail);

        });
    }
} 
1
  • Ah, I see! Thanks very much for showing the fix in the context of my code! Commented Dec 29, 2017 at 21:12
0

The following code for your reference:

<script src="//code.jquery.com/jquery-3.2.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {      
    getGroupsUsers();   
});
function getGroupsUsers(){
    var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/SiteGroups?$filter=substringof('Members',Title)&$expand=Users";

    $.ajax({
        url: requestUri,
        type: "GET",
        contentType: "application/json;odata=verbose",
         headers: { "Accept": "application/json;odata=verbose"},
        success: function (data) {
            var html="<table >";
            var groups=data.d.results;
            for(var i=0;i<groups.length;i++){
                if(i>0){
                    html+="<tr><td colspan=2 style='border-bottom: dotted 2px black;'></td></tr>";
                }
                html+="<tr><td colspan=2><b>"+groups[i].Title+ "</b></td></tr>";
                var users=groups[i].Users.results;
                for(var j=0;j<users.length;j++){
                    html+="<tr><td>"+users[j].Title+"</td><td>"+users[j].Email+"</td></tr>";
                }           
            }
            html+="</table>";
            $("#groupUsers").html(html);
        },
        error: function (e) {
            //alert("Failed to get details");                
        }
    });
}
</script>
<div id="groupUsers"></div>

enter image description here

5
  • This is excellent! Compact and understandable! Thank you so much for adding this. I learned a lot from it. Currently, all of my users are in SharePoint Groups, and the code enumerates those users perfectly. Once I am given permission by the higher-ups, I will keep the SharePoint Groups but remove the users and replace them with Active Directory (AD) Security Groups (SGs) containing those users. I imagine that this code will then show those SG names, but not the users defined in each SG. Is there a simple way to enhance this code, to show the users in those SGs? Commented Dec 29, 2017 at 21:24
  • Did you want to get users from Azure AD Security Groups? If so, check the Graph API. msdn.microsoft.com/en-us/library/azure/ad/graph/api/…
    – LZ_MSFT
    Commented Jan 1, 2018 at 1:57
  • No, just terrestrial AD SGs. Commented Jan 2, 2018 at 14:21
  • Hmm, I have been presuming that the AD SGs are terrestrial, but since our SharePoint is cloudy (O365 tenant), maybe those would have been forced to Azure. Is there a method that works for either platform? Commented Jan 2, 2018 at 14:29
  • If the AD SGs in On-Premise, as a workaround, we can get the ad groups using C# code and add the data into a custom list with permissions in SharePoint Online using CSOM C#, then get data from the custom list using REST API.
    – LZ_MSFT
    Commented Jan 3, 2018 at 3:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.