MOSS 2007 – Retrieving User Profile Properties from the Code

In many cases, there might be a situation where you want to filter the site data based on the user profiles properties. I had came across the scenario at our client where we had a user profile property – location to associate branch office location to the corporate user profile and later we wanted to filter news information on the intranet MOSS publishing portal based on the logged in user’s location (news content pages based on the page layouts with location site column based on custom field type based on user profile property). In this case, we accessed the user profile data in the code using following-

First step is to populate the user profiles in the SSP with the custom “SiteLocation” property.
Next step is to import the MOSS 2007 assemblies –
using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Administration;
using Microsoft.Office.Server.UserProfiles;

Last step is to retrieve the user profile property in the custom code –
ServerContext context = ServerContext.GetContext(SPContext.Current.Site);
UserProfileManager profileManager = new UserProfileManager(context);
UserProfile userProfile = profileManager.GetUserProfile(SPContext.Current.Web.CurrentUser.LoginName);
string location = userProfile[“SiteLocation”].Value.ToString();

Hope it will be useful.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s