I have zero experience coding in SharePoint and have been given the task of showing the current Site owners within an HTML control on EVERY page for our site.
I found this code at this link which looked like the same situation I have, running the code provided I got the same results, the table is populated with all of the sites and all of the Site Owners. So I made the change marked as the answer but it looks like the code is still iterating through all sites and popping an error message for each site. The Error message is below:
Could not get user from group: NOT FOUND
I have posted the code that is generating the error below. Can anyone help me out?
<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",
url: _spPageContextInfo.webAbsoluteUrl + "_api/web/sitegroups/getbyname('Owners')/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>