Implementing a custom user profile

The built-in user profile class (Profile) has fields for Bio, Department, Location, Phone, Tags and Title. By implementing a custom user profile you can modify this to exactly the profile information required for your use case.

In this tutorial we are going to implement a custom profile with fields for birthdate and company.

Extend ProfileBase

Create a new class called CustomProfile. Make sure the class extends the ProfileBase class. The base class already has fileds for name and profile picture and now we want to add two more fields:

using System;
using System.ComponentModel.DataAnnotations;
using System.Runtime.InteropServices;
using Weavy.Core.Models;

namespace Weavy.Models {
    
    [Serializable]
    [Guid("04ac84fa-d691-4058-bcdb-ab498fec6214")]
    public class CustomProfile : ProfileBase {
        
        /// <summary>
        ///  Gets or sets the comapny name.
        /// </summary>
        [StringLength(256)]
        [Display(Name = "Company", Description = "Company name.", Order = 2)]
        public string Company { get; set; }

        /// <summary>
        ///  Gets or sets the birthdate.
        /// </summary>
        [DataType(DataType.Date)]
        [Display(Name = "Birthdate", Description = "Your birthdate.", Order = 3)]
        public DateTime? Birthdate { get; set; }
    }
}

Class attributes

As shown above, profiles must be decorated with the [Serializable] and [Guid] attributes.

Make sure to give your class a unique Guid, otherwise it will not be recognized by Weavy.

Fields

For each piece of information you want to add to your profile you add a property. The property must be declared as public read-write, i.e. the property has the public access modifier and have both a get and a set accessor.

Profile fields must also have one of the following supported types:

  • enum
  • byte
  • short
  • int
  • long
  • bool
  • double
  • float
  • string
  • Guid
  • DateTime
  • TimeSpan

Nullables and Lists of the above types are also supported, e.g. int?, List<string> and List<DateTime?>.

Field attributes

By decorating your fields with one, or more, of the following attributes you can customize how the field is displayed, edited and/or validated.

Datatype

By default, Weavy will look at the property type (in this case DateTime?) when deciding which editor to use for the field. By decorating a field with the [DataType] attribute you can specify an additional type to associate with the field. This value will then be used by the UI to determine which editor to use for the field.

[DataType(DataType.Date)]
public DateTime? Birthdate { get; set; }

Here we specify that the Birthdate field should be treated as a date instead of date and time.

Display

The [Display] attribute lets you specify a name and description to use for the field when displayed in the user interface. It is also possible to set the order in which the field will be rendered.

[Display(Name= "Birthdate", Description = "Your birthdate.", Order = 3)]
public DateTime? Birthdate { get; set; }
Validation

By adding one, or more, [Validation] attributes to your fields you can control how they are validated.

[StringLength(256)]
public string Company { get; set; }

Here we specify that the Company field can have a maximum length of 256 characters.

For full control of validation you can let your class implement IValidatableObject and add your own validation logic.

Update settings.config

In order to use your custom profile instead of the built-in, you have to add the following to the settings.config file.

<appSettings>    
    <add key="weavy.profile-provider" value="Weavy.Models.CustomProfile, Weavy" />    
</appSettings>