Welcome to Atlanta .NET Regular Guys Sign in | Join | Help

SharePoint 2007 - Accessing User Profile properties

For my current project, I have a need to access custom mapped properties of a user profile.  In Office Server 2007, we can create additional profile properties.

We do this in Shared Services Administration by clicking on "User profiles and properties" under "User Profiles and My Sites", and then "Add profile property" under "User Profile Properties":

then

I have gone thru this exercise and added several "custom" properties.  These properties are then mapped to properties in Active Directory (AD) and imported on a schedule.  This import did not work in Beta2, but if anyone is wondering, does in fact work in the Beta2TR.

I have added the "Microsoft.Office.Server" and "Microsoft.Office.Server.UserProfiles" namespaces to my code and cannot seem to find a way to access these values for the currently logged on user.  So, if a value gets stored in one of the many available and unused properties of an AD User, such as "employeeId" using a tool such as "adsiedit.msc" supplied with the Windows Server 2003 Support Tools or by Microsoft Identity Integration Server, how can that property be accessed thru the Office Server 2007 object model if it is mapped to a property in SharePoint, such as BadgeNumber or something like that?

I see an UserProfile object in the SDK but none of the properties seem appropriate for this task.  Is it missing or am I looking in the wrong place.

Well, I just had one of those moments.  I am working on this as I write this post.  Look closely at the "Edit User Profile Property" screen:

There is a "Name" property AND a "Display Name" property.  I had been looking at the wrong property all along as the display name of the property I was interested in was in fact different than the name and in order to access its value you need the name.  At any rate, here is a snippet that will get you the values of the custom properties should you need them.

 

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

...

// get the BadgeNumber for this user from the profile store
string _badgeNumber = string.Empty;
SPSite _SPSite = null;

try
{
    // TODO: move this to a configuration setting somewhere
    // get a reference to the current site
    _SPSite = new SPSite("http://SITEURL");

    // get a reference to the context of the current site
    ServerContext _ServerContext = ServerContext.GetContext(_SPSite);

    // get a UserProfileManager
    UserProfileManager _UserProfileManager = new UserProfileManager(_ServerContext);

    // get the UserProfile of the logged on user
    UserProfile _CurrentUserUserProfile = _UserProfileManager.GetUserProfile(System.Web.HttpContext.Current.User.Identity.Name);

    _badgeNumber = (string)_CurrentUserUserProfile["badgeNumber "].Value;  // NOT ["Badge Number"], that was my initial mistake, using display name instead of name :)
}
finally
{
    _SPSite.RootWeb.Dispose();
    _SPSite.Dispose();
}

...

I hope someone gets some use out of this.  I sure will!

Published Thursday, October 26, 2006 8:30 AM by Dan Attis
Filed Under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: SharePoint 2007 - Accessing User Profile properties

Everyone congratulate Dan.  This post represents his first ever contribution to the collective.  Dan's been a frequent asker of questions in newsgroups and on blogs (and in IM and e-mail).  This is the first time he's ever offered up for general consumption a coded solution to a problem.
Thursday, October 26, 2006 9:37 AM by Matt Ranlett

# re: SharePoint 2007 - Accessing User Profile properties

Dan, Will you provide more details about the referenced classes for this example...  I'm getting various "The type or namespace name'ServerContext' could not be found"

I'm running B2TR, VS on the server...
Tuesday, November 14, 2006 7:57 AM by Bob C

# re: SharePoint 2007 - Accessing User Profile properties

Oops... My Bad.  I was missing the Microsoft.Office.Server reference...:)
Tuesday, November 14, 2006 8:26 AM by Bob C

# re: SharePoint 2007 - Accessing User Profile properties

Hey Dan
If I want to loop all users in my SharePoint in a webpart (that is not running in administrator context) how do I do?

My code is:
using (SPSite site = new SPSite("http://SITEURL"))
{
      ServerContext context = ServerContext.GetContext(site);
      UserProfileManager profileManager = new UserProfileManager(context);
      foreach(UserProfile user in profileManager)
      {
          //Do stuff
      }
}

But I keep getting an error:
System.UnauthorizedAccessException: Access Denied: Only an administrator may enumerate through all user profiles.

My email adi(at)dkis.dk
Wednesday, January 24, 2007 6:43 AM by Ameq

# Brilliant text..

i am gonna show this to my friend, man
Monday, September 22, 2008 9:49 AM by ZoomRits

# thank you

thats it, brother
Sunday, September 28, 2008 4:23 AM by SlalfMoxwafrar

# re: SharePoint 2007 - Accessing User Profile properties

Thursday, January 15, 2009 10:08 AM by Ramprasad

What do you think?

(required) 
required 
(required)