Monday, January 23, 2012

Getting the properties from user profile information using sharepoint object model sharepoint 2010

Below is the code for retrieving the user profile properties using sharepoint object model 2010

                   SPSite Osite = SpContext.Current.Site;
                   SPWeb web = Osite.OpenWeb();
                   var strCurrentUser = web.CurrentUser.Name;

                   SPServiceContext serviceContext = SPServiceContext.GetContext(Osite);
                   UserProfileManager upm = new UserProfileManager(serviceContext);

                   if (!upm.UserExists(strCurrentUser))
                   {
                       UserProfile u = upm.GetUserProfile(strCurrentUser);
                       string uemail = u[PropertyConstants.WorkEmail].ToString();
                       string ufirstname = u[PropertyConstants.FirstName].ToString();
                       string ulastname = u[PropertyConstants.LastName].ToString();
                   }

Note :  If you are remotely logging into the development machine, while debugging you might encounter a pop error -"cannot connect to remote computer" just press f11 again and it will get the value from the user profile and you will be able to debug further without exiting.