<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title type="text">Blog posts by Rajesh Katare</title><link href="http://world.optimizely.com" /><updated>2023-12-18T19:38:26.0000000Z</updated><id>https://world.optimizely.com/blogs/rajesh-katare/</id> <generator uri="http://world.optimizely.com" version="2.0">Optimizely World</generator> <entry><title>[CMS] SHOW/HIDE PROPERTIES ON SPECIFIC CONDITIONS IN CMS EDITOR</title><link href="https://world.optimizely.com/blogs/rajesh-katare/dates/2023/12/cms-showhide-properties-on-specific-conditions-in-cms-editor/" /><id>&lt;p&gt;In some of my recent discussions with CMS developers, I saw one simple and easy, yet a trick question around Custom Attribute and Conditional Properties.&lt;/p&gt;
&lt;p&gt;One of such question was like, &lt;strong&gt;How would you Show OR Hide a property in the site, on a specified conditional property ???&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Sounds simple, rite!&lt;/p&gt;
&lt;p&gt;But the catch sometimes is, we have learned so much about advanced features, that we overlook at these basic implementations. And thats when you get caught with tricky yet simple implementations :)&lt;/p&gt;
&lt;p&gt;So here is the article which will help you refresh your knowledge, and give a revived, step-wise guide, with implementation, for this query.&lt;/p&gt;
&lt;p&gt;Let&#39;s begin.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Rephrashing The Requirement:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We want a property which has few options;&amp;nbsp;&lt;br /&gt;- which when set [Hide] &quot;Option 1&quot; - Should be able to hide &quot;Option 1&quot; from your defined pagetype on the site editor view.&lt;br /&gt;- which when set [Hide] &quot;Option 2&quot; - Should be able to hide &quot;Option 2&quot; from your defined pagetype on the site editor view.&lt;br /&gt;And default should be, if no [Hide] &quot;Option&quot; is set; all properties should be visible.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Create an Enum Dropdown Option on one defined page.&amp;nbsp;&lt;br /&gt;We will take this as Start Page.&lt;/p&gt;
&lt;pre class=&quot;language-markup&quot;&gt;&lt;code&gt;[Display(GroupName = Global.GroupNames.SiteSettings, Name =&quot;Choose Option To Hide&quot;)]
public virtual string DropdownOptions { get; set; }&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt;&lt;br /&gt;Provide a Dropdown List&#39;ss Options to it.&lt;/p&gt;
&lt;pre class=&quot;language-markup&quot;&gt;&lt;code&gt;public enum DropDownEnumsList
    {
        [EnumSelectionDescription(Text = &quot;Option 1&quot;, Value = &quot;Option 1&quot;)]
        Option1,
        [EnumSelectionDescription(Text = &quot;Option 2&quot;, Value = &quot;Option 2&quot;)]
        Option2
    }&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;language-markup&quot;&gt;&lt;code&gt;public class EnumSelectionDescriptionAttribute : DescriptionAttribute
    {
        public string Text { get; set; }

        public object Value { get; set; }
	}&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;language-markup&quot;&gt;&lt;code&gt;public class SelectOneEnumAttribute : SelectOneAttribute, IMetadataAware
    {
        public SelectOneEnumAttribute(Type enumType)
        {
            EnumType = enumType;
        }

        public Type EnumType { get; set; }

        public new void OnMetadataCreated(ModelMetadata metadata)
        {
            SelectionFactoryType = typeof(EnumSelectionFactory&amp;lt;&amp;gt;).MakeGenericType(EnumType);

            base.OnMetadataCreated(metadata);
        }
    }
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt;&lt;br /&gt;Update Property On StartPage&lt;/p&gt;
&lt;pre class=&quot;language-markup&quot;&gt;&lt;code&gt;[Display(GroupName = Global.GroupNames.SiteSettings, Name =&quot;Choose Option To Hide&quot;)]
        [SelectOneEnum(typeof(DropDownEnumsList))]
        public virtual string DropdownOptions { get; set; }&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt;&lt;br /&gt;Create Show/Hide Properties in one of PageTypes (In our example we will refer to &quot;NewsPage&quot;)&lt;/p&gt;
&lt;pre class=&quot;language-markup&quot;&gt;&lt;code&gt;public virtual bool Option1 { get; set; }

public virtual bool Option2 { get; set; }&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt;&lt;br /&gt;Create Attribute Class Show/Hide&lt;/p&gt;
&lt;pre class=&quot;language-markup&quot;&gt;&lt;code&gt;public class HideSpecificPropertyAttribute : Attribute
    {
        public string SelectedDropDownOption { get; set; }
    }&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt;&lt;br /&gt;Write Editor Descriptor for Show/Hide Class&lt;/p&gt;
&lt;pre class=&quot;language-markup&quot;&gt;&lt;code&gt;public class HidePropertyOnSpecificCondition : EditorDescriptor
    {
        private readonly IContentLoader _contentLoader;
        public HidePropertyOnSpecificCondition(IContentLoader contentLoader)
        {
            _contentLoader = contentLoader;
        }
        public override void ModifyMetadata(ExtendedMetadata metadata, IEnumerable&amp;lt;Attribute&amp;gt; attributes)
        {
            base.ModifyMetadata(metadata, attributes);

            var startPageContentLink = SiteDefinition.Current.StartPage;

            var startPage = _contentLoader.Get&amp;lt;StartPage&amp;gt;(startPageContentLink);


            foreach (var property in metadata.Properties)
            {
                if (property is ContentDataMetadata contentDataMetadata)
                {
                    var hidePropertyInSpecificSiteAttribute = contentDataMetadata.Attributes.FirstOrDefault(x =&amp;gt; x is HideSpecificPropertyAttribute) as HideSpecificPropertyAttribute;

                    if (hidePropertyInSpecificSiteAttribute != null &amp;amp;&amp;amp; string.Equals(
                            hidePropertyInSpecificSiteAttribute.SelectedDropDownOption, startPage.DropdownOptions,
                            StringComparison.InvariantCultureIgnoreCase))
                    {
                        property.ShowForEdit = false;
                    }
                }

            }
        }
    }&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Step 7:&lt;/strong&gt;&lt;br /&gt;Update Show/Hide Properties with Attribute Details&lt;/p&gt;
&lt;pre class=&quot;language-markup&quot;&gt;&lt;code&gt;	[HideSpecificProperty(SelectedDropDownOption = nameof(DropDownEnumsList.Option1))]
        public virtual bool Option1 { get; set; }

        [HideSpecificProperty(SelectedDropDownOption = nameof(DropDownEnumsList.Option2))]
        public virtual bool Option2 { get; set; }&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Step 8: [Golden Step]&lt;/strong&gt;&lt;br /&gt;Add typeOf Details on Editor Descriptor at the top of all methods, to make it work for you :)&lt;/p&gt;
&lt;pre class=&quot;language-markup&quot;&gt;&lt;code&gt;[EditorDescriptorRegistration(TargetType = typeof(ContentData))]&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And there you go...&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Happy Learning :)&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Thank you.&amp;nbsp;&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;</id><updated>2023-12-18T19:38:26.0000000Z</updated><summary type="html">Blog post</summary></entry></feed>