I'm writing this post based on my experience with the UserProfileManager in SharePoint 2007. I've seen many posts about people having issues using the UserProfileManager for normal users in SharePoint.
I will start with showing you how to access the user profiles using the UserProfileManager. There are three ways to loop through the User Profiles, which are:
1. For Each
// get the server context
Microsoft.Office.Server.ServerContext ctx = Microsoft.Office.Server.ServerContext.GetContext(HttpContext.Current);
// get the user profile manager
UserProfileManager userProfileManager = new UserProfileManager(ctx);
// get the count of the user profiles
int numberOfProfiles = userProfileManager.Count;
// loop through the user profiles
foreach(UserProfile profile in userProfileManager)
{
// do some action on each currentProfile
}
2. For Loop
// get the server context
Microsoft.Office.Server.ServerContext ctx = Microsoft.Office.Server.ServerContext.GetContext(HttpContext.Current);
// get the user profile manager
UserProfileManager userProfileManager = new UserProfileManager(ctx);
// get the count of the user profiles
int numberOfProfiles = userProfileManager.Count;
// loop through the user profiles
for (int i = 0; i < numberOfProfiles; i++)
{
try
{
UserProfile profile = userProfileManager.GetUserProfile(i);
// do some action on each currentProfile
}
catch (Exception ex)
{
// failed to get user profile
// log the error
}
}
3. GetEnumerator
// get the server context
Microsoft.Office.Server.ServerContext ctx = Microsoft.Office.Server.ServerContext.GetContext(HttpContext.Current);
// get the user profile manager
UserProfileManager userProfileManager = new UserProfileManager(ctx);
UserProfile currentProfile;
// get the enumerator
System.Collections.IEnumerator enumProfs = userProfileManager.GetEnumerator();
bool continueEnum = true;
// loop through profiles
while (continueEnum)
{
try
{
// move to next profile
continueEnum = enumProfs.MoveNext();
}
catch (Exception ex)
{
// failed to get user profile
// log the error
// try to move to the next user profile
continueEnum = enumProfs.MoveNext();
}
currentProfile = (UserProfile)enumProfs.Current;
// do some action on each currentProfile
}
Using either of these methods with a user without rights for "Manage User Profiles" (set in the SSP Administration) returns a 403 Error in IIS.
If you debug the code you will receive the following error message:
After reviewing the MSDN Help, I found ...
<snip>
You must create the UserProfileManager object before accessing a UserProfile object. You can then retrieve the user profiles to which the user has access. Full access to everyone's user profile requires "Manage User Profiles" rights. Full access to your own profile requires "Use Personal Features" rights. Everyone has read access to all profiles.
</snip>
A workaround could be to iterate through a set number and trying to retrieve the profile relating to the current integer, for example:
// get the server context
Microsoft.Office.Server.ServerContext ctx = Microsoft.Office.Server.ServerContext.GetContext(HttpContext.Current);
// get the user profile manager
UserProfileManager userProfileManager = new UserProfileManager(ctx);
// set the number of profiles to iterate through
int numberOfProfiles = 400;
// loop through the user profiles
for (int i = 0; i < numberOfProfiles; i++)
{
try
{
UserProfile profile = userProfileManager.GetUserProfile(i);
// do some action on each currentProfile
}
catch (Exception ex)
{
// failed to get user profile
// log the error
}
}
I wouldn't recommend using this approach. I feel the best approach would be to perform a search for people in SharePoint. This can be created using a CAML query against the API or Web Service. I will follow up this article with steps to achieve this. A search would return public properties exposed to the search and wouldn't need administrative priviledges.
On summary ...
Iterating through the user profiles using UserProfileManager requires "Manage User Profiles" rights and therefore is an administrative function.
Only use the UserProfileManager if you are designing an administrative tool to modify the user profiles. You should only use the UserProfileManager to retrieve a single user profile (either the current user or a known account by using the domain/username).