Try our conversational search powered by Generative AI!

Subscription functionality

Vote:
 

Hello,

I'm new to episerver, and i'm apologising if i'm asking for something obvious.
I'm trying to build subscription fiction, and i found some examples but seams that that is for older versions of episerver. I want to create subscription page from code where user can add email and choose pages on which. I found that this check box list is posible to create with subscription list control and all pages that are below this root pare will be in that list.
So is it posible to do this from code, and which type should be the property for subscription list?

Thanks

#89274
Aug 12, 2014 15:00
Vote:
 

Hi Uros,

I did a subscription function for 7.5 not long ago. In my case the subscription form was added to the specific pagetype. But in your case I would have added a bool property to show that the page can be subscribed to and then list all pages with it set to true on your subscription page.

I used the DDS to save the subscriptions and then had a scheduled job that runs ones a day and checkes the store to see if any pages have updated and sends the email. You could ofcourse hook on the page published event instead run the email sending code then and there.

Here is the code for the DDSSubscriptionStore

 /// 
    /// Class used for storing the Email Subscriptions in the Dynamic Data Store (DDS)
    /// 
    [EPiServerDataStore(StoreName = "EmailSubscriptionStore", AutomaticallyCreateStore = true)]
    public class EmailSubscription : IDynamicData
    {
        /// 
        /// Gets or sets the ID of the subscription
        /// 
        public Identity Id { get; set; }
        
        /// 
        /// Gets or sets the time when the subscription was created or updated
        /// 
        public DateTime Time { get; set; }

        /// 
        /// Gets or sets the subscription page's ID
        /// 
        public int PageID { get; set; }

        /// 
        /// Gets or sets the email of the person who subscribed
        /// 
        public string Email { get; set; }

        /// 
        /// Gets or sets the flag if the subscription is verifed
        /// 
        public bool IsVerified { get; set; }

        /// 
        /// Parameter-less constructor required for the dynamic data store
        /// 
        public EmailSubscription()
        {
            Initialize();
        }

        /// 
        /// Constructor with params
        /// 
        /// The id of the page subscribed to
        /// The Email of the subscriber
        public EmailSubscription(int pageId, string email)
        {
            Initialize();
            PageID = pageId;
            Email = email;
        }

        /// 
        /// Gets a subscription for the id
        /// 
        /// Id to fetch
        /// A EmailSubscription or null
        public static EmailSubscription GetSubscription(Identity id)
        {
            var store = typeof(EmailSubscription).GetStore();
            return store.Load(id);
        }

        /// 
        /// Get all subscriptions for a specific page
        /// 
        /// The contnent to list from
        /// List of EmailSubscriptions
        public static List GetSubscriptions(ContentReference contentLink)
        {
            var store = typeof(EmailSubscription).GetStore();
            
            var subscriptions = store.Items().Where(s => s.PageID == contentLink.ID);
            
            if (subscriptions == null)
                return new List();

            return subscriptions.ToList();
        }

        /// 
        /// Delete the subscription permanently
        /// 
        /// The subscription to delete
        public static void Delete(EmailSubscription subscription)
        {
            var store = typeof(EmailSubscription).GetStore();
            store.Delete(subscription.Id);
        }

        /// 
        /// Delete all comments for a specific page
        /// 
        /// The content to delete from
        public static void ClearSubscriptions(ContentReference contentLink)
        {
            foreach (var subscription in GetSubscriptions(contentLink))
            {
                Delete(subscription);
            }
        }
        
        /// 
        /// Save the subscription
        /// 
        public void Save()
        {
            var store = typeof(EmailSubscription).GetStore();
            store.Save(this);
        }

        /// 
        /// Sets the IsVerified to true for the subscription
        /// 
        public void Verify()
        {
            var store = typeof(EmailSubscription).GetStore();
            var loadedSub = store.Load(this.Id);
            loadedSub.IsVerified = true;
            store.Save(loadedSub);
        }

        /// 
        /// Initializion method for the class
        /// 
        protected void Initialize()
        {
            Id = Identity.NewIdentity(Guid.NewGuid());
            Time = DateTime.Now;
            Email = string.Empty;
        }
    }



#89281
Aug 12, 2014 15:39
Vote:
 

Hi,
I have finished subscription functionality. I used DDS as you suggested , but i created generic repository for working with DDS. This allowed me to have loosely coupled class's where DDS's are used and for which i can write test's.

public interface IRepository
    {
        void Save(T entity);
        void Delete(T entity);
        IEnumerable GetAll();
        T GetById(Identity id);
        IQueryable GetByQuery(Expression> predicate);
    }
public class Repository : IRepository where T : class
    {
        private readonly DynamicDataStore _dataStore;

        public Repository()
        {
            _dataStore = typeof (T).GetStore();
        }

        public void Save(T entity)
        {            
            _dataStore.Save(entity);
        }

        public void Delete(T entity)
        {
            _dataStore.Delete(entity);
        }

        public IEnumerable GetAll()
        {
            return _dataStore.LoadAll();
        }

        public T GetById(Identity id)
        {
            return _dataStore.Load(id);
        }

        public IQueryable GetByQuery(Expression> predicate)
        {
            return _dataStore.Items().Where(predicate);
        }
    }



Thank you againt for you help!

#90535
Edited, Sep 11, 2014 15:36
Vote:
 

Cool, thanks for sharing!

#90536
Sep 11, 2014 16:01
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.