Kentico CMS 6.0 Developer's Guide

Managing newsletters

Managing newsletters

Previous topic Next topic Mail us feedback on this topic!  

Managing newsletters

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

Arrow


API examples for newer versions


Please visit the latest API Examples documentation to view API examples for newer versions of Kentico.



The following example creates a static newsletter.

 

private bool CreateStaticNewsletter()
{
  EmailTemplate subscriptionTemplate = EmailTemplateProvider.GetEmailTemplate("MyNewSubscriptionTemplate", CMSContext.CurrentSiteID);
  EmailTemplate unsubscriptionTemplate = EmailTemplateProvider.GetEmailTemplate("MyNewUnsubscriptionTemplate", CMSContext.CurrentSiteID);
  EmailTemplate myNewIssueTemplate = EmailTemplateProvider.GetEmailTemplate("MyNewIssueTemplate", CMSContext.CurrentSiteID);

 
  if ((subscriptionTemplate != null) && (unsubscriptionTemplate != null) && (myNewIssueTemplate != null))
   {
      // Create new static newsletter object
      Newsletter newNewsletter = new Newsletter();

 
      // Set the properties
       newNewsletter.NewsletterDisplayName = "My new static newsletter";
       newNewsletter.NewsletterName = "MyNewStaticNewsletter";
       newNewsletter.NewsletterType = NewsletterType.TemplateBased;
       newNewsletter.NewsletterSubscriptionTemplateID = subscriptionTemplate.TemplateID;
       newNewsletter.NewsletterUnsubscriptionTemplateID = unsubscriptionTemplate.TemplateID;
       newNewsletter.NewsletterTemplateID = myNewIssueTemplate.TemplateID;
       newNewsletter.NewsletterSenderName = "Sender name";
       newNewsletter.NewsletterSenderEmail = "sender@localhost.local";
       newNewsletter.NewsletterSiteID = CMSContext.CurrentSiteID;

 
      // Save the static newsletter
      NewsletterProvider.SetNewsletter(newNewsletter);

 
      return true;
   }

 
  return false;
}

 

The following example gets and updates a static newsletter.

 

private bool GetAndUpdateStaticNewsletter()
{
  // Get the static newsletter
  Newsletter updateNewsletter = NewsletterProvider.GetNewsletter("MyNewStaticNewsletter", CMSContext.CurrentSiteID);
  if (updateNewsletter != null)
   {
      // Update the properties
       updateNewsletter.NewsletterDisplayName = updateNewsletter.NewsletterDisplayName.ToLower();

 
      // Save the changes
      NewsletterProvider.SetNewsletter(updateNewsletter);

 
      return true;
   }

 
  return false;
}

 

The following example gets and bulk updates static newsletters.

 

private bool GetAndBulkUpdateStaticNewsletters()
{
  // Prepare the parameters
  string where = "NewsletterName LIKE N'MyNewStaticNewsletter%'";

 
  // Get the data
  DataSet newsletters = NewsletterProvider.GetNewsletters(where, null, 0, null);
  if (!DataHelper.DataSourceIsEmpty(newsletters))
   {
      // Loop through the individual items
      foreach (DataRow newsletterDr in newsletters.Tables[0].Rows)
       {
          // Create object from DataRow
          Newsletter modifyNewsletter = new Newsletter(newsletterDr);

 
          // Update the properties
           modifyNewsletter.NewsletterDisplayName = modifyNewsletter.NewsletterDisplayName.ToUpper();

 
          // Save the changes
          NewsletterProvider.SetNewsletter(modifyNewsletter);
       }

 
      return true;
   }

 
  return false;
}

 

The following example creates a dynamic newsletter.

 

private bool CreateDynamicNewsletter()

