Using CSOM to Get List of Site Owners

You can use CSOM to get all of the “Owners” of a site. This is done by finding the owners’ group in a site. The operation is non-trivial since the owners’ group will be named something different in each site. But SharePoint Online has a way of intuiting the owners’ group, and in CSOM this group can be accessed by loading the web. AssociatedOwnerGroup object.

context.Load(web.AssociatedOwnerGroup);
context.ExecuteQuery();

Then you will load the members of the group, and iterate through them.

context.Load(web.AssociatedOwnerGroup.Users);
context.ExecuteQuery();

if (web.AssociatedOwnerGroup.Users != null)
{
    foreach (var user in web.AssociatedOwnerGroup.Users)
    {
        //do stuff
    }
 }

The following method returns a comma delimited list of the owners emails:

  private static string GetOwnersOfSite(SiteProperties site, ClientContext context, Web web)
        {
            string owners = "|";
            try
            {
                context.Load(web.AssociatedOwnerGroup);
                context.ExecuteQuery();
                if (web.AssociatedOwnerGroup != null)
                {
                    context.Load(web.AssociatedOwnerGroup.Users);
                    context.ExecuteQuery();
                    var count = web.AssociatedOwnerGroup.Users.Count;
                    var counter = 1;
                    if (web.AssociatedOwnerGroup.Users != null)
                    {
                        foreach (var user in web.AssociatedOwnerGroup.Users)
                        {
                            owners += user.Email + (counter != count ? "," : "");
                            counter += 1;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                owners = "unknown";
            }
           

            return owners;
        }

How Can We Help? Find out how Lone Peak Software can help you with SharePoint development and configuration.

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close