Kentico CMS 7.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()
{
  // Creates a new subscriber object
  SubscriberInfo newSubscriber = new SubscriberInfo();

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

 
  // Saves the subscriber
  SubscriberInfoProvider.SetSubscriberInfo(newSubscriber);

 
  return true;
}

 

The following example gets and updates a subscriber.

 

private bool GetAndUpdateSubscriber()
{
  // Gets the subscriber
  SubscriberInfo updateSubscriber = SubscriberInfoProvider.GetSubscriberInfo("subscriber@localhost.local", CMSContext.CurrentSiteID);
  if (updateSubscriber != null)
   {
      // Updates the properties
       updateSubscriber.SubscriberFullName = updateSubscriber.SubscriberFullName.ToLower();

 
      // Saves the changes
      SubscriberInfoProvider.SetSubscriberInfo(updateSubscriber);

 
      return true;
   }

 
  return false;
}

 

The following example gets and bulk updates subscribers.

 

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

 
  // Gets the data
  DataSet subscribers = SubscriberInfoProvider.GetSubscribers(where, null);
  if (!DataHelper.DataSourceIsEmpty(subscribers))
   {
      // Loops through the items
      foreach (DataRow subscriberDr in subscribers.Tables[0].Rows)
       {
          // Creates an object from the DataRow
          SubscriberInfo modifySubscriber = new SubscriberInfo(subscriberDr);

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

 
          // Saves the changes
          SubscriberInfoProvider.SetSubscriberInfo(modifySubscriber);
       }

 
      return true;
   }

 
  return false;
}

 

The following example subscribes a subscriber to newsletter.

 

private bool SubscribeToNewsletter()
{
  // Gets the subscriber and newsletter
  SubscriberInfo subscriber = SubscriberInfoProvider.GetSubscriberInfo("subscriber@localhost.local", CMSContext.CurrentSiteID);
  NewsletterInfo newsletter = NewsletterInfoProvider.GetNewsletterInfo("MyNewStaticNewsletter", CMSContext.CurrentSiteID);

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

 
      return true;
   }

 
  return false;
}

 

The following example unsubscribes a subscriber from newsletter.

 

private bool UnsubscribeFromNewsletter()
{
  // Gets the subscriber and newsletter
  SubscriberInfo subscriber = SubscriberInfoProvider.GetSubscriberInfo("subscriber@localhost.local", CMSContext.CurrentSiteID);
  NewsletterInfo newsletter = NewsletterInfoProvider.GetNewsletterInfo("MyNewStaticNewsletter", CMSContext.CurrentSiteID);

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

 
      return true;
   }

 
  return false;
}

 

The following example deletes a subscriber.

 

private bool DeleteSubscriber()
{
  // Gets the subscriber
  SubscriberInfo deleteSubscriber = SubscriberInfoProvider.GetSubscriberInfo("subscriber@localhost.local", CMSContext.CurrentSiteID);

 
  // Deletes the subscriber
  SubscriberInfoProvider.DeleteSubscriberInfo(deleteSubscriber);

 
  return (deleteSubscriber != null);
}