Create custom fields for subscribers

This article demonstrates way how to programmatically create a custom field for email subscribers. You can use this field as a custom field in the email campaign - for example, for title or country.

NOTE: By default, this field can not be accessed in the backend. If needed, you can create your own widget and place it on a page to display the field. For more information, see Develop widgets.

The example below creates two custom fields for subscribers - Title and Country.
First, create the fields in the database, by running the following code:

C#
using Telerik.Sitefinity.Data.Metadata;
using Telerik.Sitefinity.Newsletters.Model;

namespace SitefinityWebApp
{
    public class Marketing_CreateCustomFieldForSubscribers
    {
        public void CreateCustomFieldForSubscribers()
        { 
            var metadataManager = MetadataManager.GetManager();
            var dynamicType = metadataManager.GetMetaType(typeof(Subscriber));
            if (dynamicType == null)
                dynamicType = metadataManager.CreateMetaType(typeof(Subscriber));
  
            // Create a field for Title in the database
            var metaField = metadataManager.CreateMetafield("Title");
            metaField.DBSqlType = "NVARCHAR(100)";
            metaField.DBType = "LONGVARCHAR";
            metaField.ClrType = typeof(string).FullName;
            dynamicType.Fields.Add(metaField);
  
            // Create a field for Country in the database
            var metaField2 = metadataManager.CreateMetafield("Country");
            metaField2.DBSqlType = "NVARCHAR(100)";
            metaField2.DBType = "LONGVARCHAR";
            metaField2.ClrType = typeof(string).FullName;
            dynamicType.Fields.Add(metaField2);
  
            metadataManager.SaveChanges();
        }
    }
}

Then, fill out the fields by using the code bellow. The following sample fills the fields of all the subscribers with the same data:

C#
using System.Linq;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity.Modules.Newsletters;
using Telerik.Sitefinity.Newsletters.Model;

namespace SitefinityWebApp
{
    public class Marketing_FillOutCustomFieldForSubscribers
    {
        public void FillOutCustomFieldForSubscribers()
        {
            NewslettersManager newslettersMamager = NewslettersManager.GetManager();

            // Get the mailing list you need *
            MailingList mailingList = newslettersMamager.GetMailingLists().FirstOrDefault();

            if (mailingList != null)
            {
                // Get all subscribers (or the subscriber you need **)
                var subscribers = newslettersMamager.GetSubscribers().ToList();
                for (int i = 0; i < subscribers.Count(); i++)
                {
                    subscribers[i].SetValue("Title", "Mr.");
                    subscribers[i].SetValue("Country", "BG");

                    // Save the changes
                    newslettersMamager.SaveChanges();
                }
            }
        }
    }
}

The fields, created above, can be used in the Like a web page mail template the same way as the other dynamic fields - {|Subscriber.Title|} and {|Subscriber.Country|}

The complete method for getting the fields' values looks like this:

C#
using System.Linq;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity.Modules.Newsletters;
using Telerik.Sitefinity.Newsletters.Model;

namespace SitefinityWebApp
{
    public class Marketing_GetCustomFieldForSubscribers
    {
        public void GetCustomFieldForSubscriberse()
        {
            NewslettersManager newslettersMamager = NewslettersManager.GetManager();

            // Get the mailing list you need *
            MailingList mailingList = newslettersMamager.GetMailingLists().FirstOrDefault();

            if (mailingList != null)
            {
                // Get all subscribers (or the subscriber you need **)
                var subscribers = newslettersMamager.GetSubscribers().ToList();
                for (int i = 0; i < subscribers.Count(); i++)
                {
                    var title = subscribers[i].GetValue("Title");
                    var country = subscribers[i].GetValue("Country");
                }
            }
        }
    }
}

This section contains

Import subscribers with custom fields
Import subscribers with custom fields by creating in Sitefinity CMS a separate user widget or page for this specific purpose.
Export subscribers with custom fields
Export subscribers with custom fields from Sitefinity CMS to a dedicated CSV file by creating a separate user widget or page.
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.
New to Sitefinity?