0

I'm trying to return a list of all users in the user profile service using linq. Unfortunately I can't see a way to do this, and have become stumped at 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);

                var serviceContext = SPServiceContext.GetContext(site);
                var userProfileManager = new UserProfileManager(serviceContext);

                //return (from UserProfile userProfile in userProfileManager
                //        select LoadItems(userProfile.AccountName.ToString, userProfile.DisplayName.ToString)).ToList();

                var userList = new List<SPUsersEntity>;

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

        throw;
    }
}

This is throwing an error because UserProfileManager isn't IQueryable. Is there another class or approach I can use to accomplish this?

1

The following class could be introduced for that purpose that implements Enumerable Interface:

class UserProfileTypedManager : UserProfileManager, IEnumerable<ProfileBase>
{
    public UserProfileTypedManager(SPServiceContext serviceContext) : base(serviceContext)
    {
    }

    public new IEnumerator<ProfileBase> GetEnumerator()
    {
        var e = base.GetEnumerator();
        while (e.MoveNext())
        {
            yield return e.Current as ProfileBase;    
        }
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return base.GetEnumerator();
    }

}

Then you could apply LINQ Query Operations:

public static List<SPUsersEntity> FetchItems(SPSite site)
{
    var serviceContext = SPServiceContext.GetContext(site);
    var userProfileManager = new UserProfileTypedManager(serviceContext);
    return (from UserProfile userProfile in userProfileManager select new SPUsersEntity() { LoginName = userProfile.DisplayName }).ToList();
}

Usage

using (var site = new SPSite(siteUrl))
{
    var result = FetchItems(site);
    foreach (var userEntry in result)
    {
        Console.WriteLine(userEntry.LoginName);
    }
}
  • Does this process requires less time as compare to- var srchResults = profileManager.GetEnumerator(); while (SrchResults.MoveNext()){//Code here} – Ram Nov 28 '16 at 9:50
  • What namespace I require for SPUsersEntity? – Hardik Mar 26 '18 at 11:20
0

You can try like this:

var serviceContext = SPServiceContext.GetContext(site);
var userProfileManager = new UserProfileManager(serviceContext);
foreach (UserProfile userProfile in userProfileManager)
{
    // ...
}

You will need elevated privileges to perform this operation

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.