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)

to iterate through all profiles.

Comments (2)

MikeOctober 13th, 2011 at 20:59

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

T72September 14th, 2012 at 11:41

It does not implement IEnumerable. It just meets requirements to be placed inside foreach loop, so you, for example, can not past it to a function as IEnumerable...
"A type C is said to be a collection type if it implements the System.Collections.IEnumerable interface or implements the collection pattern by meeting all of the following criteria:

C contains a public instance method with the signature GetEnumerator() that returns a struct-type, class-type, or interface-type, which is called E in the following text.
E contains a public instance method with the signature MoveNext() and the return type bool.
E contains a public instance property named Current that permits reading the current value. The type of this property is said to be the element type of the collection type."