Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.64 KB | None | 0 0
  1. private readonly string newsType = "Telerik.Sitefinity.News.Model.NewsItem,Telerik.Sitefinity.ContentModules";
  2.  
  3.         protected void Page_Load(object sender, EventArgs e)
  4.         {
  5.             var relationFieldName = "Avatar";
  6.             this.AddRelatedMediaFieldToContext(this.newsType,
  7.             new Dictionary<string, string>()
  8.             {
  9.                 {relationFieldName, string.Empty },
  10.             });
  11.         }
  12.  
  13.         private void AddRelatedMediaFieldToContext(string contentTypeFullName, Dictionary<string, string> relatedFields)
  14.         {
  15.             Type contentType = Type.GetType(contentTypeFullName);
  16.             if (contentType == null)
  17.             {
  18.                 throw new Exception(string.Format("{0} type can't be resolved.", contentTypeFullName));
  19.             }
  20.             var cfContext = new CustomFieldsContext(contentType);
  21.             var relatedDataFields = new Dictionary<string, WcfField>();
  22.             UserFriendlyDataType userFriendlyDataType = UserFriendlyDataType.RelatedMedia;
  23.  
  24.             var allowedImageExtension = Config.Get<LibrariesConfig>().Images.AllowedExensionsSettings;
  25.             var provider = "OpenAccessDataProvider";
  26.  
  27.             foreach (var relatedField in relatedFields)
  28.             {
  29.                 var field = new WcfField()
  30.                 {
  31.                     Name = relatedField.Key,
  32.                     ContentType = contentTypeFullName,
  33.                     FieldTypeKey = userFriendlyDataType.ToString(),
  34.                     IsCustom = true,
  35.  
  36.                     //Field definition
  37.                     Definition = new WcfFieldDefinition()
  38.                     {
  39.                         Title = relatedField.Key,
  40.                         FieldName = relatedField.Key,
  41.                         FieldType = typeof(RelatedMediaField).FullName,
  42.                         AllowMultipleSelection = true,
  43.                         FrontendWidgetTypeName = typeof(NewsView).FullName,
  44.                         RelatedDataProvider = provider,
  45.                         RelatedDataType = typeof(NewsItem).FullName,
  46.                         MediaType = "image",
  47.                         ValidatorDefinition = new Telerik.Sitefinity.Web.UI.Validation.Definitions.ValidatorDefinition(),
  48.                         FileExtensions = allowedImageExtension
  49.                     }
  50.                 };
  51.  
  52.                 relatedDataFields.Add(relatedField.Key, field);
  53.             }
  54.  
  55.             cfContext.AddOrUpdateCustomFields(relatedDataFields, contentType.Name);
  56.             cfContext.SaveChanges();
  57.  
  58.             SystemManager.RestartApplication(OperationReason.KnownKeys.StaticModulesUpdate);
  59.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement