Skip to content

User Preferences

nairdo edited this page Dec 20, 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 GetUserValue(string key) and a SetUserValue(string key, string value) method that you can use in your blocks to save the currently logged in user's preferences.

GetUserValue

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 userValue in rockBlock.GetUserValues( keyPrefix ) )
    {
        _userValues.Add( userValue.Key.Replace( keyPrefix, string.Empty ), userValue.Value );
    }

SetUserValue

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 userValue in _userValues )
        {
            rockBlock.SetUserValue( keyPrefix + userValue.Key, userValue.Value );
        }
    }

Clone this wiki locally