UserProfileManager is actually enumerable

I was looking for a way to get a list of all user profiles within Sharepoint 2007. The UserProfileManager only has functions to retrieve a single user, but no function that retrieves all users.

The solution is simple, although a bit hard to see: UserProfileManager implements IEnumerable, and it already contains all user profiles!

So you can just do this here:

using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;

ServerContext ctx = ServerContext.GetContext(SPContext.Current.Site);
UserProfileManager upm = new UserProfileManager(ctx);
foreach (UserProfile u in upm)
{
DoStuffWith(u);
}

to iterate through all profiles.

Comments (1)

MikeOctober 13th, 2011 at 20:59

Thanks for this. What a horrible way to design a class. Not discoverable at all!

Leave a comment

Your comment