public ActionResult AjaxSearchResults(jQueryDataTableParamModel param)
        {
            ExchangeService service = Connection.ConnectEWS();
            FindItemsResults<Item> fMsgs;
            FindItemsResults<Item> aMsgs;
            List<EmailMessage> dMsgs = new List<EmailMessage>();

            Folder gsf = GetGlobalFolder(service);

            Search sc = Session["search"] as Search;

            //Search sc = TempData["search"] as Search;

            if (sc != null)
            {
                logger.Debug("Hit Ajax Search with Search criteria -- " + sc.subject + sc.to + sc.from + sc.datefrom + sc.dateto);

                if (sc.comms != null && sc.comms.Count() > 0)
                {
                    foreach (string s in sc.comms)
                    {
                        logger.Debug("Search Commissioner: " + s);
                    }
                }
            }
            else
            {
                logger.Debug("AjaxSearch detected NULL SearchSession");
            }

            string dateFilter;
            string fromFilter;
            string subjectFilter;
            string email;
            int totaldisplay;

            //Try getting the Datatables FILTER parameters or set to null. They're processed/tested later:
            try
            {
                dateFilter = Convert.ToString(Request["sSearch_0"]);
                fromFilter = Convert.ToString(Request["sSearch_1"]);
                subjectFilter = Convert.ToString(Request["sSearch_2"]);
                email = Convert.ToString(Request["email"]);

                //sc = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Search>(Request["query"]);
            }
            catch (Exception e)
            {
                dateFilter = null;
                fromFilter = null;
                subjectFilter = null;
                email = null;
                logger.Debug(e.Message);
            }

            if (!string.IsNullOrWhiteSpace(email))
            {
                logger.Debug("Received email in querystring: " + email);
            }

            logger.Debug("sSearch Params -- " + dateFilter + fromFilter + subjectFilter);

            if (sc != null)
            {
                logger.Debug("Search form criteria -- " + sc.dateto + sc.datefrom);

                if ((!string.IsNullOrWhiteSpace(dateFilter) && !dateFilter.Equals("~")) || !string.IsNullOrWhiteSpace(fromFilter) || !string.IsNullOrWhiteSpace(subjectFilter))
                {
                    fMsgs = GetCMessages(service, gsf, sc.comms, param.iDisplayStart, param.iDisplayLength,
                        sc.datefrom, sc.dateto, sc.to, sc.from, sc.subject, sc.bodytext,
                        dateFilter, fromFilter, subjectFilter);
                    foreach (EmailMessage i in fMsgs)
                    {
                        dMsgs.Add(i);
                    }
                    totaldisplay = fMsgs.TotalCount;
                }
                else
                {
                    fMsgs = GetCMessages(service, gsf, sc.comms, param.iDisplayStart, param.iDisplayLength,
                        sc.datefrom, sc.dateto, sc.to, sc.from, sc.subject, sc.bodytext,
                        dateFilter, fromFilter, subjectFilter);
                    foreach (EmailMessage i in fMsgs)
                    {
                        dMsgs.Add(i);
                    }
                    totaldisplay = fMsgs.TotalCount;
                }

            }
            else if (!string.IsNullOrWhiteSpace(email))
            {
                logger.Debug("No session search detected. Browsing for: " + email);
                gsf = GetSourceFolder(service, email);

                if ((!string.IsNullOrWhiteSpace(dateFilter) && !dateFilter.Equals("~")) || !string.IsNullOrWhiteSpace(fromFilter) || !string.IsNullOrWhiteSpace(subjectFilter))
                {
                    string[] caemail = new string[] { email };

                    aMsgs = GetCMessages(service, gsf, caemail, param.iDisplayStart, param.iDisplayLength, null, null, null, null, null, null, dateFilter, fromFilter, subjectFilter);
                    foreach (EmailMessage i in aMsgs)
                    {
                        dMsgs.Add(i);
                    }
                    totaldisplay = aMsgs.TotalCount;
                }
                else
                {
                    logger.Debug("No filter params detected. Browsing for: " + email);

                    aMsgs = GetCMessages(service, gsf, param.iDisplayStart, param.iDisplayLength);

                    foreach (EmailMessage i in aMsgs)
                    {
                        dMsgs.Add(i);
                    }
                    totaldisplay = aMsgs.TotalCount;
                    logger.Debug("Found messages for " + email + " : " + aMsgs.TotalCount + ". dMsgs count: " + dMsgs.Count);
                }
            }
            else
            {
                if ((!string.IsNullOrWhiteSpace(dateFilter) && !dateFilter.Equals("~")) || !string.IsNullOrWhiteSpace(fromFilter) || !string.IsNullOrWhiteSpace(subjectFilter))
                {
                    string[] c = { };
                    string s = "";
                    fMsgs = GetCMessages(service, gsf, c, param.iDisplayStart, param.iDisplayLength,
                        s, s, s, s, s, s,
                        dateFilter, fromFilter, subjectFilter);
                    foreach (EmailMessage i in fMsgs)
                    {
                        dMsgs.Add(i);
                    }
                    totaldisplay = fMsgs.TotalCount;
                }
                else
                {
                    aMsgs = GetCMessages(service, gsf, param.iDisplayStart, param.iDisplayLength);
                    foreach (EmailMessage i in aMsgs)
                    {
                        dMsgs.Add(i);
                    }
                    totaldisplay = aMsgs.TotalCount;
                }
            }

            var result = dMsgs.Select(x => new[] { x.DateTimeReceived.ToString(), x.From.Name, x.Subject, HttpUtility.UrlEncode(x.Id.ToString()), HttpUtility.UrlEncode(x.Id.ToString()) });

            return Json(new
            {
                sEcho = param.sEcho,
                iTotalRecords = gsf.TotalCount,
                iTotalDisplayRecords = totaldisplay,
                aaData = result
            },
            JsonRequestBehavior.AllowGet);
        }
        public ActionResult ajaxComm(jQueryDataTableParamModel param, string email)
        {
            logger.Debug("Parameters (iDisplayStart, iDisplayLength, sSearch, sEcho): " + param.iDisplayStart + "; " + param.iDisplayLength + "; " + param.sSearch + "; " + param.sEcho);
            ExchangeService service = Connection.ConnectEWS();
            Folder gsf = GetSourceFolder(service, email);
            FindItemsResults<Item> fMsgs;
            FindItemsResults<Item> aMsgs;
            List<EmailMessage> dMsgs = new List<EmailMessage>();
            int totaldisplay = gsf.TotalCount;

            //Get search parameters:
            string dateFilter;
            string fromFilter;
            string subjectFilter;

            try
            {
                dateFilter = Convert.ToString(Request["sSearch_0"]);
                fromFilter = Convert.ToString(Request["sSearch_1"]);
                subjectFilter = Convert.ToString(Request["sSearch_2"]);
            }
            catch (Exception e)
            {
                dateFilter = null;
                fromFilter = null;
                subjectFilter = null;
                logger.Debug(e.Message);
            }

            logger.Debug("Search Parameters (iDisplayStart, iDisplayLenght, sSearch, sEcho): " + param.iDisplayStart + "; " + param.iDisplayLength + "; " + dateFilter + fromFilter + subjectFilter + "; " + param.sEcho);

            //List<EmailMessage> aMsgs = ArchivesController.GetMessages(param.iDisplayStart, param.iDisplayLength);

            if ((!string.IsNullOrWhiteSpace(dateFilter) && !dateFilter.Equals("~")) || !string.IsNullOrWhiteSpace(fromFilter) || !string.IsNullOrWhiteSpace(subjectFilter))
            {
                fMsgs = ArchivesController.GetAllMessages(service, gsf, param.iDisplayStart, param.iDisplayLength, dateFilter, fromFilter, subjectFilter);
                foreach (EmailMessage i in fMsgs)
                {
                    dMsgs.Add(i);
                }
                totaldisplay = fMsgs.TotalCount;
            }
            else
            {
                aMsgs = ArchivesController.GetAllMessages(service, gsf, param.iDisplayStart, param.iDisplayLength);
                foreach (EmailMessage i in aMsgs)
                {
                    dMsgs.Add(i);
                }
                totaldisplay = aMsgs.TotalCount;
            }

            var result = dMsgs.Select(x => new[] { x.DateTimeReceived.ToString(), x.From.Name, x.Subject, HttpUtility.HtmlEncode(x.Id) });

            return Json(new
            {
                sEcho = param.sEcho,
                iTotalRecords = gsf.TotalCount,
                iTotalDisplayRecords = totaldisplay,
                aaData = result
            },
            JsonRequestBehavior.AllowGet);
        }