Get user Profile in SharePoint 2010

In this Post I will show you an example of how to access User Profile using SharePoint 2010's Object model

The basic class for working with user profiles is the UserProfileManager which is a part of Microsoft.Office.Server namespace. With the UserProfileManager class, you can basically create, get, and remove user profiles. If you want to maintain the user profile metadata such as properties, you should use the UserProfileConfigManager class because the UserProfileManager class’s metadata access is meant to be read-only.

Now, Lets see how to access a User Profile

* Create a User Profile (if not exist)

// Get the current site context
strUrl = "http://localhost";

SPSite site = new SPSite(strUrl);
SPServiceContext serviceContext = SPServiceContext.GetContext(site);

// Initialize the user profile manager
UserProfileManager upm = new UserProfileManager(serviceContext);

// Create a user profile
string sAccount = "domain\\isha";

if (!upm.UserExists(sAccount))
upm.CreateUserProfile(sAccount);


* Get colleagues from user profile

foreach (var colleague in u.Colleagues.GetCommonColleagues())
{
lbColleagues.Items.Add(colleague.DisplayName);
}


* Latest changes of the current user’s profile

string sAccount = "domain\\isha";

UserProfile u = upm.GetUserProfile(sAccount);

foreach (var change in u.GetChanges())
{
lblOutput.Text += "
" + change.ChangeType + " " + change.ChangedProfile + " "
+ change.EventTime.ToShortDateString();
}

* Get User Profile Picture

See my example with ECMAScript at my new website Get user profile picture using Client Object model

and like this you can access a lot of other User profile properties available using Client OM.

1 comments:

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer or anyone else. Should you have any questions or concerns please e-mail me at sharepointprogrammingblogger@gmail.com .

Copyright (c) 2010 @ myshaepointwork.blogspot.com. All rights are reserved.Do Not Copy.

@ Learning SharePoint.com