Enumerating thru User Profiles
SharePoint Server allows user profile data to be imported from various sources such as the Active Directory, LDAP, and using a Business Data Catalog (BDC) connection to almost any data source that contains user info.
There may be a need to enumerate thru all the user profiles once the data has been imported and perform actions on each user profile.
Here is a simple code example that does this using an enumerator in C#:
SPSite site =
new SPSite("http://localhost");
ServerContext context = ServerContext.GetContext(site);
UserProfileManager upm = new UserProfileManager(context);
UserProfile currentProfile;
IEnumerator enumProfs = upm.GetEnumerator();
bool continueEnum = true;
while (continueEnum)
{
try
{
continueEnum = enumProfs.MoveNext();
}
catch (Exception e)
{
Console.WriteLine("EXCEPTION in enum - try to move to next: " + e.ToString());
continueEnum = enumProfs.MoveNext();
}
currentProfile = (
UserProfile)enumProfs.Current;
// do some action on each currentProfile
}
Note that I put the try/catch block to handle the scenario that as you are running the tool any profiles get modified since the import was performed. This would throw an exception and that can be handled any way perferred including what I've done which is simply to write the error to the console and continue.