{

  EmailTemplate subscriptionTemplate = EmailTemplateProvider.GetEmailTemplate("MyNewSubscriptionTemplate", CMSContext.CurrentSiteID);

  EmailTemplate unsubscriptionTemplate = EmailTemplateProvider.GetEmailTemplate("MyNewUnsubscriptionTemplate", CMSContext.CurrentSiteID);

 

  if ((subscriptionTemplate != null) && (unsubscriptionTemplate != null))

   {

      // Create new dynamic newsletter object

      Newsletter newNewsletter = new Newsletter();

 
      // Set the properties
       newNewsletter.NewsletterDisplayName = "My new dynamic newsletter";
       newNewsletter.NewsletterName = "MyNewDynamicNewsletter";
       newNewsletter.NewsletterType = NewsletterType.Dynamic;
       newNewsletter.NewsletterSubscriptionTemplateID = subscriptionTemplate.TemplateID;
       newNewsletter.NewsletterUnsubscriptionTemplateID = unsubscriptionTemplate.TemplateID;
       newNewsletter.NewsletterSenderName = "Sender name";
       newNewsletter.NewsletterSenderEmail = "sender@localhost.local";
       newNewsletter.NewsletterDynamicURL = "http://www.google.com";
       newNewsletter.NewsletterDynamicSubject = "My new dynamic issue";
       newNewsletter.NewsletterSiteID = CMSContext.CurrentSiteID;

 
      // Save the dynamic newsletter
      NewsletterProvider.SetNewsletter(newNewsletter);

 
      return true;
   }

 
  return false;
}

 

The following example gets and updates a dynamic newsletter.

 

private bool GetAndUpdateDynamicNewsletter()
{
  // Get the dynamic newsletter
  Newsletter updateNewsletter = NewsletterProvider.GetNewsletter("MyNewDynamicNewsletter", CMSContext.CurrentSiteID);
  if (updateNewsletter != null)
   {
      // Update the properties
       updateNewsletter.NewsletterDisplayName = updateNewsletter.NewsletterDisplayName.ToLower();

 
      // Save the changes
      NewsletterProvider.SetNewsletter(updateNewsletter);

 
      return true;
   }

 
  return false;
}

 

The following example gets and bulk updates dynamic newsletters.

 

private bool GetAndBulkUpdateDynamicNewsletters()
{
  // Prepare the parameters
  string where = "NewsletterName LIKE N'MyNewDynamicNewsletter%'";

 
  // Get the data
  DataSet newsletters = NewsletterProvider.GetNewsletters(where, null, 0, null);
  if (!DataHelper.DataSourceIsEmpty(newsletters))
   {
      // Loop through the individual items
      foreach (DataRow newsletterDr in newsletters.Tables[0].Rows)
       {
          // Create object from DataRow
          Newsletter modifyNewsletter = new Newsletter(newsletterDr);

 
          // Update the properties
           modifyNewsletter.NewsletterDisplayName = modifyNewsletter.NewsletterDisplayName.ToUpper();

 
          // Save the changes
          NewsletterProvider.SetNewsletter(modifyNewsletter);
       }

 
      return true;
   }

 
  return false;
}

 

The following example deletes a dynamic newsletter.

 

private bool DeleteDynamicNewsletter()
{
  // Get the dynamic newsletter
  Newsletter deleteNewsletter = NewsletterProvider.GetNewsletter("MyNewDynamicNewsletter", CMSContext.CurrentSiteID);

 
  // Delete the dynamic newsletter
  NewsletterProvider.DeleteNewsletter(deleteNewsletter);

 
  return (deleteNewsletter != null);

}

 

The following example deletes a static newsletter.

 

private bool DeleteStaticNewsletter()
{
  // Get the static newsletter
  Newsletter deleteNewsletter = NewsletterProvider.GetNewsletter("MyNewStaticNewsletter", CMSContext.CurrentSiteID);

 
  // Delete the static newsletter
  NewsletterProvider.DeleteNewsletter(deleteNewsletter);

 
  return (deleteNewsletter != null);
}