0

Is it possible to get the 'Primary Site Collection Administrator' using Microsoft.SharePoint.Client csom?

I can get the site collection administrators by checking the site users and the isadmin flag but there could be multiple users with this set.

Below is a screenshot of what I'm trying to retrieve, this is what I have so far:

      Web oWebsite = clientContext.Web;
                clientContext.Load(oWebsite, website => website.Webs, website => website.Title, website => website.SiteUsers.Include(_=>_.IsSiteAdmin, _=>_.LoginName));
                clientContext.ExecuteQuery();

                foreach (var user in oWebsite.SiteUsers) {
                    if (user.IsSiteAdmin) {
                        Console.WriteLine(user.LoginName);
                    }
                }

enter image description here

1

Yes it is possible to retrieve this information via CSOM. You're looking for the Owner property on the Site object, but accessing the Web object.

This is an example of retrieving the Owner Login Name in JavaScript.

clientContext = new SP.ClientContext.get_current();
site = clientContext.get_site();
clientContext.load(site, "Owner");
clientContext.executeQueryAsync(success, failure)
function success() {
    var owner = site.get_owner().get_loginName();
    console.log(owner);
}
function failure(sender, args) {
    console.log('Error: ' + args.get_message())
}

So retrieving in CSOM should look something like this.

Site oSite = clientContext.Site;
clientContext.Load(oSite, site => site.Owner);
clientContext.ExecuteQuery();
Console.WriteLine(oSite.Owner.LoginName);

Reference: https://msdn.microsoft.com/en-us/library/office/microsoft.sharepoint.client.site.owner.aspx

  • I'm trying the following with SharePoint 2010 and it says property does not exist. Do you know if it's possible to get this information? – Webfort Jan 30 '17 at 9:46
0

You can also use the PnP GetAdministrators extension method in CSOM.

List<UserEntity> admins = web.GetAdministrators();
foreach (UserEntity admin in admins)
{
    Console.WriteLine("Admin Login Name : " + admin.LoginName);
    Console.WriteLine("Admin Title : " + admin.Title);
}

To use PnP extension method, just install the NuGet package from package manager console.

The command for that would be :

Install-Package SharePointPnPCoreOnline 

Or if you want to add it via GUI, just click on Manage nuget packages and search for PnP and then click on Install. After that, you will be able to use the above code.

enter image description here

  • thanks but looking at that method it looks at the .IsSiteAdmin flag and does not give me the Primary owner. – Webfort Jan 30 '17 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.