---
title: Retrieve all preferences for a user
summary: Load every stored preference row for one user and hydrate Preference DTOs from the result set.
status: draft
owner: SAIF Team
last_reviewed: 2026-07-30
library: SAIF.Framework.Preferences
reference_type: package
operation_id: OP-PREF-002
trigger: Caller invokes PreferenceData.GetPreferencesForUser
---

# Retrieve all preferences for a user

This operation is the library's bulk read path: it loads every preference row stored for a normalized user ID, materializes one `Preference` DTO per row, and deserializes non-empty JSON payloads into `Preference.PreferenceItems`.

| Field | Value |
| --- | --- |
| **Operation ID** | OP-PREF-002 |
| **Trigger** | Caller invokes `PreferenceData.GetPreferencesForUser(userId)`. |
| **Inputs** | `userId` |
| **Outputs** | `List<Preference>` |
| **Primary components** | `PreferenceData.GetPreferencesForUser`, `Preference`, `JavaScriptSerializer`, `SqlHelper.ResultSetQuery` |
| **Entry point** | `SAIF.Framework.Preferences\PreferenceData.cs` |

## Steps

1. Define the SELECT that retrieves `USER_PREFERENCE_SEQ_NUM`, `USER_ID`, `APP_NAME`, `PAGE_NAME`, and `PREFERENCE_TEXT` from [USER_PREFERENCE](../database-tables.md#user_preference) for one user ([SQL-003](../sql/SQL-003-get-preferences-for-user.md), [USER_PREFERENCE](../database-tables.md#user_preference)).
2. Create a `JavaScriptSerializer`, an empty `List<Preference>`, and a fresh parameter list containing `:userid = userId.ToLower()` ([BR-001](../business-rules.md#br-001-user-ids-are-normalized-to-lower-case), [SQL-003](../sql/SQL-003-get-preferences-for-user.md), [USER_PREFERENCE](../database-tables.md#user_preference)).
3. Create a `SqlHelper.ResultSetQuery`, attach the parameters, and execute the SELECT through the active data portal ([SQL-003](../sql/SQL-003-get-preferences-for-user.md), [USER_PREFERENCE](../database-tables.md#user_preference)).
   - Create the portal request in the `SqlHelper.ResultSetQuery` constructor.
   - Add `:userid` to `DataRequest` through `AddParms`.
   - Set `DataRequest.RequestType` to `ResultSet` and `DataRequest.SQLStatement` to the SELECT text.
   - Call `IDataPortalInfo.ProcessPortalRequest`, then cast `DataRequest.Result` back to `clsResultSet`.

   > **Origin**: Steps 3.1–3.4 are implemented by [Execute a data portal request](../../saif-framework-core/operations/execute-data-portal-request.md) (`SAIF.Framework.Core`) and executed on behalf of this operation.
4. Iterate each returned row and create a new `Preference` instance.
   - Copy `USER_PREFERENCE_SEQ_NUM` into `PreferenceId`.
   - Copy `APP_NAME` into `ApplicationName`.
   - Copy `PAGE_NAME` into `Pagename`.
   - Reset `PreferenceItems` to an empty dictionary before examining the payload.
5. Read `PREFERENCE_TEXT`, trim it, and decide whether to deserialize it ([BR-006](../business-rules.md#br-006-preference-item-payload-is-json-encoded-keyvalue-data), [SQL-003](../sql/SQL-003-get-preferences-for-user.md), [USER_PREFERENCE](../database-tables.md#user_preference)).
   - If the trimmed JSON text has length greater than zero, deserialize it into `Dictionary<string, string>` and assign the result to `PreferenceItems`.
   - If the trimmed JSON text is empty, leave `PreferenceItems` as the empty dictionary created in step 4.
6. Append the hydrated `Preference` object to the output list and repeat for the remaining rows.
   - On failure: malformed JSON, portal failures, or row-conversion failures propagate because this method does not catch them.
7. Return the completed `List<Preference>` to the caller.

## Process flow

The following diagram visualizes this operation's control flow.

```mermaid
flowchart TD
	Start([Caller invokes GetPreferencesForUser]) --> Query[Execute user-scoped SELECT]
	Query --> Loop[For each returned row]
	Loop --> Json{Trimmed JSON present?}
	Json -->|Yes| Deserialize[Deserialize to dictionary]
	Json -->|No| Empty[Keep empty dictionary]
	Deserialize --> Add[Add Preference to list]
	Empty --> Add
	Add --> More{More rows?}
	More -->|Yes| Loop
	More -->|No| EndOk([Return List<Preference>])
```

## Failure and edge-case behavior

- An empty `PREFERENCE_TEXT` value produces an empty `PreferenceItems` dictionary rather than a `null` reference.
- Invalid JSON in `PREFERENCE_TEXT` raises a serializer exception that this method does not catch.
- The method applies no application or page filter; it returns every stored row for the normalized user ID.

## Related documentation
- [Operations and processes](../operations-processes.md)
- [Business rules](../business-rules.md)
- [SQL catalog](../sql/index.md)
- [Database tables](../database-tables.md)

## Sources
- SAIF.Framework.Preferences\PreferenceData.cs
- SAIF.Framework.Preferences\Preference.cs
- SAIF.Framework.Core\V1\DataAccessLayer\SqlHelper.vb
- SAIF.Framework.Core\V1\DataAccessLayer\clsResultSet.vb
