# Add related data custom field to content items

For each content item you create, for example *BlogPosts*, there are default fields, such as author and title. You can add custom fields that extend the information that the content item holds. You can also add a custom field of type *Related data*, for example, *News*. Thus, you can relate other content items to the *BlogPost* s item via the related data custom field.

In the following example, you add programmatically the *News* custom field as related data to the *BlogPosts* content item:

````C#
using System;
using System.Collections.Generic;
using System.Linq;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity.ModuleEditor.Web.Services.Model;
using Telerik.Sitefinity.Modules.News.Web.UI;
using Telerik.Sitefinity.News.Model;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Web.UI.Fields;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules
{
    public partial class RelatedDataSnippets
    {
        private readonly string blogPostType = "Telerik.Sitefinity.Blogs.Model.BlogPost,Telerik.Sitefinity.ContentModules";

        protected void Page_Load(object sender, EventArgs e)
        {
            var relationFieldName = "relatedBlogItems";
            this.AddRelatedDataFieldToContext(this.blogPostType,
            new Dictionary<string, string>()
            {
                {relationFieldName, string.Empty },
  
            });
        }

        private void AddRelatedDataFieldToContext(string contentTypeFullName, Dictionary<string, string> relatedFields)
        {
            Type contentType = Type.GetType(contentTypeFullName);
            if (contentType == null)
            {
                throw new Exception(string.Format("{0} type can't be resolved.", contentTypeFullName));
            }
            var cfContext = new CustomFieldsContext(contentType);
            var relatedDataFields = new Dictionary<string, WcfField>();
            UserFriendlyDataType userFriendlyDataType = UserFriendlyDataType.RelatedData;
            foreach (var relatedField in relatedFields)
            {
                var field = new WcfField()
                {
                    Name = relatedField.Key,
                    ContentType = contentTypeFullName,
                    FieldTypeKey = userFriendlyDataType.ToString(),
                    IsCustom = true,

                    //Field definition
                    Definition = new WcfFieldDefinition()
                    {
                        Title = relatedField.Key,
                        FieldName = relatedField.Key,
                        FieldType = typeof(RelatedDataField).FullName,
                        AllowMultipleSelection = true,
                        FrontendWidgetTypeName = typeof(NewsView).FullName,
                        RelatedDataProvider = relatedField.Value,
                        RelatedDataType = typeof(NewsItem).FullName
                    }
                };

                relatedDataFields.Add(relatedField.Key, field);
            }

            cfContext.AddOrUpdateCustomFields(relatedDataFields, contentType.Name);
            cfContext.SaveChanges();

            SystemManager.RestartApplication(OperationReason.KnownKeys.StaticModulesUpdate);
        }
    }
}

``` In the `AddRelatedDataFieldToContext` method above, you:
 
1. Specify:
    1. Full name of the content type to which you add the custom field. In the example above, this is the `BlogPost` content item: `"Telerik.Sitefinity.Blogs.Model.BlogPost,Telerik.Sitefinity.ContentModules"`
    2. Second parameter as a `Dictionary`  collection of `relatedFields`.   
        The `Dictionary` collection stores the related data fields thus associating the custom field name to the related data field object.
2. Create an instance of the `CustomFieldsContext` class and pass the content type as a parameter.
3. Create an instance of the `WcfField` class to add information about the field, its database mapping, and definition.
4. Set the following properties of the `WcfField` class:
    - `Name`   Gets or sets the name of the field. The value must not contain spaces or any special characters.
    - `ContentType`   Gets or sets the type of the content the field belongs to.
    - `FieldTypeKey`   Gets or sets the `UserFriendlyDataType`` ` value for the field. The class represents an enumeration. In this example, you set the value to `RelatedData`.
    - `IsCustom`   Indicates whether the field is a custom field.
    - `Definition`   Gets or sets a field definition. You must create an instance of the `WcfFieldDefinition`` ` class. This class wraps the information about the field definition. The definition specifies the way the field is represented in the user interface. Set the following properties of the `WcfFieldDefinition` class:
        - `Title`   Gets or sets the display title of the field.
        - `FieldName`   Gets or sets the name of the field. The value must not contain spaces or any special characters.
        - `FieldType`   Gets or sets the field control type that is used to represent the field in user interface.
        - `AllowMultipleSelection`   Gets or sets whether the control supports multiple selection.
        - `FrontendWidgetTypeName`   Gets or sets the type name of the frontend widget.
        - `RelatedDataProvider`   Gets or sets the provider of the related data.
        - `RelatedDataType`   Gets or sets the type of the related data. In this example, you specify the type as `NewsItem`.
5. Add the created custom field to the `CustomFieldContext` instance using `AddOrUpdateCustomFields `method.
6. Save the context to the database and restart the Sitefinity CMS application.
````
