1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / How to customize the template for forms notifications email?

How to customize the template for forms notifications email?

2 posts, 0 answered
  1. Nidhi
    Nidhi avatar
    40 posts
    Registered:
    20 Jul 2010
    06 Aug 2012
    Link to this post
    Hi,
    I just began using the Forms Notification feature that was released with Sitefinity 5.1.
    Is there any way we can create a custom email template for it? Or apply some custom styles to the default one?
    Thanks,
    Nidhi
  2. Jen Peleva
    Jen Peleva avatar
    568 posts
    Registered:
    10 Nov 2016
    09 Aug 2012
    Link to this post
    Hi Nidhi,

    Thank you for using our services.

    To change the email template you have to replace the default one with a custom template (htm file) and set its database id to the notification config file. This can be done on an . aspx page. Simply create an htm page, which should be a copy of the default template and in the .aspx page run the following code, which will set your hmtl page, instead of the default one:

    .apsx page

    namespace SitefinityWebApp
    {
        public partial class FormTemplate : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
     
                var formsConfig = Config.Get<FormsConfig>();
                
                    var configManager = ConfigManager.GetManager();
                    formsConfig = configManager.GetSection<FormsConfig>();
     
                    var notificationService = SystemManager.GetNotificationService();
                    var serviceContext = this.GetServiceContext();
                    var messageTemplate = GetDefaultFormEntrySubmittedEmailTemplate();
                    var templateId = notificationService.CreateMessageTemplate(serviceContext, messageTemplate);
     
                    formsConfig.Notifications.FormEntrySubmittedNotificationTemplateId = templateId;
     
                    configManager.SaveSection(formsConfig);
                
            }
     
              ServiceContext GetServiceContext()
            {
                return new ServiceContext("ThisApplicationKey", FormsModule.ModuleName);
            }
     
            public MessageTemplateRequestProxy GetDefaultFormEntrySubmittedEmailTemplate()
            {
                var messageTemplate = new MessageTemplateRequestProxy();
                 
                messageTemplate.Subject = "New form entry is submitted";
                messageTemplate.BodyHtml = Telerik.Sitefinity.Pages.Model.ControlTemplateResolver.ResolveTemplate(
                    "SitefinityWebApp.Form.htm", "SitefinityWebApp");
                return messageTemplate;
            }
        }
    }
    Notice how we user the ResolveTemplate method to "read" our custom htm file. For this purpose, the htm file should have a Build Action set to Embedded resource. This way, after you build the project, the ResolveTemplate method will look  in the SitefinityWebApp assembly for the template. I have attached both the aspx page and the htm page. It is really important to preserve the same html structure in the htm template, as the default one (the one I'm sending). Otherwise, you might break the layout of the email. As you can see, all styles are inline, so you can simply replace them with your custom styles. 

    You should run the aspx page in the browser once and this will be enough to replace the template.

    I really hope that you'll find this information useful! Regards,
    Jen Peleva
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
2 posts, 0 answered