2

I'm retrieving a list of users using the following:

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.SiteUsers
                        select LoadItems(   user.Name.ToString(),
                                            user.LoginName.ToString()
                                            )).ToList();
            }
        }
    }
    catch (Exception ex)
    {
        PostEvent("Error fetching list of SharePoint Users", BaseExceptionEventArgs.ExceptionLevel.Error, ex);

        throw;
    }
}

For some reason this information is being populated with users that are no longer in the organization. I've checked the user profile service and they don't appear to be present there, nor do they have any direct access to the site that could cause them to show-up here. What am I missing?

2

It may happen as they are configured within an Active Directory group, instead of web.SiteUsers try using web.Users , to understand the difference you can check this link

You can also get Users from User Profile Service as you said you get right result from the service

var serviceContext = SPServiceContext.GetContext(site);
var userProfileManager = new UserProfileManager(serviceContext);
foreach (UserProfile userProfile in userProfileManager)
{
  // ...your code
}
3
  • I had looked at this method as well but unfortunately this will only output users with direct access to the site and not anybody in the "Everyone" category (which is how most users access our launch page) – Michael A May 11 '15 at 4:15
  • 1
    You can filter the results, if you have some user property telling which which users are currently active in organization OR create new property to store if user is currently working in organization or not – Akshay Randive May 11 '15 at 4:21
  • Appreciated. I'll dig deeper! – Michael A May 11 '15 at 4:44

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.