I just want to display all the Site owners in a table for each site. So i have developed the below code where i was able to fetch the users form the Owners Group. It is working only for the first time. Once i Refresh then i am unable to get the users. Can you please tell me what is wrong in 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;
ExecuteOrDelayUntilScriptLoaded(GetGroups, "sp.js");
});
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>