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(); } }); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write("

Title

"); oLabelTitle.RenderControl(writer); writer.Write("

FirstName

"); oLabelFirstName.RenderControl(writer); writer.Write("

Lastname

"); oLabelLastname.RenderControl(writer); writer.Write("

About me

"); oLabelAboutMe.RenderControl(writer); writer.Write("

Department

"); oLabelDept.RenderControl(writer); writer.Write("

Sip Address

"); oLabelSipAdd.RenderControl(writer); writer.Write("

Work phone

"); oLabelWorkPhone.RenderControl(writer); writer.Write("

Work E-mail

"); oLabelWorkEmail.RenderControl(writer); writer.Write("

Website

"); oLabelWebsite.RenderControl(writer); writer.Write("

Responsibility

"); oLabelResponsibility.RenderControl(writer); writer.Write("
"); } } }