Challenge
I’ve been working on a project where I needed to show user contact information. As usual, I decided to go the fancy route and implement a tooltip that would display a user’s information when an end-user hovers over a username.
JavaScript
I use jQuery to scan the DOM for an instance of “.user”, get the attribute which contains the user ID, then do an AJAX request to retrieve the JSON data and inject into a tooltip.
Please note: I am later planning to update this post to include the JavaScript code.
References (C#)
This code requires you import a reference to the Microsoft.Office.Server
namespace. Here are my using statements:
using Microsoft.Office.Server; using Microsoft.Office.Server.Administration; using Microsoft.Office.Server.UserProfiles; using Microsoft.SharePoint; using Microsoft.SharePoint.Administration;
The Magic (C#)
The code below expects some parameters to be passed in the post header. In the example below, I am asking for the user ID and then converting that to an Int32. Also notice that the code to retrieve the user’s information is written using Elevated Privileges to impersonate the Administrator account. This prevents denial of access and should only be used in proper situations where Access Control Level (ACL) is not a concern.
private void GetUser(HttpContext context) { string key = HttpContext.Current.Request.Params["id"].ToString(); int id = Convert.ToInt32(key); object user = null; string siteURL = SPContext.Current.Site.Url; SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite site = new SPSite(siteURL)) { ServerContext serverContext = ServerContext.GetContext(context); UserProfileManager profileManager = new UserProfileManager(serverContext); SPUser spUser = site.RootWeb.Users.GetByID(id); UserProfile profile = profileManager.GetUserProfile(spUser.LoginName); user = new { Account = profile["AccountName"].Value, Title = profile["Title"].Value, First = profile["FirstName"].Value, Last = profile["LastName"].Value, PreferredName = profile["PreferredName"].Value, WorkPhone = profile["WorkPhone"].Value, Fax = profile["Fax"].Value, WorkEmail = profile["WorkEmail"].Value, Office = profile["Office"].Value, Manager = profile["Manager"].Value }; } }); JavaScriptSerializer ser = new JavaScriptSerializer(); context.Response.Write(ser.Serialize(user)); }
If this code was of use to you, leave me some love. Recommendations are also appreciated.
Share and Enjoy: Image may be NSFW.Clik here to view.

Clik here to view.

Clik here to view.

Clik here to view.

Clik here to view.

Clik here to view.

Clik here to view.

Clik here to view.

Clik here to view.

Clik here to view.

Clik here to view.

Clik here to view.

No related posts.