3

I am trying to get all the Users from a SharePoint Owner's Group for a subsite. Below Script is Working fine . But it displays both Site and Subsite Users of Owner's Group.

For example If SITE A has SITEOWNERS GROUP and it has SUBSITE A1 . Now when i use this Script in sub site, It should Display the users from SUBSITEOWNERS GROUP. Howeever it Displays users from Both SITEOWNERS GROUP & SUBSITEOWNERS GROUP. Can any one Help me on this to correct my Code.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js "></script>
<script type="text/javascript" src="http://test.com/Style%20Library/JAVASCRIPT/jquery.SPServices-2014.02.min.js"></script>
<div id='tblMain' style="float:left">
 <table id="myUsersTable"  border="1" cellpadding="2" width="400px">
    <tr>
    <th>OwnerName</th>
    <th>OwnerEmail</th>
    </tr>
</table>

</div>
<script type="text/javascript">
$(document).ready(function() { 
var currentcontext = null; 
var currentweb = null; 
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', GetGroups);
});

function GetGroups() 
{ 
currentcontext = new SP.ClientContext.get_current(); 

currentweb = currentcontext.get_web(); 

this.groupCollection = currentweb.get_siteGroups(); 

currentcontext.load(this.groupCollection); 

currentcontext.executeQueryAsync(Function.createDelegate(this, this.ExecuteOnSuccess), 
Function.createDelegate(this, this.ExecuteOnFailure)); 
} 
function ExecuteOnSuccess(sender, args) 
{ 
var listEnumerator = this.groupCollection.getEnumerator(); 
while (listEnumerator.moveNext()) 
{ 
var item = listEnumerator.get_current(); 
groupName = item.get_title(); 
//alert(groupName); 
    if (groupName.toLowerCase().indexOf("owners") >= 0)
    {
        alert(groupName); 
    GetData(groupName);
    }
} 
} 

function ExecuteOnFailure(sender, args) 
{ 
alert("error"); 
} 

function GetData(groupName)
{
    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups/getbyname('" + groupName +"')/users",
        type: "GET",
        dataType: "json",
        headers: {
        Accept: "application/json;odata=verbose", 
        },
          success: getUsersFromGroupSuccessHandler,
          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);

               });
            //console.log(Owners);


        }


}

function AddRowToTable(OwnerName,OwnerEmail)
{   
$("#myUsersTable").append( 

                           "<tr>"+
                            "<td >" + OwnerName + "</td>" +
                            "<td>" + OwnerEmail + "</td>"+
                            "</tr>"); 


}
function getUsersFromGroupErrorHandler(data, errorCode, errorMessage)
{
alert("Could not get users from group: " + errorMessage);
}

</script> 
1
  • You are doing a lazy match on the owner group, if (groupName.toLowerCase().indexOf("owners") >= 0), the more subsite you have, the more will appear. You need to be more explicit in the group you are looking for. Commented Apr 16, 2015 at 16:50

1 Answer 1

0

You are getting site collection groups earlier you are retrieving groups from the site collection, but when you execute your GetData() function, you are passing in a group name, but executing it at the root level.

http://site/_api/web/sitegroups/getbyname('Owners')/users

instead of

http://site/subsite/_api/web/sitegroups/getbyname('Owners')/users

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.