Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      The Value-Driven AI Roadmap

      September 9, 2025

      This week in AI updates: Mistral’s new Le Chat features, ChatGPT updates, and more (September 5, 2025)

      September 6, 2025

      Designing For TV: Principles, Patterns And Practical Guidance (Part 2)

      September 5, 2025

      Neo4j introduces new graph architecture that allows operational and analytics workloads to be run together

      September 5, 2025

      ‘Job Hugging’ Trend Emerges as Workers Confront AI Uncertainty

      September 8, 2025

      Distribution Release: MocaccinoOS 25.09

      September 8, 2025

      Composition in CSS

      September 8, 2025

      DataCrunch raises €55M to boost EU AI sovereignty with green cloud infrastructure

      September 8, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Finally, safe array methods in JavaScript

      September 9, 2025
      Recent

      Finally, safe array methods in JavaScript

      September 9, 2025

      Perficient Interviewed for Forrester Report on AI’s Transformative Role in DXPs

      September 9, 2025

      Perficient’s “What If? So What?” Podcast Wins Gold Stevie® Award for Technology Podcast

      September 9, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Distribution Release: MocaccinoOS 25.09

      September 8, 2025
      Recent

      Distribution Release: MocaccinoOS 25.09

      September 8, 2025

      Speed Isn’t Everything When Buying SSDs – Here’s What Really Matters!

      September 8, 2025

      14 Themes for Beautifying Your Ghostty Terminal

      September 8, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Multisite Maximum Item Validation for Content Area or Link Collection in Optimizely CMS-12.

    Multisite Maximum Item Validation for Content Area or Link Collection in Optimizely CMS-12.

    July 23, 2025

    This blog post will discuss MultiSite validation for either ContentArea or LinkItemCollection, which are both capable of storing multiple items. Although we can use the custom MaxItem attribute to validate the ContentArea or LinkItemCollection, the problem arises when the same property is used for multiple sites with different validation limits.

    In a recent project, we were tasked with migrating multiple websites into a single platform using Optimizely. These sites shared common ContentTypes wherever applicable, though their behavior varied slightly depending on the site.

    One of the main challenges involved a ContentType used as the StartPage with the same properties across different sites. While the structure remained the same, the validation rules for its properties differed based on the specific site requirements. A common issue was enforcing a maximum item validation limit on a property like a ContentArea, where each site had a different limit—for example, Site A allowed a maximum of 3 items, while Sites B and C allowed 4 and 5 items, respectively.

    To solve this multisite validation scenario, we implemented a custom validation attribute that dynamically validated the maximum item limit based on the current site context.

    Below are the steps we followed to achieve this.

    • Make a MaxItemsBySitesAttribute custom validation attribute class and add an AttributeUsage attribute with AllowMultiple = true.
    • Then inherit from ValidationAttribute as a base class. This class is used to provide server-side validation rules on content properties for Block, Page, or Media content types.
        [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
        public class MaxItemsBySitesAttribute : ValidationAttribute
        {
            private readonly string[] _siteName;
            private int _max;
    
            public MaxItemsBySitesAttribute(int max, params string[] siteName)
            {
                _max = max;
                _siteName = siteName;
            }
            protected override ValidationResult IsValid(object value, ValidationContext validationContext)
            {
                var siteSpecificLimit = GetSiteSpecificMaxLimitByFieldName(validationContext.MemberName, validationContext?.ObjectType?.BaseType);
                string errorMsg = $"{validationContext.DisplayName}, exceeds the maximum limit of {siteSpecificLimit} items for site {SiteDefinition.Current.Name}";
    
                if (value is ContentArea contentArea)
                {
                    if (contentArea.Count > siteSpecificLimit)
                    {
                        return new ValidationResult(errorMsg);
                    }
                }
                else if (value is LinkItemCollection linkItems)
                {
                    if (linkItems.Count > siteSpecificLimit)
                    {
                        return new ValidationResult(errorMsg);
                    }
                }
    
                return ValidationResult.Success;
            }
    
            private int GetSiteSpecificMaxLimitByFieldName(string fieldName, Type type)
            {
                var propertyInfo = type.GetProperty(fieldName);
                if (propertyInfo != null)
                {
                    var attributes = propertyInfo.GetCustomAttributes<MaxItemsBySitesAttribute>()?.ToList();
                    var siteMaxLimit = attributes.FirstOrDefault(x => x._siteName != null && 
                                                                 x._siteName.Any(site => site == SiteDefinition.Current.Name));
    
                    return siteMaxLimit == null ? 0 : siteMaxLimit._max;
                }
                return 0;
            }
        }
      • The function GetSiteSpecificMaxLimitByFieldName() in the above code played an important role in this class to retrieve decorated attribute(s) [MaxItemsBySites(2, “AlloyBlog”)] and [MaxItemsBySites(3, “AlloyDemo”, “AlloyEvents”)] with specified item limit counts and site names.
    • Then, decorate the [MaxItemsBySites] custom attribute(s) on the ContentArea or LinkItemCollection property by adding the maximum item limit and site(s) name as given below.
    public class StartPage : SitePageData
    {
        [Display(
            GroupName = SystemTabNames.Content,
            Order = 320)]
        [CultureSpecific]
        [MaxItemsBySites(2, "AlloyBlog")]
        [MaxItemsBySites(3, "AlloyDemo", "AlloyEvents")]
        public virtual ContentArea MainContentArea { get; set; }
    }
    • The attribute will receive a trigger and verify the maximum item limit and site name against the site that is currently running and display an error message below if validation matches.

    Site Specific Maxitem Validation Error

     

    By implementing site-specific maximum item validation in your Optimizely CMS multisite, content authors can ensure content consistency, enhance user experience, and maintain precise control over content areas and link collections across diverse properties in different sites.

    In case you want other validation by site-specific, you can use the same approach by changing the code accordingly.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleDesigning Human‑Centered GenAI Workflows for Business Process Automation
    Next Article Honeypot Fields in Sitecore Forms

    Related Posts

    Development

    Leading the QA Charge: Multi-Agent Systems Redefining Automation

    September 9, 2025
    Development

    Stop Duct-Taping AI Agents Together: Meet SmythOS

    September 9, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    This Asus portable monitor transformed my remote work setup (and it’s only $170)

    News & Updates

    Apple Sues Ex-Vision Pro Engineer Di Liu: Accused of Stealing Secrets & Joining Competitor Snap

    Security

    Orange Pi R2S Single Board Computer Running Linux: Power Consumption

    Linux

    CVE-2024-13786 – “WordPress Education Theme PHP Object Injection Vulnerability”

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    Google Quietly Tests Opal, a “Vibe-Coding” App That Turns Text into Mini Web Apps

    August 4, 2025

    Google has launched Opal in Google Labs (U.S. only), letting sketch web apps with simple…

    CVE-2025-5264 – Firefox Local Command Injection Vulnerability

    May 27, 2025

    CVE-2025-20186 – “Cisco Wireless LAN Controller Lobby Ambassador Command Injection Vulnerability”

    May 7, 2025

    CVE-2025-7817 – PHPGurukul Apartment Visitors Management System Cross-Site Scripting

    July 19, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.