Delete message templates

Using the Native API, you can delete one or all message templates at once.

Delete a single message template

To delete a specific message template, you use the NewslettersManager class. First, you initialize the NewslettersManager. Then, you check whether an item with the specified ID exists. Finally, you delete the message template by calling DeleteMessageBody and save the changes.

The following code deletes a message template with specific ID with the Native API:

  1. C#
     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Text;
     using System.Threading.Tasks;
     using Telerik.Sitefinity.Modules.Newsletters;
     
     namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.EmailCampaigns.MessageBodies
     {
         public partial class EmailCampaignsSnippets
         {
             public void DeleteMessageBody(Guid id)
             {
                 NewslettersManager manager = NewslettersManager.GetManager();
     
                 if (manager.GetMessageBodies().Where(b => b.Id == id).Count() > 0)
                 {
                     manager.DeleteMessageBody(id);
                     manager.SaveChanges();
                 }
             }
         }
     }
     

Delete all message templates

To delete all message templates, you use the NewslettersManager class. First, you initialize the NewslettersManager. Then, you get all the available message templates. Then, you iterate the collection deleting the message templates one by one. Finally, you save the changes.

The following code deletes all available message templates with the Native API:

  1. C#
     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Text;
     using System.Threading.Tasks;
     using Telerik.Sitefinity.Modules.Newsletters;
     using Telerik.Sitefinity.Newsletters.Model;
     
     namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.EmailCampaigns.MessageBodies
     {
         public partial class EmailCampaignsSnippets
         {
             public void DeleteMessageBodies()
             {
                 NewslettersManager manager = NewslettersManager.GetManager();
     
                 List<MessageBody> messageBodies = manager.GetMessageBodies().ToList();
     
                 foreach (var messageBody in messageBodies)
                 {
                     manager.DeleteMessageBody(messageBody);
                 }
     
                 manager.SaveChanges();
             }
         }
     }
     
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.