How To

10 Oct 201724 minutes to read

Validate the Custom Appointment Window Fields

The client-side validation of the fields present within the custom appointment window can be handled before submitting it, by adding appropriate validation classes to each field.

Refer the steps here and create a sample for Custom Appointment window, before proceeding with the following validations.

In the custom appointment window sample, create an additional CSS class validation as mentioned below to add it to the appropriate fields, if the validation of such fields fails.

  • HTML
  • <style> 
        .validation {
            border-color: red;
        }
    </style>

    The sample contains the fields like Subject, Description, StartTime and EndTime which needs to be validated at client-side. To do so, add the required validation code within the script section as follows.

  • JAVASCRIPT
  • // To Validate the Subject field.
    $("#subject").focusout(function() {
        if ($.trim($("#subject").val()) == "") {
            $("#subject").addClass("validation");
            return false;
        }
    });
    
    // To Validate the Description field.
    $("#customDescription").focusout(function() {
        if ($.trim($("#customDescription").val()) == "") {
            $("#customDescription").addClass("validation");
            return false;
        }
    });
    
    // To Validate the Time duration of the appointments
    $("#EndTime").focusout(function() {
        if (new Date($("#EndTime").val()).getDate() >= new Date($("#StartTime").val()).getDate()) {
            if (new Date($("#StartTime").val()).getTime() >= new Date($("#EndTime").val()).getTime())
                alert("EndTime value is lesser than the StartTime value");
        }
    });

    Now, after adding the above validations – whenever the fields within the custom appointment window are skipped without filling any information, it will be notified to the users appropriately.

    Highlight Different Work Hours for Each Resources

    By default, the work hours of the Scheduler is highlighted based on the start and end values provided within the workHours object. It remains same for all the resources, when the Scheduler is rendered with multiple resources. To customize this behavior so as to highlight different work hours range for each of the resources, the following workaround can be utilized by making use of the Scheduler events create and actionComplete.

    Initially, set the highlight as false for the workHours, so as to disable the highlighting of default work hour range.

  • HTML
  • <!--Container for ejScheduler widget-->
    <div id="Schedule1"></div>
  • JAVASCRIPT
  • <li role="presentation" class=""><a data-target="#pf7grqlulugbh6wb5rmp9t0fzguwz0sm-javascript" aria-controls="home" role="tab" data-toggle="tab" data-original-lang="js">JAVASCRIPT</a></li><div role="tabpanel" class="tab-pane" id="pf7grqlulugbh6wb5rmp9t0fzguwz0sm-javascript" data-original-lang = "js" ><div class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span></span><span class="c1">/// &lt;reference path=&quot;../tsfiles/jquery.d.ts&quot; /&gt;</span>
    <span class="c1">/// &lt;reference path=&quot;../tsfiles/ej.web.all.d.ts&quot; /&gt;</span>
    
    <span class="nx">module</span> <span class="nx">ScheduleComponent</span> <span class="p">{</span>
        <span class="nx">$</span><span class="p">(</span><span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
            <span class="kd">var</span> <span class="nx">sample</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ej</span><span class="p">.</span><span class="nx">Schedule</span><span class="p">(</span><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#Schedule1&quot;</span><span class="p">),</span> <span class="p">{</span>
                <span class="nx">currentDate</span><span class="o">:</span> <span class="k">new</span> <span class="nb">Date</span><span class="p">(</span><span class="mi">2015</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span>
                <span class="nx">showCurrentTimeIndicator</span><span class="o">:</span> <span class="kc">false</span><span class="p">,</span>
                <span class="nx">workHours</span><span class="o">:</span> <span class="p">{</span>
                    <span class="nx">highlight</span><span class="o">:</span> <span class="kc">false</span>
                <span class="p">},</span>
                <span class="nx">group</span><span class="o">:</span> <span class="p">{</span>
                    <span class="nx">resources</span><span class="o">:</span> <span class="p">[</span><span class="s2">&quot;Owners&quot;</span><span class="p">]</span>
                <span class="p">},</span>
                <span class="nx">resources</span><span class="o">:</span> <span class="p">[{</span>
                    <span class="nx">field</span><span class="o">:</span> <span class="s2">&quot;ownerId&quot;</span><span class="p">,</span>
                    <span class="nx">title</span><span class="o">:</span> <span class="s2">&quot;Owner&quot;</span><span class="p">,</span>
                    <span class="nx">name</span><span class="o">:</span> <span class="s2">&quot;Owners&quot;</span><span class="p">,</span>
                    <span class="nx">resourceSettings</span><span class="o">:</span> <span class="p">{</span>
                        <span class="nx">dataSource</span><span class="o">:</span> <span class="p">[</span>
                            <span class="p">{</span> <span class="nx">text</span><span class="o">:</span> <span class="s2">&quot;Nancy&quot;</span><span class="p">,</span> <span class="nx">id</span><span class="o">:</span> <span class="mi">1</span><span class="p">,</span>  <span class="nx">color</span><span class="o">:</span> <span class="s2">&quot;#f8a398&quot;</span><span class="p">,</span> <span class="nx">on</span><span class="o">:</span> <span class="mi">10</span><span class="p">,</span> <span class="nx">off</span><span class="o">:</span> <span class="mi">18</span> <span class="p">},</span>
                            <span class="p">{</span> <span class="nx">text</span><span class="o">:</span> <span class="s2">&quot;Steven&quot;</span><span class="p">,</span> <span class="nx">id</span><span class="o">:</span> <span class="mi">3</span><span class="p">,</span> <span class="nx">color</span><span class="o">:</span> <span class="s2">&quot;#56ca85&quot;</span><span class="p">,</span> <span class="nx">on</span><span class="o">:</span> <span class="mi">6</span><span class="p">,</span> <span class="nx">off</span><span class="o">:</span> <span class="mi">10</span> <span class="p">},</span>
                            <span class="p">{</span> <span class="nx">text</span><span class="o">:</span> <span class="s2">&quot;Michael&quot;</span><span class="p">,</span> <span class="nx">id</span><span class="o">:</span> <span class="mi">5</span><span class="p">,</span> <span class="nx">color</span><span class="o">:</span> <span class="s2">&quot;#51a0ed&quot;</span><span class="p">,</span> <span class="nx">on</span><span class="o">:</span> <span class="mi">11</span><span class="p">,</span> <span class="nx">off</span><span class="o">:</span> <span class="mi">15</span> <span class="p">}</span>
                        <span class="p">],</span>
                        <span class="nx">text</span><span class="o">:</span> <span class="s2">&quot;text&quot;</span><span class="p">,</span>
                        <span class="nx">id</span><span class="o">:</span> <span class="s2">&quot;id&quot;</span><span class="p">,</span>
                        <span class="nx">color</span><span class="o">:</span> <span class="s2">&quot;color&quot;</span><span class="p">,</span>
                        <span class="nx">start</span><span class="o">:</span> <span class="s2">&quot;on&quot;</span><span class="p">,</span>
                        <span class="nx">end</span><span class="o">:</span> <span class="s2">&quot;off&quot;</span>
                    <span class="p">}</span>
                <span class="p">}],</span>
                <span class="nx">appointmentSettings</span><span class="o">:</span> <span class="p">{</span>
                    <span class="nx">resourceFields</span><span class="o">:</span> <span class="s2">&quot;ownerId&quot;</span><span class="p">,</span>
                    <span class="nx">dataSource</span><span class="o">:</span> <span class="p">[{</span>
                        <span class="nx">Id</span><span class="o">:</span> <span class="mi">100</span><span class="p">,</span>
                        <span class="nx">Subject</span><span class="o">:</span> <span class="s2">&quot;Wild Discovery&quot;</span><span class="p">,</span>
                        <span class="nx">StartTime</span><span class="o">:</span> <span class="k">new</span> <span class="nb">Date</span><span class="p">(</span><span class="mi">2015</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">00</span><span class="p">),</span>
                        <span class="nx">EndTime</span><span class="o">:</span> <span class="k">new</span> <span class="nb">Date</span><span class="p">(</span><span class="mi">2015</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">30</span><span class="p">),</span>
                        <span class="nx">ownerId</span><span class="o">:</span> <span class="mi">3</span><span class="p">,</span>
                        <span class="nx">Location</span><span class="o">:</span> <span class="s2">&quot;CHINA&quot;</span>
                    <span class="p">}]</span>
                <span class="p">}</span>
            <span class="p">});</span>
        <span class="p">});</span>
    <span class="p">}</span></code></pre></div>
    </div>

    Display Scheduler with Appointments Filtered by Subject

    It is possible to display the Scheduler with appointments, which is filtered based on the Subject. For example, if we keep a text box and start typing a character – the list of appointments whose subject matches the typed character alone will be shown on the Scheduler. The following code example depicts the way to achieve this.

  • HTML
  • <!--textbox for entering search character-->
    <input id='txtSearch' type='text' onkeyup='searchKeyUp()' />
    
    <!--Container for ejScheduler widget-->
    <div id="Schedule1"></div>
  • JAVASCRIPT
  • /// <reference path="../tsfiles/jquery.d.ts" />
    /// <reference path="../tsfiles/ej.web.all.d.ts" />
    
    module ScheduleComponent {
        $(function () {
            // Appointment data to be bound to the Scheduler
            window.Default = [
                {
                    Id: 101,
                    Subject: "Bering Sea Gold",
                    StartTime: new Date(2015, 11, 5, 10, 00),
                    EndTime: new Date(2015, 11, 5, 11, 00),
                    Description: "",
                    AllDay: false,
                    Recurrence: false,
                    Categorize: "1,3"
                }, {
                    Id: 102,
                    Subject: "Bering Sea Gold",
                    StartTime: new Date(2015, 11, 2, 16, 00),
                    EndTime: new Date(2015, 11, 2, 17, 30),
                    Description: "",
                    AllDay: false,
                    Recurrence: false,
                    Categorize: "2,5"
                }, {
                    Id: 104,
                    Subject: "What Happened Next?",
                    StartTime: new Date(2015, 11, 5, 12, 30),
                    EndTime: new Date(2015, 11, 5, 15, 00),
                    Description: "",
                    AllDay: false,
                    Recurrence: false,
                    Categorize: "4,1"
                }, {
                    Id: 105,
                    Subject: "Daily Planet",
                    StartTime: new Date(2015, 11, 3, 01, 00),
                    EndTime: new Date(2015, 11, 3, 02, 00),
                    Description: "",
                    AllDay: false,
                    Recurrence: false,
                    Categorize: "1,3,6"
                }, {
                    Id: 107,
                    Subject: "How It's Made",
                    StartTime: new Date(2015, 11, 1, 06, 00),
                    EndTime: new Date(2015, 11, 1, 07, 30),
                    Description: "",
                    AllDay: false,
                    Recurrence: true,
                    RecurrenceRule: "FREQ=WEEKLY;BYDAY=MO,TU;INTERVAL=1;COUNT=15",
                    Categorize: "2,3,6"
                }, {
                    Id: 108,
                    Subject: "Deadest Catch",
                    StartTime: new Date(2015, 11, 3, 16, 00),
                    EndTime: new Date(2015, 11, 3, 17, 00),
                    Description: "",
                    AllDay: false,
                    Recurrence: false,
                    Categorize: "2,4,6,1"
                }, {
                    Id: 109,
                    Subject: "MayDay",
                    StartTime: new Date(2015, 3, 30, 06, 30),
                    EndTime: new Date(2015, 3, 30, 07, 30),
                    Description: "",
                    AllDay: false,
                    Recurrence: false,
                    Categorize: "5,3"
                }, {
                    Id: 115,
                    Subject: "Cash Cab",
                    StartTime: new Date(2015, 3, 30, 15, 00),
                    EndTime: new Date(2015, 3, 30, 16, 30),
                    Description: "",
                    AllDay: false,
                    Recurrence: true,
                    RecurrenceRule: "FREQ=DAILY;INTERVAL=1;COUNT=5",
                    Categorize: "1,3"
                }
            ];
            var sample = new ej.Schedule($("#Schedule1"), {
                currentDate: new Date(2015, 11, 5),
                showCurrentTimeIndicator: false,
                appointmentSettings: {
                    dataSource: dManager
                }
            });
        });
    }
  • JAVASCRIPT
  • // This function executes when a character is entered in the textbox
    function searchKeyUp() {
        var searchString = $("#txtSearch").val();
        var schObj = $("#Schedule1").data("ejSchedule");
        var result = schObj.searchAppointments(searchString);
        schObj.option("appointmentSettings", { dataSource: [] });
        if (!ej.isNullOrUndefined(result) && result.length != 0 && searchString != "")
            schObj.option("appointmentSettings", { dataSource: result });
        else
            schObj.option("appointmentSettings", { dataSource: window.Default });
    }

    Customize the Default Appointment Window

    Apart from the custom appointment window, it is possible to customize the default appointment window by adding/removing the required number of fields into it. This can be achieved through the appointmentWindowOpen event of the scheduler.

    The following code example depicts the way to achieve the customization of default appointment window.

  • HTML
  • <!--Container for ejScheduler widget-->
    <div id="Schedule1"></div>
  • JAVASCRIPT
  • /// <reference path="../tsfiles/jquery.d.ts" />
    /// <reference path="../tsfiles/ej.web.all.d.ts" />
    
    module ScheduleComponent {
        $(function () {
            var sample = new ej.Schedule($("#Schedule1"), {
                currentDate: new Date(2015, 11, 2),
                showCurrentTimeIndicator: false,
                appointmentWindowOpen: "onAppointmentOpen",
                appointmentSettings: {
                    dataSource: [{
                        Id: 100,
                        Subject: "Wild Discovery",
                        StartTime: new Date(2015, 11, 2, 9, 00),
                        EndTime: new Date(2015, 11, 2, 10, 30),
                        Location: "CHINA",
                        AppointmentType: "Tentative",
                        Status: "90%"
                    }]
                }
            });
        });
    }
  • JAVASCRIPT
  • // This function executes before the appointment window gets opened.
    function onAppointmentOpen(args) {
        // to add custom element in default appointment window
        if (this._appointmentAddWindow.find(".custom-fields").length == 0) {
            var customDesign = "<tr class='custom-fields'><td class='e-textlabel'>Event Type</td><td><input class='app-type' type='text'/></td><td class='e-textlabel'>Event Status </td><td><input class='status' type='text'/></td></tr>";
            $(customDesign).insertAfter(this._appointmentAddWindow.find("." + this._id + "appointmentArrow"));
        }
    
        if (!ej.isNullOrUndefined(args.appointment)) {
            // if double clicked on the appointments, retrieve the custom field values from the appointment object and fills it in the appropriate fields.
            this._appointmentAddWindow.find(".app-type").val(args.appointment.AppointmentType);
            this._appointmentAddWindow.find(".status").val(args.appointment.Status);
        } else {
            // if double clicked on the cells, clears the field values.
            this._appointmentAddWindow.find(".app-type").val("");
            this._appointmentAddWindow.find(".status").val("");
        }
    }

    Synchronize the Schedule with Outlook

    Schedule appointments can be synchronized with Outlook and vice versa, by making use of the Microsoft Outlook 12/15 Object library.

    The following code example depicts the way to synchronize the Schedule with Outlook.

  • HTML
  • <!--Container for ejScheduler widget-->
    <div id="Schedule1"></div>
  • JAVASCRIPT
  • /// <reference path="../tsfiles/jquery.d.ts" />
    /// <reference path="../tsfiles/ej.web.all.d.ts" />
    
    module ScheduleComponent {
        $(function () {
            var dataManager = ej.DataManager({
                url: '@Url.Action("GetApp", "Home")',
                crudUrl: '@Url.Action("Batch","Home")',
                adaptor: new ej.UrlAdaptor(),
                crossDomain: true
            });
            var sample = new ej.Schedule($("#Schedule1"), {
                width: "100%",
                height: "525px",
                currentDate: new Date(2015, 5, 15),
                appointmentSettings: {
                    dataSource: dataManager,
                    id: "Id",
                    subject: "Subject",
                    startTime: "StartTime",
                    endTime: "EndTime",
                    startTimeZone: "StartTimeZone",
                    endTimeZone: "EndTimeZone",
                    allDay: "AllDay",
                    recurrence: "Recurrence",
                    recurrenceRule: "RecurrenceRule"
                }
            });
        });
    }

    Schedule control is not directly compatible with the Outlook calendar object, as it returns the COM object. Therefore, in order to bind the schedule control with those data retrieved from outlook, then it is necessary to convert the COM object into the schedule acceptable object format. To do so add the following code example in your code behind.

  • C#
  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.ComponentModel;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using Microsoft.Office.Interop.Outlook; // required Microsoft Assembly 
    using System.Web.Script.Serialization;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using ScheduleCRUDJS.Models;
    using System.Collections;
    namespace ScheduleCRUDJS.Controllers
    {
        public class HomeController : Controller
        {
            ScheduleDataDataContext db = new ScheduleDataDataContext();
    
            public ActionResult Index()
            {
                return View();
            }
    
            [HttpPost]
            public JsonResult GetApp() // function to display the outlook appointments in Schedule initially
            {
                IEnumerable data = GetData(); 
                return Json(data, JsonRequestBehavior.AllowGet);
            }
            public List<MultipleResource> GetData() // function to return the outlook appointments
            {
                Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
                Microsoft.Office.Interop.Outlook.NameSpace mapNamespace = oApp.GetNamespace("MAPI");
                Microsoft.Office.Interop.Outlook.MAPIFolder CalendarFolder = mapNamespace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar);
                Microsoft.Office.Interop.Outlook.Items outlookCalendarItems = CalendarFolder.Items;
                List<Microsoft.Office.Interop.Outlook.AppointmentItem> appointmentList = new List<Microsoft.Office.Interop.Outlook.AppointmentItem>();
    
                foreach (Microsoft.Office.Interop.Outlook.AppointmentItem item in outlookCalendarItems)
                {
                    appointmentList.Add(item);
                }
    
                List<MultipleResource> newApp = new List<MultipleResource>();
                // converting COM object into IEnumerable object
                for (var i = 0; i < appointmentList.Count; i++)
                {
                    MultipleResource app = new MultipleResource();
                    app.Id = i;
                    app.Subject = appointmentList[i].Subject;
                    app.AllDay = appointmentList[i].AllDayEvent;
                    app.StartTime = Convert.ToDateTime(appointmentList[i].Start.ToString());
                    string endTime = appointmentList[i].End.ToString();
                    DateTime appEndDate = Convert.ToDateTime(endTime);
                    if (endTime.Contains("12:00:00 AM") && app.AllDay == true)
                        app.EndTime = appEndDate.AddMinutes(-1);
                    else
                        app.EndTime = appEndDate;
                    app.Recurrence = false;
                    app.RecurrenceRule = null;
                    newApp.Add(app);
                }
                return newApp;
            }
            public JsonResult Batch(EditParams param)
            {
                if (param.action == "insert" || (param.action == "batch" && param.added != null))          // this block of code will execute while inserting the appointments
                {
                    var value = param.action == "insert" ? param.value : param.added[0];
                    int intMax = db.MultipleResources.ToList().Count > 0 ? db.MultipleResources.ToList().Max(p => p.Id) : 1;
                    DateTime startTime = Convert.ToDateTime(value.StartTime);
                    DateTime endTime = Convert.ToDateTime(value.EndTime);
                    MultipleResource appoint = new MultipleResource()
                    {
                        Id = intMax + 1,
                        StartTime = startTime,
                        EndTime = endTime,
                        StartTimeZone = value.StartTimeZone,
                        EndTimeZone = value.EndTimeZone,
                        Subject = value.Subject,
                        OwnerId = value.OwnerId,
                        AllDay = value.AllDay,
                        Recurrence = value.Recurrence,
                        RecurrenceRule = value.RecurrenceRule
                    };
                    db.MultipleResources.InsertOnSubmit(appoint);
                    db.SubmitChanges();
                    Microsoft.Office.Interop.Outlook.Application outlookApp = new Microsoft.Office.Interop.Outlook.Application();
                    Microsoft.Office.Interop.Outlook.AppointmentItem newAppointment = null;
                    newAppointment = (Microsoft.Office.Interop.Outlook.AppointmentItem)outlookApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olAppointmentItem);
                    newAppointment.Start = Convert.ToDateTime(value.StartTime).ToUniversalTime();
                    newAppointment.End = Convert.ToDateTime(value.EndTime).ToUniversalTime();
                    newAppointment.Subject = value.Subject.ToString();
                    newAppointment.Save();
                }
                IEnumerable data = new ScheduleDataDataContext().MultipleResources.Take(200);
                return Json(data, JsonRequestBehavior.AllowGet);
            }
        }
    }

    NOTE

    In order to achieve the above scenario, need to refer the Microsoft assembly in your application (Microsoft Outlook 12/15 Object library [Microsoft.Office.Interop.Outlook] and refer it in the controller page as shown above).