Display SPUser Information on an application page
-
29-09-2020 - |
Question
I have an application page (SP2010) where I have a SPUser object (the current user), now I would like to display this SPUser as an Hyperlink on the apsx, linked to his UserProfilePage (userdisp.aspx) like those links in a normal list (field Author or Editor). Sure I could make a new hyperlink control and use the attributes (?) of the SPUser object to build something that looks like it could be OOTB from SP - but it isn't.
Is there any "best practice way" or "official MS way" to display these information? Perhaps so that the lync status of the User will be shown too?
Solution
Have you tried to use SPFieldUser.GetFieldValueAsHtml method?
Update
You can try use this code, but I'm not sure about details because it from my head and I cannot test it now.
SPUser user = SPContext.Current.Web.CurrentUser;
SPFieldUser userField = SPContext.Current.Web.Fields[SPBuiltInFieldId.AssignedTo] as SPFieldUser;
string userLookup = string.Format("{0};#{1}", user.ID, user.Name);
string link = userField.GetFieldValueAsHtml(userLookup);