Here I am sharing one webpart in sharepoint with u all that will display user information like User Name, Login Name, Email Id, and Groups of current login user.
So, start here first open Vs 2005 or Vs 2008 and click on sharepoint extension and create one webpart with c#.
Give some appopriate name to that webpart Example : CurrentLoginuser. then open c# file of that webpart.
CurrentLoginuser.cs
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.Office.Server.UserProfiles;
using System.Security.Principal;
using System.Threading;
namespace LoginUserWp
{
[Guid("06f570ee-f6d3-4ad0-b623-064f1dd5804b")]
public class CurrentLoginUser : System.Web.UI.WebControls.WebParts.WebPart
{
Label label;
Image Photo;
string str;
public CurrentLoginUser()
{
}
protected override void CreateChildControls()
{
Photo = new Image();
Photo.Width = Unit.Pixel(150);
Photo.Height = Unit.Pixel(150);
label = new Label();
this.Controls.Add(label);
}
protected override void Render(HtmlTextWriter writer)
{
GetLoginUser();
this.Photo.RenderControl(writer);
this.label.RenderControl(writer);
}
public void GetLoginUser()
{
try
{
SPWeb web = SPControl.GetContextWeb(Context);
SPUser sUser = web.CurrentUser;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(web.Site.ID))
{
using (SPWeb oweb = site.OpenWeb(web.ID))
{
SPList list = oweb.Site.RootWeb.Lists["User Information List"];
SPQuery qry = new SPQuery();
qry.Query = "<OrderBy><FieldRef Name='ID' /></OrderBy><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + sUser.Name + "</Value></Eq></Where>";
SPListItemCollection ListCol = list.GetItems(qry);
string[] ImagePath = ListCol[0]["Picture"].ToString().Split(',');
Photo.ImageUrl = ImagePath[0].ToString();
str += "<BR>" + "Name:" + sUser.Name + "<BR>";
str += "Login Name :" + sUser.LoginName + "<BR>";
str += "Email:" + sUser.Email + "<BR>";
str += "Groups:" + sUser.Groups.Count + "<BR>";
foreach (SPGroup grp in sUser.Groups)
{
str += " * " + grp.Name + " total Members:" + grp.Users.Count + "<BR>";
}
}
}
});
}
catch (Exception e)
{
label.Text = "No Current User";
}
label.Text = str;
}
}
}
Now build Webpart and then deploy on your sharepoint site. Output will look like this..


e frhnPosted Feb 22, 2013, 1:50 AM
Hi Chandresh,thanks for your article,I want to use your code for sharepoint 2010 ,I made a Visual Webpart with Vs2010 and put your code there,then I deployed and use it in a team site but it only display a blank image nothing else,what i did wrong? I really appreciate your help...
Neel BPosted Jul 10, 2012, 4:11 PM
Chandresh, Great article, I have another question, can you Please guide on how to post the user info to another page. My scenario is some reports needs parameters from AD, Now i do have the ad vaues in the web part, How to post it to another page in same doamin Thank you Neel
manoj nayakPosted Jan 28, 2011, 12:39 AM
Hi Chandresh, This article is very nice, I deploye this webpart but its not working, I have created so many users in my system level, and those users are added in sharepoint central administration, as per different user have different permission when they login, that time as per permission they work properly but in the Current Login Webpart is display blank, Can you tell me whats the problem. Thanks, Manoj
ahmad testPosted Jun 23, 2009, 4:55 AM
i try this i don't have image ok it showing image with red x but where is my information i don't see any thing
surya narayanaPosted May 4, 2009, 12:05 PM
hi chandresh, I am trying to deploy this web part and it deployed successfully when i change the setup. bat file main URl.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p> <o:p> </o:p> I can see this webpart in my webpart list but when I click for preview I cant see the picture of that webpart. I am very new to share point so can you please explain me in brief? Thanks Surya<o:p></o:p>