Pages

Subscribe:

Thursday, March 15, 2012

Retrieve and Manage User Profile Properties in SharePoint 2010


The below Code snippet is useful to retrieve the User Profile Properties.
First we have to take the reference of the following dlls and add Namespaces.
(Dlls path, where the SharePoint server or Foundation is installed “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI”)
Microsoft.Office.Server;
Microsoft.Office.Server.UserProfiles;
Microsoft.SharePoint;
//siteColl ? get the current site collection
Microsoft.SharePoint.SPServiceContext serviceContext = Microsoft.SharePoint.SPServiceContext.GetContext(siteColl);
UserProfileManager upm = new Microsoft.Office.Server.UserProfiles.UserProfileManager(serviceContext);
ProfileSubtypePropertyManager pspm = upm.DefaultProfileSubtypeProperties;
Microsoft.Office.Server.UserProfiles.UserProfile profile = null;
//get the current user Login to retrieve the user profile details
bool existingUser = upm.UserExists(Login);
if (existingUser)
{
profile = upm.GetUserProfile(Login);
if (profile[PropertyConstants.FirstName].Value != null)
{
lblName.Text = profile[PropertyConstants.FirstName].Value.ToString() + ”
” + profile[PropertyConstants.LastName].Value.ToString();
}
if (profile[PropertyConstants.Title].Value != null)
{
lblDesgination.Text = profile[PropertyConstants.Title].Value.ToString();
}
if (profile["PictureUrl"].Value.ToString() != “” || profile["PictureUrl"].Value.ToString() != null)
{
UserImage.ImageUrl = profile["PictureUrl"].Value.ToString();
}
}
The following code snippet is useful to get the Colleague details of the User
Microsoft.Office.Server.UserProfiles.UserProfile oUserProfile = upm.GetUserProfile(Login);
foreach (Colleague colleague in oUserProfile.Colleagues.GetItems())
{
string strDesignation = colleague.Profile.GetProfileValueCollection(PropertyConstants.JobTitle).ToString();
lblColleagues.Text += colleague.Profile.DisplayName.ToString()+ ”
“;
lblDesignationcolleague.Text = strDesignation;
}
SharePoint allows us to view and access all the user profile properties we require and even create our own custom fields.
Let’s start by opening Central Administration and navigate to Manage Service Applications > User Profile Service Application, which will take you to the following page:
Click on “Manage User Properties” to view a list of all user field properties SharePoint uses. To either rename the display name or view the actual property name, click on a field and press “Edit”.
The “Name” field (as highlighted below) is not editable and for a very good reason too! These are the property ID’s that we will call when wanting to retrieve their value. All default field names are not editable.
As I stated earlier, you can create your own properties and call them whatever you want. But SharePoint already provides us with so many OOB, you probably won’t need to create anymore anytime soon.

1 comments:

  1. Where would you put this code in and run it from?

    ReplyDelete