//public bool CancelRequest(string sharepointUrl, string accessToken,string hosturl,string apponlytoken, string status)
        public bool CancelRequest(SharePointContext spContext, string status)
      //  public bool CancelRequest( string status)
        {
            Microsoft.SharePoint.Client.ListItemCollection listItems;
          // using (ClientContext clientContext = TokenHelper.GetClientContextWithAccessToken(sharepointUrl, accessToken))
            if(spContext==null)
              spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext.Current);
            using (var clientContext = spContext.CreateAppOnlyClientContextForSPAppWeb())
            {

                try
                {
                    //if (this.StartDate > DateTime.Now)
                    //{
                    Web web = clientContext.Web;
                    ListCollection lists = web.Lists;
                    List selectedList = lists.GetByTitle("TimeOffRequests");
                    clientContext.Load<ListCollection>(lists); // this lists object is loaded successfully
                    clientContext.Load<List>(selectedList);  // this list object is loaded successfully
                    clientContext.ExecuteQuery();

                    CamlQuery camlQuery = new CamlQuery();
                    StringBuilder camlwhere = new StringBuilder();
                    camlwhere.Append("<Where>");
                    camlwhere.Append("<Eq><FieldRef Name='RequestID'/><Value Type='Text'>" + this.RequestID + "</Value></Eq>");
                    camlwhere.Append("</Where>");
                    camlQuery.ViewXml = @"<View><Query>" + camlwhere.ToString() + "</Query></View>";
                    listItems = selectedList.GetItems(camlQuery);
                    clientContext.Load<Microsoft.SharePoint.Client.ListItemCollection>(listItems);
                    clientContext.ExecuteQuery();

                    string concernApprover = "";

                    foreach (Microsoft.SharePoint.Client.ListItem oListItem in listItems)
                    {
                        if (status == "Approved")
                        {
                            if (oListItem["ID"] != null)
                            {
                                Microsoft.SharePoint.Client.ListItem listItem = selectedList.GetItemById(Convert.ToInt32(oListItem["ID"].ToString()));
                                //  var app1 = oListItem["Approver1"] as Microsoft.SharePoint.Client.FieldLookupValue;
                                //var app2 = oListItem["Approver2"] as Microsoft.SharePoint.Client.FieldLookupValue;
                                //var app3 = oListItem["Approver3"] as Microsoft.SharePoint.Client.FieldLookupValue;

                                //var status1 = oListItem["Approver1Status"] as string;
                                //var status2 = oListItem["Approver2Status"] as string;
                                //var status3 = oListItem["Approver3Status"] as string;

                                //if (status1 != "")                                    
                                //    concernApprover = app1.ToString();                                  
                                //if (status2 != "")
                                //    concernApprover = app2.ToString();
                                //if (status3 != "")                                 
                                //    concernApprover = app3.ToString();
                                // concernApprover = app1.ToString();
                                //Send an Cancellation email to approver1
                                //update "CancelStatus"
                                if (oListItem["Approver1"] == null)  // if there is no Approvers
                                {
                                    // listItem.DeleteObject();//implemented in workflow
                                    listItem["CancelStatus"] = "Cancel";
                                    listItem.Update();
                                    // Delete from calendar

                                    try
                                    {

                                        string deptCalName = Config.DepartmentCalendar;//default from web.config
                                        //Get from App Config (custom)
                                        ConfigListValues objConfAppList = new ConfigListValues();
                                        objConfAppList.GetConfigValues(spContext);
                                        if (objConfAppList.items != null)
                                        {
                                            if (objConfAppList.items["DepartmentCalendar"] != null)
                                            {
                                                deptCalName = objConfAppList.items["DepartmentCalendar"].ToString();
                                            }
                                        }


                                        DeptCalendar obj = new DeptCalendar();
                                        string requestorName = oListItem["RequestedBy"] as Microsoft.SharePoint.Client.FieldLookupValue == null ? "" : (oListItem["RequestedBy"] as Microsoft.SharePoint.Client.FieldLookupValue).LookupValue;
                                        string type = oListItem["TimeOffType"].ToString() == null ? "UnAssigned" : oListItem["TimeOffType"].ToString();
                                        obj.Title = requestorName + "-" + type;
                                        DateTime startDate = (DateTime)oListItem["StartDateTime"];
                                        obj.EventTime = startDate;

                                        // obj.DeleteEvent(hosturl, apponlytoken, deptCalName);
                                        obj.DeleteEvent(deptCalName, spContext);
                                    }
                                    catch
                                    {
                                    }

                                }
                                else
                                {
                                    listItem["CancelStatus"] = "Cancel";
                                    listItem.Update();
                                }
                            }
                        }
                        else
                        {
                            Microsoft.SharePoint.Client.ListItem listItem = selectedList.GetItemById(Convert.ToInt32(oListItem["ID"].ToString()));

                            listItem["CancelStatus"] = "Cancel";
                            listItem.Update();

                            //listItem.DeleteObject(); //implemented in workflow


                        }
                    }

                    clientContext.ExecuteQuery();

                    // }
                }

                catch (Exception ex)
                {
                    Microsoft.SharePoint.Client.Utilities.Utility.LogCustomRemoteAppError(clientContext, Global.ProductId, ex.Message);
                    clientContext.ExecuteQuery();
                  
                }

                return true;
            }
        }
        public static string DeleteRequest(string requestid, string startdate, string enddate, string requestorname, string type, string status)
        {

            if (HttpContext.Current.Session["CurrentSPContext"] != null)
            {
                TimeOffRequests obj = new TimeOffRequests();
                obj.RequestID = requestid;
                obj.StartDate = DateTime.Parse(startdate);
                obj.EndDate = DateTime.Parse(enddate);
                if (status == "Approved")
                {
                    string CancelLeaveDay = Config.CancelLeaveDay;
                    ConfigListValues objConfigAppList = new ConfigListValues();
                    objConfigAppList.GetConfigValues( HttpContext.Current.Session["CurrentSPContext"] as SharePointContext);
                    if (objConfigAppList.items != null)
                    {
                        if (objConfigAppList.items["CancelLeaveDay"] != null)
                        { CancelLeaveDay = objConfigAppList.items["CancelLeaveDay"].ToString(); }
                    }
                    DateTime dtNew = DateTime.Now.AddDays(Convert.ToInt32(CancelLeaveDay));
                    if (obj.StartDate > dtNew)
                    {
                        if (obj.DeleteRequest(HttpContext.Current.Session["CurrentSPContext"] as SharePointContext,"1"))
                        {
                            string deptCalName = Config.DepartmentCalendar;//default from web.config                        
                            try
                            {
                                ConfigListValues objConfAppList = new ConfigListValues();
                                objConfAppList.GetConfigValues( HttpContext.Current.Session["CurrentSPContext"] as SharePointContext);
                                if (objConfAppList.items != null)
                                {
                                    if (objConfAppList.items["DepartmentCalendar"] != null)
                                    {
                                        deptCalName = objConfAppList.items["DepartmentCalendar"].ToString();
                                    }
                                }
                                DeptCalendar objDept = new DeptCalendar();
                                objDept.Title = requestorname + "-" + type;
                                DateTime startDate = DateTime.Parse(startdate);
                                objDept.EventTime = startDate;
                                objDept.DeleteEvent(deptCalName,HttpContext.Current.Session["CurrentSPContext"] as SharePointContext);
                                return "Request has been cancelled and deleted.";
                            }
                            catch (Exception ex)
                            {
                            }
                            return "Your request has been successfully submitted for Cancellation. Approver has to review.";
                        }
                        else
                            return "Unable to cancel the Approved Request. Please try again.";
                    }
                    else
                        return "You can cancel the request only which are requested after " + dtNew.ToShortDateString();
                }
                else
                    return "Only Approved Requests will be processed.";
            }
            return "";
        }