5

SharePoint 2013 On-Premises.

I have a need to display Site Collection Administrators somewhere on the main page. To test this out, I have written a JavaScript for fetching user data of a website, doing isSiteAdmin checks and displays if the information is correct.

The code is given below:

window.onload = function getUsers() { //script displays all Site Collection Administrators 

var clientContext = new SP.ClientContext.get_current();
var web = clientContext.get_web();


var users = web.get_siteUsers(); //for adminUsers

var currentUser = web.get_currentUser(); // for currentUser

var allGroups = currentUser.get_groups(); // for currentUserGroup

//first query
clientContext.load(users);
clientContext.executeQueryAsync(adminUsers, executionFail);

var usList = []; // array for displaying users, since there can be more than one

function adminUsers(sender, args) { 
    for(var i = 0; i < users.get_count();i++){ // loop to gather all users

        var user = users.getItemAtIndex(i);
        var urlUser = user.get_loginName();
        var urlUserSub = urlUser.substr(7);
        var url = "test";
        var name = user.get_title();
        var check = user.get_isSiteAdmin();

        if (check === true) // if user is SCA then add to the array
        {
            usList.push("<p style=\"margin-bottom:0px;\"><a href="+url+urlUserSub+">" + name +"</a></p>"); // some formatting for better visual display
        }
    }
    document.getElementById('titleId').innerHTML = "Site Collection Administrators";
    document.getElementById('addition').innerHTML = usList.join("");
} // end of adminUsers

Now the problem is that this code only works if you are Site Collection Administrator yourself.

How can I make this to work for other users? The idea is for all users to go on the main page of a given website and see who the Site Collection Administrator is, therefore everyone must be able to see it, not just me or any other site collection admin.

It appears to be a permission problem, but searching the web or checking any documentation gave me nothing. I concede the point that I have only been using SharePoint, or any SharePoint, for that matter, for only 3 weeks, so any help is appreciated.

1
  • You can get administrators from UserInformationList. Check my answer. Commented Nov 25, 2016 at 7:43

3 Answers 3

5

It is possible to list all administrators of a site collection using JavaScript(REST API).

UserInformationList has the all users information of your site. You can get administrators using $filter.

REST API

Make a GET request to following URL

/_vti_bin/ListData.svc/UserInformationList?$filter=IsSiteAdmin eq true

UserInformationList is a public list. So any using with Read/View permission will be able to access your scrip. Test/Explore your REST query using my REST Client.

PS: UserInformationList is a hidden list that you can not view it from browser.

Example using jQuery

function adminUsers() {
    var usList = [];
    jQuery.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_vti_bin/ListData.svc/UserInformationList?$filter=IsSiteAdmin eq true",
        method: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
        },
        success: function(data) {
            data.d.results.forEach(function(admin) {
                // your code goes here
            });
        },
        error: function(error) {
            alert(JSON.stringify(error));
        }
    });
}
1
  • Same thing is also available from the Soap UserGroup.asmx web services. Each user or group element will have an IsSiteAdmin attribute Commented Nov 1, 2017 at 5:01
6

Users are not allowed to enumerate 'higher' permissions.

So have your script (which only has to run when administrators havechanged) dump the info into a public List (or formatted HTML file)

4
  • What about if I want to have this script be executed every time you visit the website? How would I go about creating a public list?
    – Russel B
    Commented Nov 22, 2016 at 10:30
  • @RusselB, You can have the script execute each time, but it'll only work if the current user is an admin. A public list is simply a list, just make sure that everyone can view items in it.
    – wjervis
    Commented Nov 22, 2016 at 12:09
  • As wjervis said... A List takes some extra administration, you have to clear existing Items, make your script (over)write a .txt file with the HTML you already have to a Library. Then use a Content Editor WebPart linked to that .txt file to display the HTML Commented Nov 22, 2016 at 12:45
  • 2
    Users are not allowed to enumerate 'higher' permissions. This point is valid in general but OP's problem can solved using UserInformationList. Added an answer. Commented Nov 25, 2016 at 7:35
3

As site collection administrator options can be accessed only by site collection administrator, normal user cannot get this permissions.

One options i can think of is writing a site workflow and store this information in separate custom list, give read permissions to normal users on this list. Then you can use this list to query and display your data.

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.