public bool UpdateStatus(string accessToken, string status) { bool returnvalue = false; //if (!VerifyApp()) // return false; if (Request.QueryString[Config.ListURL] != null) { // sharepointUrl = new Uri(Request.QueryString[Config.ListURL]); try { // using (ClientContext clientContext = TokenHelper.GetClientContextWithAccessToken(sharepointUrl.ToString(), accessToken)) var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext.Current); using (var clientContext = spContext.CreateAppOnlyClientContextForSPAppWeb()) { Web web = clientContext.Web; ListCollection lists = web.Lists; List selectedList = lists.GetByTitle("TimeOffRequests"); clientContext.Load<ListCollection>(lists); clientContext.Load<List>(selectedList); clientContext.ExecuteQuery(); CamlQuery camlQuery = new CamlQuery(); string camlwhere = "<Where>"; camlwhere += "<Eq><FieldRef Name='RequestID'/><Value Type='Text'>" + timeOffRequestID + "</Value></Eq>"; camlwhere += "</Where>"; camlQuery.ViewXml = @"<View><Query>" + camlwhere + "</Query></View>"; Microsoft.SharePoint.Client.ListItemCollection listItems = selectedList.GetItems(camlQuery); clientContext.Load<Microsoft.SharePoint.Client.ListItemCollection>(listItems); clientContext.ExecuteQuery(); foreach (Microsoft.SharePoint.Client.ListItem oListItem in listItems) { oListItem["Status"] = "Pending Approval";//default oListItem["Approver1Status"] = oListItem["Approver1Status"] != null ? oListItem["Approver1Status"] : ""; oListItem["Approver2Status"] = oListItem["Approver2Status"] != null ? oListItem["Approver2Status"] : ""; oListItem["Approver3Status"] = oListItem["Approver3Status"] != null ? oListItem["Approver3Status"] : ""; if (oListItem["Approver1"] != null && oListItem["Approver1Status"].ToString() == "Pending Approval") { var docType = oListItem["Approver1"] as Microsoft.SharePoint.Client.FieldLookupValue; string app1 = ""; if (docType != null) app1 = docType == null ? "UnAssigned" : docType.LookupValue; if (CurrentUser == null) LoadUserProfile(); if (CurrentUser.Title != app1) { this.lblerrmsg.Text = " Approver1 : " + lblappr1.Text + " has to process this request prior your actions"; return false; } oListItem["Approver1Modified"] = DateTime.Now; oListItem["Approver1Status"] = status; //If more Approver2 or 3 exists, if (status == "Denied") { oListItem["Status"] = status; returnvalue = true; } else { if (this.lblappr2.Text != "" || this.lblappr3.Text != "") oListItem["Status"] = "Pending Approval"; else { oListItem["Status"] = status; if (oListItem["Status"].ToString() == "Approved") { try { DeptCalListClass objDept = new DeptCalListClass(); objDept.AddDeptCal(null, this.lblRequestedBy.Text + "-" + this.lblTimeoffType.Text, this.txtStartDate.Text, this.txtEndDate.Text); } catch { } } returnvalue = true; } } } //Logged user is Approver2 // if (CurrentApproverLevel == 2) else { if (oListItem["Approver2"] != null && oListItem["Approver2Status"].ToString() == "Pending Approval") { //if Approver1 didnt approved. if (oListItem["Approver1Status"].ToString() != "Approved") { this.lblerrmsg.Text = " Approver1 : " + lblappr1.Text + " has to process this request prior your actions"; return false; } //validate current user var docType = oListItem["Approver2"] as Microsoft.SharePoint.Client.FieldLookupValue; string app2 = ""; if (docType != null) app2 = docType == null ? "UnAssigned" : docType.LookupValue; if (CurrentUser == null) LoadUserProfile(); if (CurrentUser.Title != app2) { this.lblerrmsg.Text = " You do not have the access to process this request."; return false; } { if (oListItem["Approver1Status"].ToString() == "Approved") { oListItem["Approver2Modified"] = DateTime.Now; oListItem["Approver2Status"] = status; //if Approver3 Exists if (status == "Denied") { oListItem["Status"] = status; returnvalue = true; } else { if (this.lblappr3.Text != "") oListItem["Status"] = "Pending Approval"; else { oListItem["Status"] = status; if (oListItem["Status"].ToString() == "Approved") { try { // string siteToken; // if (Session["contextToken"] != null) // { DeptCalListClass objDept = new DeptCalListClass(); objDept.AddDeptCal(null, this.lblRequestedBy.Text + "-" + this.lblTimeoffType.Text, this.txtStartDate.Text, this.txtEndDate.Text); // } } catch { } } returnvalue = true; } } } } } //Logged User = Approver3 // if (CurrentApproverLevel == 3) else if (oListItem["Approver3"] != null && oListItem["Approver3Status"].ToString() == "Pending Approval") { if (oListItem["Approver2Status"].ToString() != "Approved") { this.lblerrmsg.Text = " Approver2 : " + lblappr2.Text + " has to process this request prior your actions"; return false; } var docType = oListItem["Approver3"] as Microsoft.SharePoint.Client.FieldLookupValue; string app3 = ""; if (docType != null) app3 = docType == null ? "UnAssigned" : docType.LookupValue; if (CurrentUser == null) LoadUserProfile(); if (CurrentUser.Title != app3) { this.lblerrmsg.Text = " You do not have the access to process this request."; return false; } if (oListItem["Approver1Status"].ToString() == "Approved" && oListItem["Approver2Status"].ToString() == "Approved") { oListItem["Approver3Modified"] = DateTime.Now; oListItem["Approver3Status"] = status; if (status == "Approved") { try { DeptCalListClass objDept = new DeptCalListClass(); objDept.AddDeptCal(null, this.lblRequestedBy.Text + "-" + this.lblTimeoffType.Text, this.txtStartDate.Text, this.txtEndDate.Text); } catch { } } oListItem["Status"] = status; returnvalue = true; } else { this.lblerrmsg.Text = " Approver1 : " + lblappr1.Text + " Or Approver2: " + lblappr2.Text + " has to process this request prior your actions"; return false; } } } oListItem.Update(); } clientContext.ExecuteQuery(); } return returnvalue; } catch { return false; } } return 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()); }