Kentico CMS 6.0 Developer's Guide

Managing subscribers

Managing subscribers

Previous topic Next topic Mail us feedback on this topic!  

Managing subscribers

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 subscriber.

 

private bool CreateSubscriber()
{
  // Create new subscriber object
  Subscriber newSubscriber = new Subscriber();

 
  // Set the properties
   newSubscriber.SubscriberFirstName = "Name";
   newSubscriber.SubscriberLastName = "Surname";
   newSubscriber.SubscriberFullName = "Name Surname";
   newSubscriber.SubscriberEmail = "subscriber@localhost.local";
   newSubscriber.SubscriberSiteID = CMSContext.CurrentSiteID;

 
  // Save the subscriber
  SubscriberProvider.SetSubscriber(newSubscriber);

 
  return true;
}

 

The following example gets and updates a subscriber.

 

private bool GetAndUpdateSubscriber()
{
  // Get the subscriber
  Subscriber updateSubscriber = SubscriberProvider.GetSubscriber("subscriber@localhost.local", CMSContext.CurrentSiteID);
  if (updateSubscriber != null)
   {
      // Update the properties
       updateSubscriber.SubscriberFullName = updateSubscriber.SubscriberFullName.ToLower();

 
      // Save the changes
      SubscriberProvider.SetSubscriber(updateSubscriber);

 
      return true;
   }

 
  return false;
}

 

The following example gets and bulk updates subscribers.

 

private bool GetAndBulkUpdateSubscribers()
{
  // Prepare the parameters
  string where = "SubscriberEmail LIKE N'subscriber@localhost.local%'";

 
  // Get the data
  DataSet subscribers = SubscriberProvider.GetSubscribers(where, null);
  if (!DataHelper.DataSourceIsEmpty(subscribers))
   {
      // Loop through the individual items
      foreach (DataRow subscriberDr in subscribers.Tables[0].Rows)
       {
          // Create object from DataRow
          Subscriber modifySubscriber = new Subscriber(subscriberDr);

 
          // Update the properties
           modifySubscriber.SubscriberFullName = modifySubscriber.SubscriberFullName.ToUpper();

 
          // Save the changes
          SubscriberProvider.SetSubscriber(modifySubscriber);
       }

 
      return true;
   }

 
  return false;
}

 

The following example subscribes a subscriber to newsletter.

 

private bool SubscribeToNewsletter()
{
  // Get the subscriber and newsletter
  Subscriber subscriber = SubscriberProvider.GetSubscriber("subscriber@localhost.local", CMSContext.CurrentSiteID);
  Newsletter newsletter = NewsletterProvider.GetNewsletter("MyNewStaticNewsletter", CMSContext.CurrentSiteID);

 
  if ((subscriber != null) && (newsletter != null))
   {
      // Subscribe to 'My new static newsletter'
      SubscriberProvider.Subscribe(subscriber.SubscriberID, newsletter.NewsletterID, DateTime.Now);

 
      return true;
   }

 
  return false;
}

 

The following example unsubscribes a subscriber from newsletter.

 

private bool UnsubscribeFromNewsletter()
{
  // Get the subscriber and newsletter
  Subscriber subscriber = SubscriberProvider.GetSubscriber("subscriber@localhost.local", CMSContext.CurrentSiteID);
  Newsletter newsletter = NewsletterProvider.GetNewsletter("MyNewStaticNewsletter", CMSContext.CurrentSiteID);

 
  if ((subscriber != null) && (newsletter != null))
   {
      // Unubscribe from 'My new static newsletter'
      SubscriberProvider.Unsubscribe(subscriber.SubscriberID, newsletter.NewsletterID);

 
      return true;
   }

 
  return false;
}

 

The following example deletes a subscriber.

 

private bool DeleteSubscriber()
{
  // Get the subscriber
  Subscriber deleteSubscriber = SubscriberProvider.GetSubscriber("subscriber@localhost.local", CMSContext.CurrentSiteID);

 
  // Delete the subscriber
  SubscriberProvider.DeleteSubscriber(deleteSubscriber);

 
  return (deleteSubscriber != null);
}