Thursday, July 14, 2011

How to get total number of users logged in a day?

There is no direct way of retrieving this information. You have to solution out to get this data. In this article, I have outlined what needs to be done in order to get this data.

Step 1:
- Create a basic delegate control and add it to your MasterPage. In the delegate control, all you do is write code like this to update the OOB SharePoint list that manages all users who have logged into the site. This list can be accessed from this URL

http:/Your Server/_catalogs/users/simple.aspx

========================================Code=========================================================

//You have to run this with elevated privilidges as the user would not have access

//to this list. The list can only be accessed by Admin.

SPSecurity.RunWithElevatedPrivileges(delegate()

{

try

{

using (SPSite oSiteCollection = new SPSite(SPContext.Current.Site.ID))

{

using (SPWeb oWeb = oSiteCollection.RootWeb)

{

oWeb.AllowUnsafeUpdates = true;

SPList myUserList = oWeb.SiteUserInfoList;

SPListItem myUserItem = myUserList.Items.GetItemById(myLoggedInUser.ID);

myUserItem["Name"] = myUser.FirstName + " " + myUser.LastName;

myUserItem["Work e-mail"] = myUser.EmailAddress;

myUserItem.Update();

}

}

}

catch (Exception e1)

{

SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("External", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, "SecurityWebPart.AddPersonalDetailsToSite-" + e1.Message, "");

}

});

========================================Code=========================================================

This code really does not do much, you can even just put the update statement. When you add this code, it will put the time stamp when the user last logged onto your site.

Step 2
Then, write a timer job to extract daily logged user information from the above list and store it in another List. The reason you have to do this is so as to maintain the history.

Step 3
Finally, you can present your information in a webpart or directly from this list.

No comments:

Post a Comment