More in this section

Forums / Developing with Sitefinity / Newsletter Module API Question

Newsletter Module API Question

15 posts, 0 answered
  1. Zack
    Zack avatar
    41 posts
    Registered:
    18 Apr 2007
    02 Apr 2008
    Link to this post
    Is it possible to add a Newsletter Subscriber through the Sitefinity API?
  2. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    07 Apr 2008
    Link to this post
    Hi Zack,

    We have such APIs but they are not documented yet. We are currently working on this part of the documentation.

    Unfortunately, I cannot describe these APIs in this thread, but as soon as we have some documentation, I will send it to you via email.

    I can give you a hint and you could try to implement the rest:

    You have to use Telerik.Notifications.Newsletters namespace with its DataManager class. It is like the other Managers for managing content.
    The interfaces you need are ISubscriber and the IGroupNewsletter.

    I'm sorry for not being able to help you much. I will inform you right away once we have some detailed documentation.

    Regards,
    Georgi
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. Chris Williams
    Chris Williams avatar
    14 posts
    Registered:
    25 Oct 2004
    15 Jul 2008
    Link to this post
    I'd really like to see this code as well.

    I think I'm part of the way there, but my code fails when I try to add a new ISubscriber to the IList of subscribers.

    I get a failure that the subscriber is not of type "GroupSubscriber"... but it's not clear how to create a GroupSubscriber.

    DataManager dataManager = new DataManager();

    IGroup companyNewsLetter = dataManager.GetGroup(new Guid("69571bcb-201e-4443-982a-2392a44c8a31"));

    IList subscribers = companyNewsLetter.Subscribers;

    ISubscriber newSubscriber = dataManager.CreateSubscriber("testuser@mac.com");

    subscribers.Add(newSubscriber);

  4. Sonya
    Sonya avatar
    231 posts
    Registered:
    24 Sep 2012
    18 Jul 2008
    Link to this post
    Hi Chris Williams,

    To achive this goal follow steps below:

    The code should run under the context of the website (a Sitefinity project). Use the following functions:
    /// <summary> 
    /// Class main function 
    /// </summary> 
    /// <param name="args"></param> 
    static void Main ( string[ ] args ) 
        // Subscriber email 
        string email = "sample@telerik.com"
        // First name 
        string fname = "first Name"
        // Last name 
        string lname = "last Name"
        // Group name ( Which group the subscriber will subscribe in ) 
        string groupName = "sampleGroup"
        
        // Create object of newsletter data manager 
        DataManager manager = new DataManager ( "Newsletters" ); 
        // Get groupId from the group name. 
        Guid groupId = GetGroupName ( manager, groupName ); 
        // Check wheather the returned value is empty ( means no group with the specific name found ). 
        if ( groupId != Guid.Empty ) 
            // Start subscriber subscroption process 
            RegisterNewEmail ( manager, email, fname, lname, GetGroupName ( manager, groupName ) ); 
        else 
            // handle the message 
            Console.WriteLine ( "Group {0} not found", groupName ); 
     
    /// <summary> 
    /// Iterate over the existing groups and catch the group Id with the same group name 
    /// </summary> 
    /// <param name="manager"> newsletter data manager </param> 
    /// <param name="groupName"> group name </param> 
    /// <returns> group id </returns> 
    private static Guid GetGroupName ( DataManager manager, string groupName ) 
        IList groups = manager.GetGroups ( string.Empty ); 
        foreach ( IGroup group in groups ) 
        { 
            if ( group.GroupName == groupName ) 
                return group.GroupId; 
        } 
        return Guid.Empty; 
     
    /// <summary> 
    /// Registering a subscriber at specific group 
    /// </summary> 
    /// <param name="manager"> newsletter data manager </param> 
    /// <param name="email"> subscriber email </param> 
    /// <param name="fname"> subscriber first name </param> 
    /// <param name="lname"> subscriber last name </param> 
    /// <param name="groupId"> group id </param> 

    This the part which you can add a GroupSubscriber to your group:
    private static void RegisterNewEmail ( DataManager manager, string email, string fname, string lname, Guid groupId ) 
        // Get the subscriber with the following email. if exist skip the creation of new subscriber. 
        // this case may be happened if that subscriber is subscriping at many groups. 
        ISubscriber newSub = manager.GetSubscriber ( email ); 
        if ( newSub == null ) 
        { 
            // Create new subscriber object 
            newSub = manager.CreateSubscriber ( email ); 
            newSub.FirstName = fname; 
            newSub.LastName = lname; 
            // Save the object. 
            manager.SaveSubscriber ( newSub ); 
        } 
     
        // Check if the current subscriber is already subscriped at the following group. 
        IGroupSubscriber gSub = manager.GetGroupSubscriberRelation ( groupId, newSub.SubscriberId ); 
        if ( gSub == null ) 
        { 
            // Create new object of group subscriber ( Considered as a bridge between the group object 
            //  and the subscriber object. 
            gSub = manager.CreateGroupSubscriberRelation ( groupId, newSub.SubscriberId ); 
            // Save the current object 
            manager.SaveGroupSubscribers ( gSub ); 
        } 

    Let us know if you need further assistance.

    Kind regards,
    Sonya
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  5. Brook
    Brook avatar
    39 posts
    Registered:
    21 Mar 2007
    13 Jan 2009
    Link to this post
    I am so happy I found this example, these kind of examples great to have in your developers guide. 

    Thanks!
  6. Peter
    Peter avatar
    10 posts
    Registered:
    24 Aug 2008
    12 Apr 2009
    Link to this post
    Awesome! Just what I was looking for. Thanks guys..

    /Peter
  7. TIOS
    TIOS avatar
    48 posts
    Registered:
    22 Mar 2006
    25 Aug 2009
    Link to this post
    Just incase people are having trouble what namespaces the sample code uses its:

    Imports Telerik.Newsletters
    Imports Telerik.Notifications.Newsletters

    or

    using Telerk.Newsletters;
    using Teleriking.Notifications.Newsletters
  8. Gabriel
    Gabriel avatar
    7 posts
    Registered:
    18 Jul 2008
    19 Oct 2009
    Link to this post
    Hi there.

    I´ve been trying to unsubscrive a Subscriber from a Group with the API sitefinity.

    I´ve already can subscrive, here is the code:

     

    protected void lnkActivateSubscription_OnClick(object sender, EventArgs e)

     

    {

     

    MembershipUser myuser = Membership.Provider.GetUser(ClienteCurrent.Instance.CurrentClient.UsernameSitefinity, false);

     

     

    if (myuser.UserName != null && myuser.Email != null)

     

    {

     

    DataManager dManager = new DataManager();

     

     

    ISubscriber NewSubscriber = dManager.CreateSubscriber(myuser.Email);

     

    dManager.SaveSubscriber(NewSubscriber);

     

    IList listofGroups = dManager.GetGroups("GroupName ASC");

     

     

    foreach (IGroup singleG in listofGroups)

     

    {

     

    if (singleG.GroupName == "Newsletter")

     

    {

     

    Guid groupGuid = new Guid(singleG.GroupId.ToString());

     

     

    // Check if the current subscriber is already subscriped at the following group.

     

     

    IGroupSubscriber gSub = dManager.GetGroupSubscriberRelation(groupGuid, NewSubscriber.SubscriberId);

     

     

    if (gSub == null)

     

    {

     

    // Create new object of group subscriber ( Considered as a bridge between the group object

     

     

    // and the subscriber object.

     

    gSub = dManager.CreateGroupSubscriberRelation(groupGuid, NewSubscriber.SubscriberId);

     

    // Save the current object

     

    dManager.SaveGroupSubscribers(gSub);

    lnkActivateSubscription.Visible =

    false;

     

    lnkDesActivateSubscription.Visible =

    true;

     

    }

    }

    }

    }

     

    }



    The unsubscrive method that I´ve implemented his this:

     

    protected void lnkDesActivateSubscription_OnClick(object sender, EventArgs e)

     

    {

     

    MembershipUser myuser = Membership.Provider.GetUser(ClienteCurrent.Instance.CurrentClient.UsernameSitefinity, false);

     

     

    if (myuser.UserName != null && myuser.Email != null)

     

    {

     

    DataManager dManager = new DataManager();

     

     

    ISubscriber Subscriber = dManager.GetSubscriber(myuser.Email);

     

     

    //dManager.SaveSubscriber(NewSubscriber);

     

     

    IList listofGroups = dManager.GetGroups("GroupName ASC");

     

     

    foreach (IGroup singleG in listofGroups)

     

    {

     

    if (singleG.GroupName == "Newsletter")

     

    {

     

    Guid groupGuid = new Guid(singleG.GroupId.ToString());

     

     

    //foreach (GroupSubscriber groupSubscriver in singleG.Subscribers)

     

     

    //{

     

     

    // Check if the current subscriber is already subscriped at the following group.

     

     

    IGroupSubscriber gSub = dManager.GetGroupSubscriberRelation(groupGuid, Subscriber.SubscriberId);

     

     

    if (gSub != null)

     

    {

     

    // Delete object of group subscriber ( Considered as a bridge between the group object

     

     

    // and the subscriber object.

     

    dManager.DeleteGroupSubscriberRelation(singleG.GroupId, Subscriber.SubscriberId);

     

    // Save the current object

     

    dManager.SaveGroupSubscribers(gSub);

    lnkActivateSubscription.Visible =

    true;

     

    lnkDesActivateSubscription.Visible =

    false;

     

    }

     

    //}

     

    }

    }

    }

    }



    The problem is that when I call the GetGroupSubscriberRelation method it´s always returning null to IGroupSubscriber gSub.

    I don´t understand, it should be exactly the same thing has the subscrive method but reversed, but it´s not working.
    Could anybody give me some help.

    Thanks in advanced.

  9. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    19 Oct 2009
    Link to this post
    Hi Gabriel,

    Try using the following code to get the subscriber groups:

    IList subscriberGroups = dManager.GetSubscriberGroupsOfSubscriber ( subscriberEmail );
           foreach ( IGroupSubscriber gSub in subscriberGroups )
           {
               dataManager.DeleteGroupSubscriberRelation ( gSub );
           }

    Make sure that the groupGuid and SubscriberId are not null and there is a subscribed user in a group with name "Newsletter" as you have specified in your code.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  10. Gabriel
    Gabriel avatar
    7 posts
    Registered:
    18 Jul 2008
    20 Oct 2009
    Link to this post
    Hi Ivan.

    Thanks for the reply.

    Let me clear up some things.


    The unsubscrive method that I´ve implemented his this:

     

    protected void lnkDesActivateSubscription_OnClick(object sender, EventArgs e)

     

    {

     

     

    MembershipUser myuser = Membership.Provider.GetUser(ClienteCurrent.Instance.CurrentClient.UsernameSitefinity, false);

     

     

     

    if (myuser.UserName != null && myuser.Email != null)

     

    {

     

     

    DataManager dManager = new DataManager();

     

     

     

    ISubscriber Subscriber = dManager.GetSubscriber(myuser.Email);

     

     

     

    //dManager.SaveSubscriber(NewSubscriber);

     

     

     

    IList listofGroups = dManager.GetGroups("GroupName ASC");

     

     

     

    foreach (IGroup singleG in listofGroups)

     

    {

     

     

    if (singleG.GroupName == "Newsletter")

     

    {

     

     

    Guid groupGuid = new Guid(singleG.GroupId.ToString());

     

     

     

    //foreach (GroupSubscriber groupSubscriver in singleG.Subscribers)

     

     

     

    //{

     

     

     

    // Check if the current subscriber is already subscriped at the following group.

     

     

     

    IGroupSubscriber gSub = dManager.GetGroupSubscriberRelation(groupGuid, Subscriber.SubscriberId);

     

     

     

    if (gSub != null)

     

    {

     

     

    // Delete object of group subscriber ( Considered as a bridge between the group object

     

     

     

    // and the subscriber object.

     

     

    dManager.DeleteGroupSubscriberRelation(singleG.GroupId, Subscriber.SubscriberId);

     

    // Save the current object

     

     

    dManager.SaveGroupSubscribers(gSub);

    lnkActivateSubscription.Visible =

    true;

     

    lnkDesActivateSubscription.Visible =

     

    false;

     

    }

     

     

    //}

     

     

    }

    }

    }

    }



    Just to clear things out, yes I can add a subscriber with my method lnkActivateSubscription_OnClick.

    Yes it's created a subscriber in the groupSubscriber that I creatred named  Newsletter.

    No, the value of the groupGuid and SubscriberID is not null, how do I know that?
     
    Because, when I do:

    if (singleG.GroupName == "Newsletter")

     

    {

     

     

    Guid groupGuid = new Guid(singleG.GroupId.ToString());

    }

    I get the Guid ID of that group, and when I do

    ISubscriber Subscriber = dManager.GetSubscriber(myuser.Email);

    it returns me the Object Subscriber with is Guid ID.

    Your example doesn´t work for me, because I will have different Groups where that subscriber can be associated, I just want to unsubscribe the subscriver from a specific group, in this case, Newsletter.

     

    Heather way I tried your example, with some differences to my needs:

     

    IList subscriberGroups = dManager.GetSubscriberGroupsOfSubscriber ( subscriberEmail );
           foreach ( IGroupSubscriber gSub in subscriberGroups )
           {
                    if(gsub.GroupName == "Newsletter"
                    {
                   dataManager.DeleteGroupSubscriberRelation ( gSub );
                    } 
           }

    But

    IList subscriberGroups = dManager.GetSubscriberGroupsOfSubscriber ( subscriberEmail );

    is returning the value 0.

    And I´m sure that the email that is being passed is the email of a subscriber that exists in the Newsletter GroupSubscriber

    Is this a bug in the API of sitefinity?

    This should be so simple has the subscribe method, don´t understand what am I doing wrong?

    Thanks in advanced.



  11. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    21 Oct 2009
    Link to this post
    Hello Gabriel,

    I tried the following code and the user that was previously added to my Newsletters group was removed.

    MembershipUser myuser = Membership.Provider.GetUser("nl", false);
            if (myuser.UserName != null && myuser.Email != null)
            {
                DataManager dManager = new DataManager();
                ISubscriber Subscriber = dManager.GetSubscriber(myuser.Email);
                //dManager.SaveSubscriber(NewSubscriber);
                IList listofGroups = dManager.GetGroups("GroupName ASC");
                foreach (IGroup singleG in listofGroups)
                {
                    if (singleG.GroupName == "Newsletter")
                    {
     
                        Guid groupGuid = new Guid(singleG.GroupId.ToString());
                        IGroupSubscriber gSub = dManager.GetGroupSubscriberRelation(groupGuid, Subscriber.SubscriberId);
                       if (gSub != null)
                       {
     
                            dManager.DeleteGroupSubscriberRelation(singleG.GroupId, Subscriber.SubscriberId);
     
     
                        }
                    }
                }
            }

    If the subscriber exists in the Newsletters group it should be removed  using the code above.

    All the best,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  12. Gabriel
    Gabriel avatar
    7 posts
    Registered:
    18 Jul 2008
    22 Oct 2009
    Link to this post
    Hi Ivan.
    Thanks for your help.
    But the problem is just half resolved.
    The problem is that if the subscriber is a CMS user in fact with that method he is unsubscribed from that group.

    The problem is that in the moment I only can do that from the admin backoffice.

    If i try to do that whit my activatresubscription method the subscriber is created but is never associated to the user of the sitefinty cms.

    How can I do that.

    I´ve already tried to put

    ISubscriber

     

    newSubscriber = dManager.CreateSubscriber(myuser.Email);

     

    newSubscriber.IsCms =

    true;

     

    dManager.SaveSubscriber(newSubscriber);

    but it didn´t worked.

    Do you have any clue?

    Thanks in advanced.

  13. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    23 Oct 2009
    Link to this post
    Hello Gabriel,

    You can use the code below to create the relation:

    var newsletterManager = new DataManager();
    ProfileBase profile = Profile.GetProfile("someCMSUser");
    MembershipUser user = Membership.GetUser(profile.UserName);
    ISubscriber subscriber = newsletterManager.CreateSubscriber(user.Email);
    if (profile != null)
    {
        string fName = profile.GetPropertyValue("FirstName").ToString();
        if (!string.IsNullOrEmpty(fName))
            subscriber.FirstName = fName;
        string lName = profile.GetPropertyValue("LastName").ToString();
        if (!string.IsNullOrEmpty(lName))
            subscriber.LastName = lName;
    }
     subscriber.IsCms = true;
     newsletterManager.SaveSubscriber(subscriber);

    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  14. Venkatesh Alagu
    Venkatesh Alagu avatar
    42 posts
    Registered:
    22 Nov 2009
    07 Jul 2010
    Link to this post
    hi,

    I saw this forum.. thanks for ur replies. but i dont know which file to edit to create our own API for newsletter subscription. can u pls help me in this issue as soon as possible??

    Is there any other way to create custom subscription form and we need to blast html newsletter from sitefinity??

    Thanks for all ur supports.


    Regards,

    Venkatesh
  15. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    07 Jul 2010
    Link to this post
    Hi venkatesh alagu,

    The built-in template is located under Sitefinity\ControlTemplates\Newsletters\SubscriptionFormTemplate.ascx

    You could create your own control using the API or standard ASP.NET methods like SmtpClient, MailMessage etc. and just drop the control on a page as you are doing with the built-in control.

    Kind regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Register for webinar
15 posts, 0 answered