Kentico CMS 6.0 Developer's Guide

Managing on-line users

Managing on-line users

Previous topic Next topic Mail us feedback on this topic!  

Managing on-line users

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

Arrow


API examples for newer versions


Please visit the latest API Examples documentation to view API examples for newer versions of Kentico.



The following example gets a dataset of on-line users and updates their properties.

 

private bool GetOnlineUsers()
{
    string where = "";
    int topN = 10;
    string orderBy = "";
    string location = "";
    string siteName = CMSContext.CurrentSiteName;
    bool includeHidden = true;
    bool includeKicked = false;

 
  // Get DataSet of on-line users
  DataSet users = SessionManager.GetOnlineUsers(where, orderBy, topN, location, siteName, includeHidden, includeKicked);

    if (!DataHelper.DataSourceIsEmpty(users))
    {
        foreach (DataRow userDr in users.Tables[0].Rows)
        {
            // Create object from DataRow
            UserInfo modifyUser = new UserInfo(userDr);
 
            // Update the properties
            modifyUser.FullName = modifyUser.FullName.ToUpper();
 
            // Save the changes
            UserInfoProvider.SetUserInfo(modifyUser);
        }
 
        return true;
    }
 
    return false;
}

 

The following example checks if a specified user is currently on-line.

 

private bool IsUserOnline()
{
    bool includeHidden = true;
 
    // Get user and site objects
    UserInfo user = UserInfoProvider.GetUserInfo(CMSContext.CurrentUser.UserID);
    SiteInfo site = SiteInfoProvider.GetSiteInfo(CMSContext.CurrentSiteName);
 
    if ((user != null) && (site != null))
    {

      // Check if user is on-line
      return SessionManager.IsUserOnline(site.SiteName, user.UserID, includeHidden);

    }
 
    return false;
}

 

The following example kicks a user from a site.

 

private bool KickUser()
{

  // Get the user
  UserInfo kickedUser = UserInfoProvider.GetUserInfo(CMSContext.CurrentUser.UserID);

        
    if (kickedUser != null)
    {
        // Kick the user
        SessionManager.KickUser(kickedUser.UserID);
 
        return true;
    }
 
    return false;
}