Scroll

Server Events: Store User Preferences in a Database

These server events demonstrate how a database can be used to store user preferences. This is a more robust solution than the default browser cookie storage. User preferences will persist independent of which browser they are using, and there is no risk of a user accidentally clearing the cookies.

There are two server events that should be implemented in conjunction with each other. In addition, the following config setting needs to be set to Server Events.

( User Settings ) User Preference Storage Method

Note: These examples are provided for reference only. They will need to be customized for your environment.

Getting User Preferences

Global Event: OnGetUserPreferences

References:

  • sqlutils.dll;
  • system.dll

Namespaces:

  • SqlUtils;
  • System.Data

Code

/**
* Triggered by the OnGetUserPreference Server Event, in conjunction with the User Preference Storage 
* Method (set to Server Event) in User Settings, this hook implementation retrieves the user preference
* from a database table.
* @param sessionInfo is the SessionInfo object as provided by Exago
* @param id is the user preference id
* @return the user preference identified by the given user preference id and user
*/ 
public static string onGetUserPreference(SessionInfo sessionInfo, string id)
{
var db = DbObjectFactory.DataSourceType.[SQLite|SqlServer]; /* data source type */ string retVal = null; try { DbSqlHandler sql = new DbSqlHandler(db, connectionString); sql.Stmt = string.Format("SELECT upValue FROM {0} WHERE upId = @id AND [email protected]; ", "UserPrefs"); sql.SqlParams.Add(DbObjectFactory.GetParameter(db, "@id", id, DbType.String)); sql.SqlParams.Add(DbObjectFactory.GetParameter(db, "@userId", sessionInfo.UserId, DbType.String)); retVal = (string)sql.ExecuteScalarCmd(); } catch { } return retVal; }

Setting User Preferences

Global Event: OnSetUserPreferences

References:

  • sqlutils.dll;
  • system.dll

Namespaces:

  • SqlUtils;
  • System.Data

Code

/**
* Triggered by the OnSetUserPreference Server Event, in conjunction with the User Preference Storage 
* Method (set to Server Event) in User Settings, this hook implementation sets the user preference
* in a database table.
* @param sessionInfo is the SessionInfo object as provided by Exago
* @param id is the user preference id
* @param value is the user preference value
*/
public static void onSetUserPreference(SessionInfo sessionInfo, string id, string value)
{
var db = DbObjectFactory.DataSourceType.[SQLite|SqlServer]; /* data source type */ object objVal = DBNull.Value; if (!string.IsNullOrEmpty(value)) objVal = value; try { DbSqlHandler sql = new DbSqlHandler(db, connectionString); if (string.IsNullOrEmpty(ServerEvents.onGetUserPreference(sessionInfo, id))) { sql.Stmt = string.Format("INSERT INTO {0} (userId, upId, upValue) VALUES (@userId, @id, @upValue)", "UserPrefs"); sql.SqlParams.Add(DbObjectFactory.GetParameter(db, "@userId", sessionInfo.UserId, DbType.String)); sql.SqlParams.Add(DbObjectFactory.GetParameter(db, "@id", id, DbType.String)); sql.SqlParams.Add(DbObjectFactory.GetParameter(db, "@upValue", value, DbType.String)); sql.ExecuteNQCmd(); } else { sql.Stmt = string.Format("UPDATE {0} SET [email protected] WHERE [email protected] AND [email protected]", "UserPrefs"); sql.SqlParams.Add(DbObjectFactory.GetParameter(db, "@userId", sessionInfo.UserId, DbType.String)); sql.SqlParams.Add(DbObjectFactory.GetParameter(db, "@id", id, DbType.String)); sql.SqlParams.Add(DbObjectFactory.GetParameter(db, "@upValue", value, DbType.String)); sql.ExecuteNQCmd(); } } catch { } }

Hidden Article Information

Article Author
Exago Development
created 2017-08-23 15:53:42 UTC
updated 2018-04-16 19:13:26 UTC

Labels
SAE, server event, events, example, user, get, set, preferences, database, sqlutils,
Have more questions? Submit a request