public void NavigateToCalendar()
 {            
         string deptCalName = Config.DepartmentCalendar;//default from web.config
         //Get from App Config (custom)
         ConfigListValues objConfAppList = new ConfigListValues();
         objConfAppList.GetConfigValues(null);
         if (objConfAppList.items != null)
         {
             if (objConfAppList.items["DepartmentCalendar"] != null)                  
                 deptCalName = objConfAppList.items["DepartmentCalendar"].ToString();
         }
         Response.Redirect(Request.QueryString["SPHostUrl"] + "/_layouts/15/start.aspx#/Lists/" + deptCalName, false);    
 }
        public void IsMember(User CurrentUser)
        {
            string siteApproverGroupname = Config.TimeOffApprovers;
            ConfigListValues objConfigAppList = new ConfigListValues();
            objConfigAppList.GetConfigValues(null);
            if (objConfigAppList.items != null)
            {
                if (objConfigAppList.items.ContainsKey("TimeOffApprovers"))
                    siteApproverGroupname = objConfigAppList.items["TimeOffApprovers"].ToString();
            }

            GroupsClass objTOR = new GroupsClass();
            IsGroupMember = objTOR.IsCurrentUserExistInGroup(siteApproverGroupname, CurrentUser.LoginName);
        }
 protected void btnChkCalendar_Click(object sender, EventArgs e)
 {
   //  VerifyApprover();
     string deptCalName = Config.DepartmentCalendar;//default from web.config
     //Get from App Config (custom)
     ConfigListValues objConfAppList = new ConfigListValues();
     objConfAppList.GetConfigValues(null);
     if (objConfAppList.items.Count>0 )
     {
         if (objConfAppList.items["DepartmentCalendar"] != null)             
             deptCalName = objConfAppList.items["DepartmentCalendar"].ToString();
     }
     Response.Redirect(Request.QueryString["SPHostUrl"] + "/_layouts/15/start.aspx#/Lists/" + deptCalName, false);
 }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            GroupsClass obj = new GroupsClass();
           // sharepointUrl = new Uri(Request.QueryString[Config.ListURL]);
            timeOffRequestID = Request.QueryString["RequestID"];
            string[] reqdAtten = { RequestedByEmail };
            string[] optAtten={};
            if (UpdateStatus(((Button)sender).CommandArgument, "Approved"))
            {
                string msg = "Time Off Request of " + this.lblTimeoffType.Text + ": on " + this.txtStartDate.Text + " to " + this.txtEndDate.Text + " has been Approved";
                EWSClass objEWS = new EWSClass();
                if (this.lblFullday.Text == "Full Day(s)")
                {
                    this.txtStartDate.Text += " 12:00 AM";
                    this.txtEndDate.Text += " 12:00 PM";
                }

                        
                string UserID = Config.SenderEmail;//default from web.config
                string UserPassword = Config.SenderPassword;
                string WorkingHours = Config.WorkingHours;            
                ConfigListValues objConfigAppList = new ConfigListValues();
                objConfigAppList.GetConfigValues(null);
                if (objConfigAppList.items != null)
                {
                    if (objConfigAppList.items.ContainsKey("SenderEmail"))                   
                        UserID = objConfigAppList.items["SenderEmail"].ToString();  
                    if (objConfigAppList.items.ContainsKey("SenderPassword"))                  
                        UserPassword = objConfigAppList.items["SenderPassword"].ToString();
                    if (objConfigAppList.items.ContainsKey("WorkingHours"))
                        WorkingHours = objConfigAppList.items["WorkingHours"].ToString();
                }
                objEWS.SetupCalendarEvent(msg, this.txtStartDate.Text, this.txtEndDate.Text, reqdAtten, optAtten, UserID, UserPassword, WorkingHours);
                try
                {
                    string siteToken;
                    if (Session["contextToken"] != null)
                    {
                        DeptCalListClass objDept = new DeptCalListClass();
                        siteToken = Session["contextToken"] as string;
                        objDept.AddDeptCal(null, this.lblRequestedBy.Text + "-" + this.lblTimeoffType.Text, this.txtStartDate.Text, this.txtEndDate.Text);
                    }
                }
                catch { }
                Response.Redirect("ui.aspx?SPHostUrl=" + Request.QueryString["SPHostUrl"].ToString() + "&approval=1&" + Config.ListURL + "=" + Request.QueryString["SPAppWebUrl"].ToString());  
            }
            Response.Redirect("ui.aspx?SPHostUrl=" + Request.QueryString["SPHostUrl"].ToString() + "&approval=0&" + Config.ListURL + "=" + Request.QueryString["SPAppWebUrl"].ToString());               
        }
        //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;
            }
        }