How to get logged in user count in SharePoint

In this tutorial, we will discuss, how to get logged in user count in SharePoint using server object model code.

Get logged in user count in SharePoint

Now, we will see how to track the logged in user count in SharePoint site using SharePoint server object model code.

Step 1: Create a list in SharePoint as “Tracking List” which will keep user information like UserName and one more column called Created.

Step 2: If you have enabled FBA in your application, then after successful login, apply the below code to insert the userName in the tracking list.

 SPSecurity.RunWithElevatedPrivileges(delegate ()
                {
                    using (SPSite spSite = new SPSite(SPContext.Current.Site.Url))
                    {
                        using (SPWeb spWeb = spSite.OpenWeb())
                        {
                            SPList spList = spWeb.Lists[Tracking List];
                            spList.ParentWeb.AllowUnsafeUpdates = true;
                            SPListItem item = spList.Items.Add();
                            item["userName "] = Email.tostring();
                            item.Update();
                            spList.ParentWeb.AllowUnsafeUpdates = false;
                        }
                    }
                });

Step 3: Next create a method to filter the count from the tracking list using the SharePoint server object model.

public static VisitorLog GetMaxsiteVisitor()
        {
            try
            {
                VisitorLog VisitorLogs = new VisitorLogs ();
                SPSecurity.RunWithElevatedPrivileges(delegate ()
                {
                    using (SPSite spSite = new SPSite(SPContext.Current.Site.Url))
                    {
                        using (SPWeb web = spSite.OpenWeb())
                        {
                            
                            DateTime curntDt = DateTime.Now;

                            SPList spList = web.Lists[Tracking List];
                            SPQuery querygetTodayCount = new SPQuery();                           

                            string CurrentDate = curntDt.ToString("yyyy-MM-ddTHH:mm:ssZ");                    

                            querygetTodayCount.ViewXml = @"<View><Query><Where><Geq><FieldRef Name='Created'/><Value Type='DateTime' IncludeTimeValue='FALSE'>" + CurrentDate + "</Value></Geq></Where></Query></View>";

                            SPListItemCollection itemsTodayCount = spList.GetItems(querygetTodayCount);
                            VisitorLogs.UserCountToday = 0;
                          

                            if (itemsTodayCount != null && itemsTodayCount.Count > 0)
                            {

                                VisitorLogs.UserCountToday = itemsTodayCount.Count;
                            }
                           
                        }
                    }

                });
                return VisitorLogs;
            }

            catch (Exception ex)
            {
                CommonHelper.ProcessDataException(ex);
                return null;
            }
        }

In the above code, I filter the created column to get the count on how many users are login to the application.

This is the one type of easiest way to track how many users are logged in to the site today.

image 27

You may like following SharePoint server obejct model tutorials:

In this SharePoint tutorial, we will discuss how to get logged in user count in SharePoint using server object model.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>