Scope: managed applications, mobile applications, and ordinary applications.
1. A session parameter is a variable that contains data about the current user session. To initialize session parameters, embed the code in the session module (see 2.1). The parameter values are mostly used in queries and for access restriction.
Examples:
- CurrentProject — the CatalogRef.Projects type.
- DataExchangeEnabled — the Boolean type.
- ClientWorkstation — the CatalogRef.Workstations type.
Session parameters can be addressed only programmatically. Example:
Project = SessionParameters.CurrentProject;
To set or get a session parameter, the session user must be granted the respective right.
Example of using session parameters for access restriction:
WHERE Document.Author = &CurrentUser
In this case, the session user doesn't require any specific rights to get the session parameter.
2. It is not recommended to use session parameters to store values that are addressed only on the client side. In the 1C:Enterprise client/server mode, session parameters are stored on the server. So, an attempt to read or edit session parameters on the client will result in increased number of server calls and the data transferred.
If a value is addressed only on the client side, store it in a global variable in the managed application or ordinary application module.
3. It is not recommended to use session parameters to cache values that are frequently addressed on the server side. Instead, in the server common module, define a caching function. This approach is reasonable unless the caching function runtime commensurate with the cache cleanup time.
Initializing session parameters on demand
$$$ Do not initialize session parameters at startup. Reason:
- At startup, not all session parameters are addressed.
- Sometimes, the application has to reset the values of parameters session.
The best practice is to employ the SessionParametersSetting handler to set the parameter values on demand. That is, a session parameter must be initialized only on the first call, when the parameter is considered undefined.
Example:
Procedure SessionParametersSetting(SessionParametersNames)
If SessionParametersNames = Undefined Then
// Set session parameters at startup (SessionParametersNames = Undefined)
// Set session parameters that can be initialized
// at startup
Else
// Set session parameters on demand
// If session parameters address the same data during initialization,
// they must be initialized as a batch. To avoid re-initialization, save the names
// of initialized parameters in array SpecifiedParameters
SpecifiedParameters = New Array;
For Each ParameterName From SessionParametersNames Do
SetSessionParameterValue(ParameterName, SpecifiedParameters);
EndDo;
EndIf;
EndProcedure
// Set the values and return the names of the session parameters
// in parameter SpecifiedParameters.
//
// Parameters
// ParameterName - String - the name of the session parameter to be initialized.
// SpecifiedParameters - Array - the array that stores the names of
// the initialized parameters.
//
Procedure SetSessionParameterValue(Val ParameterName, SpecifiedParameters)
// Return if during the call of SessionParametersSetting
// ParameterName has been set.
If SpecifiedParameters.Find(ParameterName) <> Undefined Then
Return;
EndIf;
If ParameterName = "CurrentUser" Then
SessionParameters.CurrentUser = <value>;
SessionParameters.<another session parameter> = <value>;
SpecifiedParameters.Add(ParameterName);
SpecifiedParameters.Add("<another session parameter>");
EnfIf;
EndProcedure