Retrieving user profile information without using web service

This one I figured out recently. I have always used the UserProfileService.asmx to get the profile properties of users. Even wrote a little function to return the number based on a profile property (so it would work with custom properties). Now, I don't need to do that anymore.

SPWeb mySite = SPContext.Current.Web;
SPSite site = new SPSite(mySite.Url.ToString());
ServerContext context = ServerContext.GetContext(site);
UserProfileManager profileManager = new UserProfileManager(context);
UserProfile prof = profileManager.GetUserProfile(mySite.CurrentUser.ToString());

string PreferredName = (prof[PropertyConstants.PreferredName].Value == null) ? "" : prof[PropertyConstants.PreferredName].Value.ToString();
string PictureUrl = (prof[PropertyConstants.PictureUrl].Value == null) ? DefaultImageUrl : prof[PropertyConstants.PictureUrl].Value.ToString();

You can request all the properties you want using the PropertyConstants method which uses the Microsoft.Office.Server and Microsoft.Office.Server.UserProfiles class.

That is definitely handy... The question now is, is it possible to query all the sharepoint users and place them in a drop down list so you can retrieve details of a particular user (from the drop down list) without using a web service? If it's possible, I haven't figured out how. I only know how to do that by querying the UserProfileService.asmx web service, which I will discuss in my next post.

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.