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

Forums / Developing with Sitefinity / Deleting a Newsletter Subscriber

Deleting a Newsletter Subscriber

3 posts, 0 answered
  1. Duncan Evans
    Duncan Evans avatar
    122 posts
    Registered:
    07 Jul 2009
    08 Feb 2010
    Link to this post
    01.DataManager dManager = new DataManager();
    02. 
    03.    foreach (ISubscriber subscriber in dManager.GetSubscribers())
    04.    {
    05.        if (CheckEmail(subscriber.Email))
    06.        {
    07.            if (chkResetNames.Checked)
    08.            {
    09.                subscriber.FirstName = char.ToUpper(subscriber.FirstName[0]) + subscriber.FirstName.Substring(1);
    10.                subscriber.LastName = char.ToUpper(subscriber.LastName[0]) + subscriber.LastName.Substring(1);
    11. 
    12.                dManager.SaveSubscriber(subscriber);
    13.            }
    14.        }
    15.        else
    16.        {
    17.            dManager.DeleteSubscriber(subscriber.SubscriberId);
    18.        }

    I am writing a tool that will go throught all my newsletter subscribers and to a check ont he email address etc. If it comes back as being a false email then i want to delete this subscriber from all Subscriber groups.

    the code above makes all my newsletter subscriber group produce the following error:

    Object reference not set to an instance of an object.


    Could someone please help me with some code that will allow me to update/edit as well as delete subscribers and any associations to any groups.

    Thanks,

    Duncan
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    08 Feb 2010
    Link to this post
    Hello Duncan Evans,

    This will delete the subscriber from all groups

    DataManager dataManager = new DataManager ( );
     
    IList gSubs = dataManager.GetSubscriberGroupsOfSubscriber ( subscriberEmail );
    foreach ( IGroupSubscriber gSub in gSubs )
    {
        dataManager.DeleteGroupSubscriberRelation ( gSub );
    }

    For editing subscribers, please check the other thread you opened here.

    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Follow the status of features or bugs in PITS and vote for them to affect their priority.
  3. Duncan Evans
    Duncan Evans avatar
    122 posts
    Registered:
    07 Jul 2009
    08 Feb 2010
    Link to this post
    You the MAN Ivan!

    Guid guid = new Guid(cboSubscriberLists.SelectedValue);
     
    DataManager dManager = new DataManager();
    IList allsubs = dManager.GetSubscribersOfGroup(guid, string.Empty);
     
    try
    {
     
        foreach (IGroupSubscriber sub in allsubs)
        {
            ISubscriber currsub = dManager.GetSubscriber(sub.SubscriberId);
     
            if (chkResetNames.Checked)
            {
                currsub.FirstName = char.ToUpper(currsub.FirstName[0]) + currsub.FirstName.Substring(1).ToLower();
                currsub.LastName = char.ToUpper(currsub.LastName[0]) + currsub.LastName.Substring(1).ToLower();
                dManager.SaveSubscriber(currsub);
            }
     
            if (chkValidateEmail.Checked)
            {
                if (!CheckEmail(currsub.Email))
                {
                    IList gSubs = dManager.GetSubscriberGroupsOfSubscriber(currsub.Email);
                    foreach (IGroupSubscriber gSub in gSubs)
                    {
                        dManager.DeleteGroupSubscriberRelation(gSub);
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        errorString = "Error processing request.";
    }

    Thanks!

    Duncan
Register for webinar
3 posts, 0 answered