Skip to main content

Thinking out aloud - Dave Hunter's SharePoint Blog

Go Search
Home
Blog
  

Locations of visitors to this page


 Useful Links

  Microsoft UK events
  SharePoint 2007 on CodePlex
  SharePoint Community Portal
  SharePoint User Group
  SharePoint Community Portal
  SharePoint Pedia
  SharePoint MSDN Forums
  SharePoint University
Home > Thinking out aloud - Dave Hunter's SharePoint Blog > Posts > Design Considerations for using the SharePoint UserProfileManager
My thoughts and findings on Microsoft Information worker technologies, including MCMS and SharePoint 2007 (MOSS).
Design Considerations for using the SharePoint UserProfileManager
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 the user doesn't have rights you may think to use the SPSecurity.RunWithElevatedPrivileges for more information http://www.davehunter.co.uk/Blog/Lists/Posts/Post.aspx?ID=43.  RunWithElevatedPrivileges runs the code in the brackets under the SharePoint System Account.  This still fails.
 
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).
  Copyright
This work is licenced under a Creative Commons Attribution-Noncommercial-No Derivative Works 2.0 UK: England & Wales License
  Site Map

Comments

GetUserProfile(long)

Unfortunately UserProfileManager.GetUserProfile(long) loads profile by its recordId (see table UserProfile_Full, column RecordID bigint), NOT index.
Because profiles may be deleted gaps may occur in database, so this method cannot be used....
at 29/05/2008 10:04

Company Directory

I tried to use search and a CAML query to generate a company directory. The problem I ran into was that the search result is returned sorted either by relevance or socail distance (whatever that is). It is obviously not possible to sort by a given field (would that have been too easy??)
You can do the sorting in xslt, only problem: maximum page for person search has 50 entries. So you get several pages, each sorted within, but not over all the pages.

That is why I try to solve this using UserProfileManager.
at 21/10/2008 14:32

 Dave Hunter

I'm currently a Senior Consultant at Netstore 2e2. I specialise in Microsoft Information Worker technologies (especially MOSS, MCMS and .NET), with over 8 years of experience within this area of specialism. 

I have gained two certifications for MOSS and WSS and look to complete the full certification track soon.

View Dave Hunter's profile on LinkedIn

  Subscribe in a reader


 Latest Posts

I’m attending the first ever SharePoint Saturday in the UK31/08/2010 19:43
I’ve re-joined CIBER UK31/08/2010 19:26
I’ve been Awarded a MVP for SharePoint01/04/2010 19:49
SharePoint 2010 Training on Microsoft E-Learning15/03/2010 17:56
SharePoint UK User Group - Thursday 27th August London Meeting25/08/2009 17:56
CAML Query that filters on the current user23/07/2009 17:50
Microsoft Ramp Up Free SharePoint Developer Training22/07/2009 20:59
How To: Change a SharePoint Application Pool Programmatically07/07/2009 18:08
SharePoint Forums Topping 100 Answers06/07/2009 12:29
Find out the SharePoint Internal Name for a Column or Site Column28/05/2009 09:56
1 - 10 Next