8 Jul 2011

Get SharePoint User Profile of Specific User Programmatically

Again, I want to share some of my encapsulated methods. So, I’ve been through so many projects, developing this and that, with the same pattern. It very helpful when you choose to use encapsulation and get it for later use.

This method is about get SharePoint user profile. You can use this code whether you are using SharePoint 2007 or SharePoint 2010. Enjoy… Winking smile

Code Snippet
  1. public static UserProfile GetUserProfile(string LoginName)
  2. {
  3.     return GetUserProfile(SPContext.Current.Site, LoginName);
  4. }
  5. public static UserProfile GetUserProfile(SPSite Site, string LoginName)
  6. {
  7.     return GetUserProfile(Site, LoginName, true);
  8. }
  9. public static UserProfile GetUserProfile(SPSite Site, string LoginName, bool RevertSystemAccount)
  10. {
  11.     //check if SharePoint System Account
  12.     if (IsSharePointSystem(LoginName) && RevertSystemAccount)
  13.         LoginName = Site.WebApplication.ApplicationPool.Username;
  14.     SPUser Usr = Site.RootWeb.EnsureUser(LoginName);
  15.     ServerContext SCtx = ServerContext.GetContext(Site);
  16.     UserProfileManager UPM = new UserProfileManager(SCtx);
  17.     if (UPM.UserExists(Usr.LoginName)) { return UPM.GetUserProfile(Usr.LoginName); }
  18.     else return null;
  19. }

3 komentar:

[give me your ideas, hopes, comments and compliments below...]
[aaand...... +1 if you need to....]