When a user registers with an application, he provides information which establishes user's identity. For one application these properties may include name, phone number and email address, for another it could be user id, age and gender. Backendless User Service allows each application to have a custom set of properties associated with the user entity.
Defining properties with the API¶
User properties must be defined in the object representing a user. When user registers or user object is updated, Backendless saves the property values in the
Users table of Backendless Data service. If a property is not declared on the server-side, Backendless automatically creates a definition for it. This behavior can be turned off/on using the Dynamic Schema Definition configuration setting in the console (select Data > Configuration). The setting is turned on by default:
Set/Update User Property¶
When setting or updating a user property, the values must be of "primitive" data types (boolean, string, number, date). To assign a non-primitive value, use the Data relation API.
The following API can be used to set or update properties in a user object:
Methods to set/add user properties are available in the
BackendlessUser class. When registering a new user, the methods below can be used on a new instance of
BackendlessUser. When updating an existing user, the instance must be retrieved from the server (either via login or data object retrieval APIs).
// adding a new property public void AddProperty( string key, object value ) // changing value for an existing property public void SetProperty( string key, object value ) // if a property from "newProps" exists in BackendlessUser object, // it is updated, otherwise, it is added to the object. public void PutProperties( Dictionary<string, object> newProps )
Retrieve User Property Values¶
The following API can be used to get user property values:
// retrieves property value for the specified name identified by the "key" argument public object GetProperty( string key ) // get a complete collection of the user object properties public Dictionary<string, object> Properties;
The example below describes how to retrieve the
phoneNumber user property.
BackendlessUser user = // user object retrieval is out of scope of the example // get the value of the "phoneNumber" property String phoneNumber = (String) user.Properties[ "phoneNumber" ]; // alternatively, the property can be obtained as: String phoneNumber2 = (String) user.GetProperty( "phoneNumber" );
Defining properties with Console¶
User property management is available in the schema editor of the
Users table. To open the schema editor:
- Login to Backendless Console and select your application.
- Click the Data icon, and select the Users table.
- Click the Schema menu.
There are built-in user properties, such as
name properties can be deleted if they do not apply to your app, although it is recommended to keep the
password property cannot be deleted.
It is required that one of the properties is marked as identity. This is the property Backendless uses for the Login and Restore Password operations. As users register, Backendless ensures the value for the identity property is unique.
"password" is a special property/column. Backendless automatically adds the column when an application is created. The following rules apply to the password column:
- Password cannot be removed from the application.
- Password cannot be used as identity.
- Password is a required property