Skip to main content
edited tags
Link
Michael A
  • 1.4k
  • 6
  • 37
  • 60
Source Link
Michael A
  • 1.4k
  • 6
  • 37
  • 60

A list of all users in a SharePoint site, including in a group?

I'm trying to get a list of all users that have access to a SharePoint site. This is working, except it's only showing people who have been given direct access and then listing out the "everyone" group - instead of group members:

public List<SPUsersEntity> FetchItems(string siteName)
{
    try
    {
        PostEvent("Attempting to load users from: " + siteName, BaseExceptionEventArgs.ExceptionLevel.Debug);

        using (var site = new SPSite(siteName))
        {
            using (var web = site.OpenWeb())
            {
                PostEvent("Successfully opened: " + web.Url, BaseExceptionEventArgs.ExceptionLevel.Debug);

                return (from SPUser user in web.Users
                        select LoadItems(   user.Name.ToString(),
                                            user.LoginName.ToString()
                                            )).ToList();
            }
        }
    }
    catch (Exception ex)
    {
        PostEvent("Error fetching list of SharePoint Users", BaseExceptionEventArgs.ExceptionLevel.Error, ex);

        throw;
    }
}

How can I just get a list of the users that have access to a site - ignoring groups.