
Editorial illustration.
Originally published 16 June 2023; rewritten and reviewed on 15 October 2026.
Every ASP.NET developer who has maintained Kentico sites for a few years ends up with a personal scratchpad of helper snippets. They save real time when you are wiring up an email notification, a quick “Add to Calendar” button, a pager expression, or a background import job. The trouble starts when those five-line snippets get copied from a Kentico 11 Web Forms project into a Kentico 13 MVC Core solution—or handed to a junior developer—without the surrounding assumptions.
Looking back at the snippet collection from my original 2023 post, the code itself was only half the story. What actually makes a snippet safe to reuse is knowing which application boundary it touches and what breaks when the context shifts.
Keep email template resolution explicit and observable
A common helper pattern wraps EmailTemplateInfoProvider.GetEmailTemplate and EmailSender.SendEmailWithTemplateText (or IEmailService in newer Xperience releases) into a single SendNotification(...) method that returns a bool. That convenience can hide two nasty bugs in production.
First, template resolution depends on site context and macro encoding. Always pass the explicit site name or site ID, resolve macros through a dedicated MacroResolver instance populated with known keys, and encode any user-supplied values before they hit an HTML email body. Second, returning true from EmailSender.SendEmailWithTemplateText usually means the message was handed to the CMS_EmailQueue table, not that SMTP accepted it. If your site relays mail through a strict corporate SMTP server or SendGrid, keep queue failures visible in the Event Log and monitor stuck emails rather than assuming true equals delivery.
var template = EmailTemplateInfo.Provider.Get("OrderConfirmation", siteId);
if (template == null)
{
throw new InvalidOperationException($"Missing email template 'OrderConfirmation' for site {siteId}.");
}
var resolver = MacroContext.CurrentResolver.CreateChild();
resolver.SetNamedSourceData("OrderNumber", orderNumber);
resolver.SetNamedSourceData("CustomerName", HTMLHelper.HTMLEncode(customerName));
Treat calendar links and paging expressions as contracts
An “Add to Calendar” link looks trivial until an international user opens it and the webinar lands at 2 a.m. instead of 10 a.m. Treat those URLs as structured data: convert start and end timestamps explicitly to UTC (yyyyMMddTHHmmssZ) or include the IANA time zone (Asia/Kuala_Lumpur), URL-encode the title and location with Uri.EscapeDataString, and verify the generated link in both Google Calendar and Outlook. Never leave a hard-coded sample date from a blog post inside a shared Razor partial.
Paging expressions carry the same risk of silent off-by-one bugs. Whether you are binding a legacy CMSRepeater or writing a custom DocumentQuery pager in C#, document and test three boundary cases: zero results, a single-page result, and the final page when the remainder is smaller than pageSize. Knowing whether the underlying API expects zero-based or one-based page indices saves hours of debugging duplicate items on page two.
Handle logging suppression and async context deliberately
One snippet in the older collection wrapped an operation in using (new CMSActionContext { LogEvents = false }) to quiet down noisy Event Log entries during bulk updates. That flag is sharp enough to cut both ways. Suppressing logs makes sense when a nightly sync touches 10,000 SKU records and you log a single summary entry at the end; it is dangerous when applied broadly around user-facing form submissions or permission changes where audit trails matter.
The other classic trap in Kentico is HttpContext.Current turning null inside CMSThread or Task.Run background work. Forcing PortalContext.ViewMode = ViewModeEnum.LiveSite globally is rarely the right repair—it papers over a missing request scope and can leak unintended state across threads. Instead, capture the site ID, culture code, and record identifiers before queuing the background task, and pass them in as plain parameters:
var jobContext = new ImportJobInput(
SiteId: SiteContext.CurrentSiteID,
Culture: LocalizationContext.CurrentCulture.CultureCode,
InitiatedByUserId: MembershipContext.AuthenticatedUser.UserID
);
CMSThread.RunAsync(() => RunImport(jobContext));
A snippet is only truly reusable when it carries a two-line comment stating the Kentico version it was tested on, the required inputs, and what happens on failure. Without those notes, a shortcut today becomes a mystery dependency six months later.