Display information of current login user in sharepoint site


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..

Current User Information

Recommended Ebook

SharePoint Framework (SPFx) A Developers Guide

Download Now!
Similar Articles