-
Notifications
You must be signed in to change notification settings - Fork 12
User Preferences
Blocks and Pages have a feature which facilitates easily reading and saving a users preferences (person settings/attributes). The base Page and Block classes have a GetUserPreference(string key) and a SetUserPreference(string key, string value) method that you can use in your blocks to save the currently logged in user's preferences.
A user's preferences can be retrieved and then used as seen in this example from the Rock Grid filter. Here we see the Rock:Grid retrieving a collection of the current block's filter preferences previously stored for the current user:
string keyPrefix = string.Format( "grid-filter-{0}-", rockBlock.CurrentBlock.Id );
foreach ( var userPreference in rockBlock.GetUserPreferences( keyPrefix ) )
{
_userPreferences.Add( userPreference .Key.Replace( keyPrefix, string.Empty ), userPreference .Value );
}A user's preferences can be stored for later use. In this example you can see the Rock:Grid saving a collection of the current block's filter preferences previously stored for the current user:
RockBlock rockBlock = this.RockBlock();
if ( rockBlock != null )
{
string keyPrefix = string.Format( "grid-filter-{0}-", rockBlock.CurrentBlock.Id );
foreach ( var userPreference in _userPreferences )
{
rockBlock.SetUserPreference( keyPrefix + userPreference .Key, userPreference .Value );
}
}