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

      Tenable updates Vulnerability Priority Rating scoring method to flag fewer vulnerabilities as critical

      July 24, 2025

      Google adds updated workspace templates in Firebase Studio that leverage new Agent mode

      July 24, 2025

      AI and its impact on the developer experience, or ‘where is the joy?’

      July 23, 2025

      Google launches OSS Rebuild tool to improve trust in open source packages

      July 23, 2025

      EcoFlow’s new portable battery stations are lighter and more powerful (DC plug included)

      July 24, 2025

      7 ways Linux can save you money

      July 24, 2025

      My favorite Kindle tablet just got a kids model, and it makes so much sense

      July 24, 2025

      You can turn your Google Photos into video clips now – here’s how

      July 24, 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

      Blade Service Injection: Direct Service Access in Laravel Templates

      July 24, 2025
      Recent

      Blade Service Injection: Direct Service Access in Laravel Templates

      July 24, 2025

      This Week in Laravel: NativePHP Mobile and AI Guidelines from Spatie

      July 24, 2025

      Retrieve the Currently Executing Closure in PHP 8.5

      July 24, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      FOSS Weekly #25.30: AUR Poisoned, Linux Rising, PPA Explained, New Open Source Grammar Checker and More

      July 24, 2025
      Recent

      FOSS Weekly #25.30: AUR Poisoned, Linux Rising, PPA Explained, New Open Source Grammar Checker and More

      July 24, 2025

      How to Open Control Panel in Windows 11

      July 24, 2025

      How to Shut Down Windows 11

      July 24, 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

    Blade Service Injection: Direct Service Access in Laravel Templates

    July 24, 2025
    Development

    This Week in Laravel: NativePHP Mobile and AI Guidelines from Spatie

    July 24, 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

    Call of Duty: Warzone is officially offline. Here’s when you can play it again.

    News & Updates

    Atla AI Introduces the Atla MCP Server: A Local Interface of Purpose-Built LLM Judges via Model Context Protocol (MCP)

    Machine Learning

    Microsoft Edge now lets IT quietly share secure passwords with employees

    Operating Systems

    CVE-2023-37517 – HCL Leap Cross-Site Scripting (XSS)

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    Benchmarking the Orange Pi R2S Single Board Computer

    July 7, 2025

    The Orange Pi R2S features a RISC-V processor, four wired network ports (two 2.5G ethernet…

    CVE-2025-5435 – Marwal Infotech CMS SQL Injection Vulnerability

    June 2, 2025

    CVE-2025-44898 – Fortinet Wireless Access Point Stack Overflow Vulnerability

    May 20, 2025

    The Designer’s Hierarchy of Career Needs

    June 13, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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