Skip to content

User Preferences

azturner edited this page Dec 31, 2012 · 2 revisions

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.

GetUserPreference

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 );
    }

SetUserPreference

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 );
        }
    }

Clone this wiki locally