+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Custom field for Events

Custom field for Events

5 posts, 1 answered
  1. Shalaka
    Shalaka avatar
    50 posts
    Registered:
    24 Mar 2011
    10 Feb 2012
    Link to this post
    Hi,
    I want to add a combo box to my event as a custom field.I have created a usercontrol which is bound to database table.
    it gets added to my event. but due to that field my event data is not getting store.I i remove that field from event, everything is working fine.
    Please provide me some code for doing such thing.

    Thanks & Regards,
  2. Svetoslav Petsov
    Svetoslav Petsov avatar
    456 posts
    Registered:
    24 Sep 2012
    14 Feb 2012
    Link to this post
    Hi,

     Here's how you can achieve this with our Choice Field:

    You can inherit from ChoiceField and modify its Choices collection with the items from your database table. However, you also need to create a Definition and an Element for your custom field. Then, when you register the field, choose the type to be Multiple Choice. Here's how this should look:

    The Custom Choice Field class that inherits from ChoiceField 

    Copy Code
    [FieldDefinitionElement(typeof(CustomChoiceFieldElement))]public classCustomChoiceField : ChoiceField
       
        {
       
            public CustomChoiceField()
       
                base()
       
            {
       
                     
       
            }
       
                 
       
            public override Telerik.Sitefinity.Web.UI.Fields.Enums.RenderChoicesAs RenderChoicesAs
       
            {
       
                get
       
                {
       
                    returnTelerik.Sitefinity.Web.UI.Fields.Enums.RenderChoicesAs.DropDown;
       
                }
       
                set
       
                {
       
                    base.RenderChoicesAs = value;
       
                }
       
            }
       
                 
       
            protected override voidInitializeControls(Telerik.Sitefinity.Web.UI.GenericContainer container)
       
            {
       
                this.Choices.Clear();
       
                ChoiceItem choiceItem = new ChoiceItem();
       
                choiceItem.Value = "first";
       
                ChoiceItem choiceItem2 = new ChoiceItem();
       
                choiceItem2.Value = "second";
       
         
       
         
       
                this.Choices.Add(choiceItem);
       
                this.Choices.Add(choiceItem2);
       
                base.InitializeControls(container);
       
                     
       
                     
       
                     
       
            }
       
            protected override string ScriptDescriptorType
       
            {
       
                get
       
                {
       
                    return typeof(ChoiceField).FullName;
       
                }
       
            }
       
         
       
        }
    You can see here that I'm clearing the Choices collection and populating it with new records. This is the place where you can populate the collection with your custom items from the data table.

    Also here are the relevant element and definition for the field control: 

    Copy Code
    class CustomChoiceFieldDefinition : ChoiceFieldDefinition
       
        {
       
            public CustomChoiceFieldDefinition() : base()
       
            {
                      this.FieldType = typeof(CustomChoiceField);
            }
       
         
       
            /// <summary>
       
            /// Initializes a new instance of the <see cref="SimpleImageFieldDefinition"/> class.
       
            /// </summary>
       
            /// <param name="element">The configuration element used to persist the control definition.</param>
       
            public CustomChoiceFieldDefinition(ConfigElement element)
       
                base(element)
       
            {
                     this.FieldType = typeof(CustomChoiceField);
            }
       
        }

    Copy Code
    class CustomChoiceFieldElement : ChoiceFieldElement
       
        {
       
            /// <summary>
       
            /// Initializes a new instance of the <see cref="SimpleImageFieldElement"/> class.
       
            /// </summary>
       
            /// <param name="parent">The parent.</param>
       
            public CustomChoiceFieldElement(ConfigElement parent)
       
                base(parent)
       
            {
       
            }
       
         
       
                 
       
         
       
            /// <summary>
       
            /// Gets an instance of the <see cref="SimpleImageFieldDefinition"/> class.
       
            /// </summary>
       
            /// <returns></returns>
       
            public override DefinitionBase GetDefinition()
       
            {
       
                return new CustomChoiceFieldDefinition(this);
       
            }
       
        }

    Hope this helps. 

    All the best,
    Svetoslav Petsov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
    Answered
  3. Shalaka
    Shalaka avatar
    50 posts
    Registered:
    24 Mar 2011
    15 Feb 2012
    Link to this post
    Hi,
    This works.
    Thanks a lot

  4. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    04 Nov 2013
    Link to this post
    is this still the correct way to do this? I attempted to follow this, created a custom field just as shown, and I added it to the Documents type no problem, it even shows all the custom items.

    However, when I publish, I get this error: 

    500 (There was an error deserializing the object of type Telerik.Sitefinity.Web.Services.ContentItemContext`1[[Telerik.Sitefinity.Libraries.Model.Document, Telerik.Sitefinity.Model, Version=6.1.4700.0, Culture=neutral, PublicKeyToken=b28c218413bdf563]]. End element 'MyChoices' from namespace '' expected. Found element 'item' from namespace ''.)

    What am I missing?
  5. Stacey
    Stacey avatar
    291 posts
    Registered:
    18 Oct 2012
    15 Apr 2014 in reply to SelAromDotNet
    Link to this post

    Did you ever get an answer on this?  I am trying to add a custom field for BlogPosts that exhibits the same issue when I try to publish.  The Thunder generated control works just find as a custom field for News though.

     

5 posts, 1 answered