How to retrieve current SharePoint Site Collection Administrators with the Client Object Model?

Hello,

In this post, I will show you how retrieve site collection administrators of the site with SharePoint Client Object Model.

For the moment there are no methods or attributes that can help you to retrieve site collection admins directly.

This is why, the idea of this script is to retrieve all users from the list SiteUserInfoList and apply a filter on that, based on the user property: “IsSiteAdmin”

With the methods below I assume that you have initiated your clientContext and a Web object correctly.

For example:

var ctx = new SP.ClientContext.get_current();
var site = ctx.get_web();
ctx.load(site);
...

So now you have initiated your ClientContext (var ctx) and your Web (var site).

You will find the code that will display the site administrators:

function GetAllUsers() {
   //Retrieve the SiteUserInfoList
   var userInfoList = site.get_siteUserInfoList();

   //Create the query that will retrieve the first name & last name of Site Administrators
   var camlQuery = new SP.CamlQuery();
   camlQuery.set_viewXml("<View><ViewFields><FieldRef Name='FirstName' /><FieldRef Name='LastName' /><FieldRef Name='IsSiteAdmin' /></ViewFields><Query><Where><Eq><FieldRef Name='IsSiteAdmin' /><Value Type='Boolean'>1</Value></Eq></Where></Query></View>");
   this.collListItem = userInfoList.getItems(camlQuery); 

   //load and execute
   ctx.load(collListItem);
   ctx.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceededAllUser), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceededAllUsers() {
   //Retrieve the enumerator
   var listItemEnumerator = collListItem.getEnumerator();
   //loop on all site admins retrieved
   while (listItemEnumerator.moveNext()) {
     var oListItem = listItemEnumerator.get_current();
     //display the information of the user
     alert( oListItem.get_item('FirstName') + '\n: ' + oListItem.get_item('LastName'));
  }
}

function onQueryFailed(sender, args) {
  alert('request failed ' + args.get_message() +'\n' + args.get_stackTrace());
}

Advertisements
This entry was posted in SharePoint and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s