Kentico Code Snippet for Developer

I believe when you start getting to customize Kentico you will referring to Kentico API section in CMS(Old Kentico) and also Kentico API documentation online. Here I share with you some code snippet you can use for your kentico projects.

What is code snippet?

Before I share my code snippet, someone might question what is code snippet.? According to wikipedia, snippet means reusable code, machine code or text. So its a code that you can use for every project, without need to remember. Copy and paste then change the value accordingly to the specific project and you are good to go.   

Email Template

This methos require 3 parameters to pass. first parameter is email template code name, recipients (can be multiple email address, split by ;) and the last parameter is Dictionary<string, object>. The last parameter will be used in email template to map the data. Example in your email template have {%CustomerName%} so it will map with the Dictionary Key where equal to "CustomerName".
    public static bool sendEmail(string emailTemplate, string Recipients, Dictionary<string, object> dataToPass)
    {
        try
        {
            MacroResolver mcr = MacroResolver.GetInstance();
            mcr.SetNamedSourceData(dataToPass);
            EmailMessage msgRecipient = new EmailMessage();
            EmailTemplateInfo EmailTemplate = EmailTemplateProvider.GetEmailTemplate(emailTemplate, SiteContext.CurrentSiteID);
            msgRecipient.From = EmailTemplate.TemplateFrom;
            msgRecipient.Subject = EmailTemplate.TemplateSubject;
            msgRecipient.EmailFormat = EmailFormatEnum.Html;
            msgRecipient.Recipients = Recipients;

            EmailSender.SendEmailWithTemplateText(SiteContext.CurrentSiteName, msgRecipient, EmailTemplate, mcr, true);
            return true;

        }
        catch (Exception ex)
        {
            EventLogProvider.LogException(ex.Source, "sendEmail", ex);
        }
        return false;
    }

Add Calendar Event Link

Google calendar

You can use this link to generate button "Add to calendar" . Basically this snippet will be useful if you have an event page in the website. So user can click on the link to add event details in their google calendar.
 
Domain : https://calendar.google.com/calendar/r/eventedit
Querystrings:- 
  1. text - event name
  2. dates - format (yyyyMMddTHHmmss/yyyyMMddTHHmmss) eg : 20190316T090000/20190316T110000
  3. ctz - Timezone eg : Asia/Kuala_Lumpur
  4. details - Event Details - Think about url length can't more than 2000 words
  5. location - Location of the event
Example full URL : click here
 

Direct Page Transformation - Universal Pager

When you want to use pagination in kentico, you can achieve this by using web part called Universal Pager. In the configuration webpart, you will see one section called Direct Page. Default value is empty and you start to wondering what is the value you should put in the transformation. Here is one of the example 
Page <asp:textbox id="directPageControl" runat="server" style="width: 25px;"%> of <%# Eval("Pages") %>

Turned off Event Log in code wrapper

Logging an error is good but sometime you need to run scheduler to do certain job and the job will automatically recorded in Event Log Module. This general log in Event Log Module will be alot like Update content, publish content, delete content etc . You can turned event log off while wrapping the code like below :   
    using (CMSActionContext ctx = new CMSActionContext())
    {
        ctx.LogEvents = false;
       //your action here.

    }

 

Doing async method - dont forget to add this in the context

When I try to use async method in Kentico, there will be null value where you cannot retrieved NodeAliasPath and AbsoluteURL is null. After debugging few times, this is solution. 
AbsoluteNull.png

Add this line of code in your async method to solve above issue. 
 
 
CMS.PortalEngine.PortalContext.ViewMode = ViewModeEnum.LiveSite;

 


 
*TBC...

 

 

 Back to listing