# Add related media custom field to content items

For each content item you create, for example *NewsItem*, there are default fields, such as author and title. You can add custom media fields that extend the information that the content item holds, for example, an image. Thus, you can relate other content items to the *NewsItem* via the related media custom field.

In the following example, you add programmatically the *Image* custom field as related media to the *NewsItem* content item:

```C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Sitefinity.Configuration;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity.ModuleEditor.Web.Services.Model;
using Telerik.Sitefinity.Modules.Libraries.Configuration;
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 RelatedMediaSnippets
   {
       private readonly string newsType = "Telerik.Sitefinity.News.Model.NewsItem,Telerik.Sitefinity.ContentModules";

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

       private void AddRelatedMediaFieldToContext(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.RelatedMedia;
           var allowedImageExtension = Config.Get<LibrariesConfig>().Images.AllowedExensionsSettings;
           var provider = "OpenAccessDataProvider";
           
           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(RelatedMediaField).FullName,
                       AllowMultipleSelection = true,
                       FrontendWidgetTypeName = typeof(NewsView).FullName,
                       RelatedDataProvider = provider,
                       RelatedDataType = typeof(NewsItem).FullName,
                       MediaType = "image",
                       ValidatorDefinition = new Telerik.Sitefinity.Web.UI.Validation.Definitions.ValidatorDefinition(),
                       FileExtensions = allowedImageExtension
                   }
               };

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

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

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

```

In the `AddRelatedMediaFieldToContext` method above, you:

1. Specify:
   1. Full name of the content type to which you add a media custom field. In this example, the content type is`NewsItem`.
   2. Second parameter as a `Dictionary` collection of related fields.\
      The `Dictionary` collection stores the related media fields, thus associating the custom field name to the related media 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 `RelatedMedia`.

* `IsCustom`\
  Indicates whether the field is a custom field.

* `Definition`\
  Gets or sets a field definition. 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 media. In this example, you specify the type as `Image`.

* Add the created custom field to the `CustomFieldContext` instance using `AddOrUpdateCustomFields `method.

* Save the context to the database and restart the Sitefinity CMS application.
