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

Forums / Developing with Sitefinity / How to Add an Event

How to Add an Event

4 posts, 0 answered
  1. Jason
    Jason avatar
    9 posts
    Registered:
    23 Mar 2009
    02 Sep 2009
    Link to this post
    I looked at the API material but I'm just not understanding how to create a user interface to add an event to the events calendar.  Does anyone have a complete example of how to do this???
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    03 Sep 2009
    Link to this post
    Hi Adisa,

    Below is a sample code that user RadSCheduler as interface for creating, delering and updating appointments.

    using System;  
    using System.Web.UI.WebControls;  
    using Telerik.Events;  
    using Telerik.Events.WebControls;  
    using System.Linq; 
    using System.Collections.Generic; 
    using Telerik.Web.UI; 
    using Telerik.Cms.Web.UI; 
    using System.ComponentModel; 
    using System.Web.UI; 
    using Telerik.Framework.Web; 
    using Telerik.Cms.Engine; 
    using Telerik; 
    using System.Web; 
    using Telerik.Web; 
      
    /// <summary>  
    /// Summary description for MyEventsScheduleView  
    /// </summary>  
    public class CustomEventsScheduleViewControl : CompositeControl 
        #region Properties 
     
        /// <summary> 
        /// Name of the provider 
        /// </summary> 
        [System.ComponentModel.Category("Data")] 
        public string ProviderName 
        { 
            get 
            { 
                if (String.IsNullOrEmpty(providerName)) 
                    providerName = EventsManager.DefaultContentProvider; 
                return providerName; 
            } 
            set 
            { 
                providerName = value; 
            } 
        } 
     
        /// <summary> 
        /// Gets or sets the url to EventsScheduleView template. 
        /// </summary> 
        [WebEditor("Telerik.FileManager.UrlWebEditor, Telerik.FileManager"), 
         System.ComponentModel.Category("Appearance"), 
         System.Web.UI.Themeable(false), 
         DefaultValue("")] 
        public virtual string LayoutTemplatePath 
        { 
            get 
            { 
                object obj = this.ViewState["EventsScheduleViewTemplatePath"]; 
                if (obj == null
                    return "~/Sitefinity/ControlTemplates/Events/EventsScheduleView.ascx"
                return (string)obj; 
                 
            } 
            set 
            { 
                this.ViewState["EventsScheduleViewTemplatePath"] = value; 
            } 
        } 
     
        /// <summary> 
        /// Gets or sets EventsScheduleView template 
        /// </summary> 
        [ 
        Browsable(false), 
        System.Web.UI.PersistenceMode(PersistenceMode.InnerProperty), 
        DefaultValue(typeof(ITemplate), ""), 
        Description("Events Schedule View template"), 
        TemplateContainer(typeof(EventsScheduleView)) 
        ] 
        public virtual ITemplate LayoutTemplate 
        { 
            get 
            { 
                if (layoutTemplate == null
                    layoutTemplate = ControlUtils.GetTemplate<DefaultTemplate>(LayoutTemplatePath); 
                return layoutTemplate; 
            } 
            set 
            { 
                layoutTemplate = value; 
            } 
        } 
     
        /// <summary> 
        /// Url of the page where single event will be displayed. That page should contain 
        /// EventView control. 
        /// </summary> 
        [WebEditor("Telerik.Cms.Web.UI.UrlEditorWrapper, Telerik.Cms"), 
        Category("Behavior")] 
        public virtual string SingleEventUrl 
        { 
            get 
            { 
                object obj = ViewState["SingleEventUrl"]; 
                if (obj != null
                    return (string)obj; 
                return String.Empty; 
            } 
            set 
            { 
                ViewState["SingleEventUrl"] = value; 
            } 
        } 
        #endregion 
        #region Overriden Methods 
     
        /// <summary> 
        /// Overrides the base method and registers ControlDesignerBase control as one whose control state  
        /// must be persisted. 
        /// </summary> 
        /// <param name="e"></param> 
        protected override void OnInit(EventArgs e) 
        { 
            base.OnInit(e); 
            if (base.Page != null
                base.Page.RegisterRequiresControlState(this); 
     
            if (String.IsNullOrEmpty(providerName)) 
                providerName = EventsManager.DefaultContentProvider; 
     
            if (!String.IsNullOrEmpty(providerName)) 
            { 
                if (eventsManager == null || eventsManager.Provider.Name != providerName) 
                    eventsManager = new EventsManager(providerName); 
            } 
        } 
     
        /// <summary> 
        /// Gets the HTML tag for the ListDisplay. 
        /// </summary> 
        protected override HtmlTextWriterTag TagKey 
        { 
            get 
            { 
                return HtmlTextWriterTag.Div; 
            } 
        } 
     
        /// <summary> 
        /// Restores control state information from a previous page request that was saved by the SaveControlState 
        /// method. 
        /// </summary> 
        /// <param name="savedState">Represents the control state to be restored.</param> 
        protected override void LoadControlState(object savedState) 
        { 
            if (savedState != null
            { 
                object[] state = (object[])savedState; 
                providerName = (string)state[0]; 
                from = (DateTime)state[1]; 
                to = (DateTime)state[2]; 
            } 
        } 
     
        /// <summary> 
        /// Saves server control state changes. 
        /// </summary> 
        /// <returns></returns> 
        protected override object SaveControlState() 
        { 
            return new object[] { 
                    providerName, 
                    from, 
                    to 
                }; 
        } 
     
        /// <summary> 
        /// Overriden. Called to populate the child control hierarchy. This is the main 
        /// method to render the control's markup, since it is a CompositeControl and contains 
        /// child controls. 
        /// </summary> 
        protected override void CreateChildControls() 
        { 
            Controls.Clear(); 
     
            if (Page == null || DesignMode) 
            { 
                Label noDesignModeLabel = new Label(); 
                noDesignModeLabel.Text = "The control cannot be previewed in design mode."
                Controls.Add(noDesignModeLabel); 
            } 
            else 
            { 
                container = new EventsScheduleViewContainer(this); 
                LayoutTemplate.InstantiateIn(container); 
     
                ObjectDataSource dataSource = new ObjectDataSource(); 
                dataSource.ID = "EventsDataSource"
                dataSource.TypeName = "Telerik.Events.EventsManager"
     
                dataSource.SelectMethod = "GetEvents"
                dataSource.SelectParameters.Add("from", TypeCode.DateTime, from.ToString()); 
                dataSource.SelectParameters.Add("to", TypeCode.DateTime, to.ToString()); 
                dataSource.SelectParameters.Add("sortExpression", String.Empty); 
                dataSource.SelectParameters.Add("status", ContentStatus.Published.ToString()); 
                dataSource.ObjectCreating += new ObjectDataSourceObjectEventHandler(dataSource_ObjectCreating); 
                dataSource.Selecting += new ObjectDataSourceSelectingEventHandler(dataSource_Selecting); 
                Controls.Add(dataSource); 
     
                container.EventsSchedule.DataSourceID = dataSource.ID; 
                container.EventsSchedule.DataKeyField = "ID"
                container.EventsSchedule.DataSubjectField = "EventTitle"
                container.EventsSchedule.DataStartField = "Start"
                container.EventsSchedule.DataEndField = "End"
                // allow delete, insert and edit. 
                container.EventsSchedule.AllowDelete = true
                container.EventsSchedule.AllowInsert = true
                container.EventsSchedule.AllowEdit = true
                container.EventsSchedule.AppointmentUpdate += new AppointmentUpdateEventHandler(EventsSchedule_AppointmentUpdate); 
                container.EventsSchedule.AppointmentInsert += new AppointmentInsertEventHandler(EventsSchedule_AppointmentInsert); 
                container.EventsSchedule.AppointmentDelete += new AppointmentDeleteEventHandler(EventsSchedule_AppointmentDelete); 
                container.EventsSchedule.AppointmentCreated += new AppointmentCreatedEventHandler(EventsSchedule_AppointmentCreated); 
     
                Controls.Add(container); 
     
     
            } 
        } 
     
        void EventsSchedule_AppointmentDelete(object sender, SchedulerCancelEventArgs e) 
        { 
            //throw new NotImplementedException(); 
        } 
     
        void EventsSchedule_AppointmentInsert(object sender, SchedulerCancelEventArgs e) 
        { 
           var newContent = eventsManager.CreateContent("text/html");  
           newContent.SetMetaData("Title", e.Appointment.Subject.ToString()); 
           
           eventsManager.SaveContent(newContent); 
     
        } 
     
        void EventsSchedule_AppointmentUpdate(object sender, AppointmentUpdateEventArgs e) 
        { 
            IEvent cnt = eventsManager.GetEvent(new Guid(e.Appointment.ID.ToString())); 
            cnt.ContentItem.SetMetaData("Title", e.ModifiedAppointment.Subject); 
             
        } 
        #endregion 
        #region Helper methods 
     
        
        protected virtual string GetItemUrl(IContent contentItem, string singleItemUrl) 
        { 
            string sep = String.Empty; 
            string ret = String.Empty; 
            string url = singleItemUrl; 
            if (String.IsNullOrEmpty(url) && CmsContext.CurrentUrl != null
            { 
                url = CmsContext.CurrentUrl.Path; 
            } 
            else 
            { 
                sep = "&"
     
                ret = String.IsNullOrEmpty(Context.Request.QueryString["ReturnURL"]) ? String.Concat("ReturnURL=", Context.Server.UrlEncode(Context.Request.RawUrl)) : String.Concat("ReturnURL=", Context.Server.UrlEncode(Context.Request.QueryString["ReturnURL"])); 
            } 
            if (String.IsNullOrEmpty(contentItem.Url)) 
            { 
                if (url != null
                { 
                    int idx = url.IndexOf('?'); 
                    if (idx != -1) 
                        url = url.Substring(0, idx); 
                } 
                url = String.Concat(url, "?"
                    ContentManager.ContentProviderKey, "=", contentItem.ProviderName, "&"
                    ContentManager.ContentItemKey, "=", contentItem.ID, sep, ret); 
            } 
            else 
            { 
                string itemUrl = String.Concat(contentItem.Url, eventsManager.Content.Provider.ContentExtension); 
                if (eventsManager.Content.Provider.UrlFormatQueryStringIndex != -1) 
                { 
                    itemUrl += "?" + ContentUrlRewriterService.FormatURL(contentItem, eventsManager.Content.Provider 
                        , ContentUrlRewriterService.FormatOptions.QueryString); 
                } 
                else if (sep.Length > 0) 
                { 
                    sep = "?"
                } 
                if (!itemUrl.StartsWith("~")) 
                { 
                    if (url != null
                    { 
                        int idx = url.LastIndexOf('.'); 
                        if (idx != -1) 
                        { 
                            url = url.Substring(0, idx); 
                        } 
                        else 
                        { 
                            idx = url.IndexOf('?'); 
                            if (idx != -1) 
                                url = url.Substring(0, idx); 
                        } 
                    } 
     
                    itemUrl = String.Concat(VirtualPathUtility.RemoveTrailingSlash(url), itemUrl); 
                } 
                url = String.Concat(itemUrl, sep, ret); 
            } 
            IUrlService svc = UrlServices.GetLanguageService(); 
            if (svc != null
                return svc.ResolveLanguageUrl(url); 
            return url; 
        } 
        #endregion 
        #region Event handlers 
     
        private void dataSource_Selecting(object sender, EventArgs e) 
        { 
            ((ObjectDataSourceView)sender).SelectParameters["from"] = new Parameter("from", TypeCode.DateTime, from.ToString()); 
            ((ObjectDataSourceView)sender).SelectParameters["to"] = new Parameter("to", TypeCode.DateTime, to.ToString()); 
        } 
     
        private void dataSource_ObjectCreating(object sender, ObjectDataSourceEventArgs e) 
        { 
            e.ObjectInstance = eventsManager; 
        } 
     
        private void EventsSchedule_AppointmentCreated(object sender, AppointmentCreatedEventArgs e) 
        { 
            IEvent _event = eventsManager.GetEvent(new Guid(e.Appointment.ID.ToString())); 
            HyperLink eventDetailsLink = (HyperLink)e.Container.FindControl("eventDetailsLink"); 
            if (eventDetailsLink != null
            { 
                eventDetailsLink.Text = e.Appointment.Subject; 
                //show description onmouse over 
                eventDetailsLink.ToolTip = _event.ContentItem.Content.ToString(); 
                eventDetailsLink.NavigateUrl = GetItemUrl(_event.ContentItem, SingleEventUrl); 
            } 
        } 
        #endregion 
        #region Private fields 
     
        private string providerName; 
        private EventsManager eventsManager; 
        private ITemplate layoutTemplate; 
        private EventsScheduleViewContainer container; 
     
        private DateTime from = DateTime.MinValue; 
        private DateTime to = DateTime.MaxValue; 
        #endregion 
        #region Templates 
     
        private class DefaultTemplate : ITemplate 
        { 
            public void InstantiateIn(Control container) 
            { 
                // TODO: implement default template 
            } 
        } 
        #endregion 
        #region Containers 
     
        private class EventsScheduleViewContainer : GenericContainer<CustomEventsScheduleViewControl> 
        { 
            public EventsScheduleViewContainer(CustomEventsScheduleViewControl owner) 
                : base(owner) 
            { 
            } 
     
            public RadScheduler EventsSchedule 
            { 
                get 
                { 
                    if (eventsSchedule == null
                        eventsSchedule = (RadScheduler)FindRequiredControl<Control>("eventsSchedule"); 
                    return eventsSchedule; 
                } 
            } 
     
            private RadScheduler eventsSchedule; 
     
        } 
        #endregion 
     
     


    Note that it is sample code,so some methods should be modified.


    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.
  3. Jason
    Jason avatar
    9 posts
    Registered:
    23 Mar 2009
    03 Sep 2009
    Link to this post
    How is this implemented into Sitefinity?  Is that code put into a control and dropped on the page somewhere?  I still don't understand how it all works together.
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    03 Sep 2009
    Link to this post
    Hi Adisa,

    It is a custom control. You can add it to App_Code folder or compile it to assembly. I suggest that you should take a look at our documentation firstly. This will make the things more clear.

    Developers Manual
    RadScheduler
    MSDN

    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.
Register for webinar
4 posts, 0 answered