Friday, December 30, 2011

This code for retrieving the First Name and Last Name from the SharePoint 2010 User Profile Service. Here I determine the currently logged in user through SPWeb class's CurrentUser attributes This code will be useful when you have your own attributes on sharePoint User Profile. Need to specify the internal name of the column.
                SPSite site = SPContext.Current.Site;
                SPWeb web = SPContext.Current.Web;
                SPServiceContext serviceContext = SPServiceContext.GetContext(site);
                UserProfileManager manager = new UserProfileManager(serviceContext);
                UserProfile profile = manager.GetUserProfile(web.CurrentUser.LoginName.ToString());
                ProfileValueCollectionBase baseVal = profile.GetProfileValueCollection("FirstName");
                ProfileValueCollectionBase baseVal = profile.GetProfileValueCollection("LastName");