My last post was about Contact / Feedback webpart for Sharepoint 2007
In this post I am going to show you how to retrieve current user profile and show the properties like Title, First name, Last Name etc.
using Microsoft.Office.Server.UserProfiles.UserProfileManager()
It is a collection of user profile objects used to access user profile data we need to just call the UserProfileManager() class to access a specific user profile
Here I am going to do the same
using System; using System.Runtime.InteropServices; using System.Web.UI; using System.Web.UI.WebControls.WebParts; using System.Xml.Serialization; using System.Web.UI.WebControls; using Microsoft.Office.Server; using Microsoft.Office.Server.Administration; using Microsoft.Office.Server.UserProfiles; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using Microsoft.SharePoint.WebPartPages; namespace RetrieveUserProfile { [Guid("b95130c6-6bc0-4ce3-8596-778af2321354")] public class RetrieveUserProfile : System.Web.UI.WebControls.WebParts.WebPart { Label oLabelTitle; Label oLabelFirstName; Label oLabelLastname; Label oLabelAboutMe; Label oLabelDept; Label oLabelSipAdd; Label oLabelWorkPhone; Label oLabelWorkEmail; Label oLabelWebsite; Label oLabelResponsibility; Label oLabelMessage; protected override void CreateChildControls() { base.CreateChildControls(); //Label for Retrieving user profiles oLabelTitle = new Label(); this.Controls.Add(oLabelTitle); oLabelFirstName = new Label(); this.Controls.Add(oLabelFirstName); oLabelLastname = new Label(); this.Controls.Add(oLabelLastname); oLabelAboutMe = new Label(); this.Controls.Add(oLabelAboutMe); oLabelDept = new Label(); this.Controls.Add(oLabelDept); oLabelSipAdd = new Label(); this.Controls.Add(oLabelSipAdd); oLabelWorkPhone = new Label(); this.Controls.Add(oLabelWorkPhone); oLabelWorkEmail = new Label(); this.Controls.Add(oLabelWorkEmail); oLabelWebsite = new Label(); this.Controls.Add(oLabelWebsite); oLabelResponsibility = new Label(); this.Controls.Add(oLabelResponsibility); oLabelMessage = new Label(); this.Controls.Add(oLabelMessage); } protected override void Render(System.Web.UI.HtmlTextWriter writer) { SPSecurity.RunWithElevatedPrivileges(delegate() { try { SPSite mySite = SPControl.GetContextSite(Context); SPWeb myWeb = SPControl.GetContextWeb(Context); ServerContext context = ServerContext.GetContext(mySite); UserProfileManager myProfileManager = new UserProfileManager(context); string CurrentUser = SPContext.Current.Web.CurrentUser.LoginName; UserProfile myProfile = myProfileManager.GetUserProfile(CurrentUser); if (myProfile[PropertyConstants.Title].Value != null) { oLabelTitle.Text = myProfile[PropertyConstants.Title].Value.ToString(); } if (myProfile[PropertyConstants.FirstName].Value != null) { oLabelFirstName.Text = myProfile[PropertyConstants.FirstName].Value.ToString(); } if (myProfile[PropertyConstants.LastName].Value != null) { oLabelLastname.Text = myProfile[PropertyConstants.LastName].Value.ToString(); } if (myProfile[PropertyConstants.AboutMe].Value != null) { oLabelAboutMe.Text = myProfile[PropertyConstants.AboutMe].Value.ToString(); } if (myProfile[PropertyConstants.Department].Value != null) { oLabelDept.Text = myProfile[PropertyConstants.Department].Value.ToString(); } if (myProfile[PropertyConstants.SipAddress].Value != null) { oLabelSipAdd.Text = myProfile[PropertyConstants.SipAddress].Value.ToString(); } if (myProfile[PropertyConstants.WorkPhone].Value != null) { oLabelWorkPhone.Text = myProfile[PropertyConstants.WorkPhone].Value.ToString(); } if (myProfile[PropertyConstants.WorkEmail].Value != null) { oLabelWorkEmail.Text = myProfile[PropertyConstants.WorkEmail].Value.ToString(); } if (myProfile[PropertyConstants.WebSite].Value != null) { oLabelWebsite.Text = myProfile[PropertyConstants.WebSite].Value.ToString(); } if (myProfile[PropertyConstants.Responsibility].Value != null) { oLabelResponsibility.Text = myProfile[PropertyConstants.Responsibility].Value.ToString(); } } catch (UserNotFoundException ex) { oLabelMessage.Text = ex.ToString(); } });
This morning I am trying to fix an issue with a client where they wanted to send an appointment using the logged in user as the from address. I told them this was simple thing to do, thinking that all I needed was SPContext.Current.Web.CurrentUser.Email. Of course that was blank, so now three hours later I am still dealing with NullReferenceExceptions trying to get the user’s email working for all users.
MOSS can be such rubbish
Will this code execute for a user who is logged in with reader or limited access rights?
@Petew: Please look into whether the current user email property contains value in user profile property page in Shared Service Administration
I tried using this code but it is ginving the below error
Error 1 Cannot convert type ‘Microsoft.Office.Server.UserProfiles.UserProfile’ to ‘UserProfile.UserProfile’ F:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\UserProfile\UserProfile\UserProfile\UserProfile.cs 91 49 UserProfile
does anybody has a solution?
Hi Will the same code work for Sharepoint 2010 ..
Thanks
Naga Aditya..
please Tell me how to add Sharepoint 7 dll in sharepoint 2010 because i am facing a problem to add servercontext.getyContext() in sharepoint 2010
Thanks a lot, I needed Fist and Last name and it works on SharePoint 2010.
Im thankful for the blog.Really looking forward to read more. Keep writing.