0

We have to fetch users who are site collection administrators of all the sites. For some site collections the users are displayed as o365 groups itself. How do we get users within the o365 group using CSOM?

1

This was accomplished by creating an Azure AD app using Microsoft Graph API. Relevant article for those interested is given below:-

Fetch o365 group owners

0

Use below code to get List of users in specific SharePoint group:

ClientContext clientContext = new ClientContext("https://yoursite/");  
GroupCollection collGroup = clientContext.Web.SiteGroups;  
Group getGroup = collGroup.GetById(5);  ///Here you should enter your group ID
UserCollection collUser = getGroup.Users;  
clientContext.Load(collUser);  
clientContext.ExecuteQuery();  
foreach (User getUSer in collUser)  
{  
      Console.WriteLine("User: {0} Email: {2} Login Name: {3}",   
      getGroup.Title, getGroup.Email, getGroup.LoginName);  
}  
1
  • SharePoint group is not the same as o365-group in this context. The O365-group is an Azure AD group – Robert Lindgren Mar 12 '19 at 7:17
0

First you get the groupd id from the propertybag:

KKSiteProperties GetProperties()
{
    var propertyBag = SPOContext.Web.AllProperties;
    SPOContext.Web.Context.Load(propertyBag);
    SPOContext.Web.Context.ExecuteQueryWithIncrementalRetry(3, _logger);

    return new KKSiteProperties()
    {
        DefaultLibraryId = propertyBag.FieldValues.TryGetValue<string>("GroupDocumentsListId"),
        GroupDocumentsUrl = propertyBag.FieldValues.TryGetValue<string>("GroupDocumentsUrl"),
        GroupType = propertyBag.FieldValues.TryGetValue<string>("GroupType"),
        Language = propertyBag.FieldValues.TryGetValue<string>("vti_defaultlanguage"),
        UnifiedGroupId = propertyBag.FieldValues.TryGetValue<string>("GroupId"),
        UnifiedGroupName = propertyBag.FieldValues.TryGetValue<string>("GroupAlias")
    };
}

Then you get the user from the group throug Microsoft Graph:

public async Task<List<SiteUser>> GetOwners()
{
    var ownersResult = new List<SiteUser>();

    if (string.IsNullOrEmpty(PropertyBag.UnifiedGroupId))
    {
        return ownersResult;
    }

    var client = await GetGraphClient();
    var owners = await client.GetGroupOwners(PropertyBag.UnifiedGroupId);

    return owners.Select(p => new SiteUser {DisplayName=p.DisplayName,Email=p.Mail}).ToList();
}

Notice I only take owners here, could be members too.

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.