0

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.

1

Use SPWeb.SiteUsers instead of SPWeb.Users. SPWeb.Users gives only those users that have been explicitly granted permissions whereas SPWeb.SiteUsers gives all users of a site collection. See this for more information:http://snahta.blogspot.in/2008/12/users-vs-allusers-vs-siteusers.html

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.