private async Task SyncAllFilesFolders() { try { //*** Set Variables int intCount = 0, intSuccess = 0, intFailed = 0; Session["SyncAllNumbers"] = ""; string strFolderGUID = ""; string strFolderpath = ""; //*** Loop on All Objects on DropBox Grid View foreach (GridViewRow itemRow in grdVWDropBoxFilesFolderList.Rows) { strFolderpath = ""; //*** Refresh Counts intCount += 1; //*** set Session Variable (Shared Variable) Session["SyncAllNumbers"] = grdVWDropBoxFilesFolderList.Rows.Count.ToString() + "," + intCount.ToString() + "," + intSuccess.ToString() + "," + intFailed.ToString(); //*** Check on Drop Box Enity if (bool.Parse(((Label)itemRow.FindControl("lblisFolder")).Text)) //**** If Folder { //************************************ //**** Create Folder on ExactOnline //************************************ //**** Construct Exact Online Class ExactOnlineConnector objExactOnlineConnector = new ExactOnlineConnector(System.Configuration.ConfigurationManager.AppSettings["exactOnlineClientId"], System.Configuration.ConfigurationManager.AppSettings["exactOnlineClientSecret"], System.Configuration.ConfigurationManager.AppSettings["exactOnlineEndPoint"], new Uri(HttpContext.Current.Session["exactOnlineReturnBackURL"].ToString()), Session["ExactOnlineReturnCode"].ToString()); if (Application["ExactOnlineAccessToken"] != null) { objExactOnlineConnector.AccessToken = Application["ExactOnlineAccessToken"].ToString(); } strFolderGUID = objExactOnlineConnector.CreateDocumentFolder(((Label)itemRow.FindControl("lblFileName")).Text, Session["CurrentExactFolderGUID"].ToString()); if (strFolderGUID == "") { //*** If Error returned intFailed += 1; } else { intSuccess += 1; } } else //**** If File { //****************************************************************** //**** Get File Stream then upload it to ExactOnline & Flush //****************************************************************** //*** Construct Parent Folder Path String string strParentFolderpath = ""; if ((List <string>)Session["FolderPath"] != null) { foreach (var item in (List <string>)Session["FolderPath"]) { strParentFolderpath += "/" + item; } } string strPath = strParentFolderpath + "/" + ((Label)itemRow.FindControl("lblFileName")).Text; strFolderpath = strPath; //*** Create Folder Function Stream fnStreamResult = await DropBoxConnector.Download(Application["dropBoxClientObj"], strPath); if (DropBoxConnector.MsgError != "") //*** If error { intFailed += 1; } else { //************************************************************* //*** Convert File to Byte Array and upload it to Exact Online //************************************************************* //**** Construct Exact Online Class ExactOnlineConnector objExactOnlineConnector = new ExactOnlineConnector(System.Configuration.ConfigurationManager.AppSettings["exactOnlineClientId"], System.Configuration.ConfigurationManager.AppSettings["exactOnlineClientSecret"], System.Configuration.ConfigurationManager.AppSettings["exactOnlineEndPoint"], new Uri(HttpContext.Current.Session["exactOnlineReturnBackURL"].ToString()), Session["ExactOnlineReturnCode"].ToString()); if (Application["ExactOnlineAccessToken"] != null) { objExactOnlineConnector.AccessToken = Application["ExactOnlineAccessToken"].ToString(); } strFolderGUID = objExactOnlineConnector.CreateDocumentWithAttachment(((Label)itemRow.FindControl("lblFileName")).Text, Session["CurrentExactFolderGUID"].ToString(), Common.ConvertStreamtoByteArr(fnStreamResult)); if (strFolderGUID == "") { intFailed += 1; } else { intSuccess += 1; } } //****************************************************************** } //*** The Add to update record into DB if (bool.Parse(((Label)itemRow.FindControl("lblisFolder")).Text)) //*** If Folder { if ((List <string>)Session["FolderPath"] != null) { foreach (var item in (List <string>)Session["FolderPath"]) { strFolderpath += "/" + item; } } strFolderpath += "/" + ((Label)itemRow.FindControl("lblFileName")).Text; } FilesDocumentsEntities objFilesDocumentsEntities = new FilesDocumentsEntities(); //*** Check First if File Already exisit into DB DropBoxExactOnline objRecord = objFilesDocumentsEntities.DropBoxExactOnlines.Where(i => i.DropBoxPath == strFolderpath).FirstOrDefault(); if (objRecord != null) { //**** Update DB objRecord.DropBoxPath = strFolderpath; objRecord.ExactOnlineGUID = strFolderGUID; if (bool.Parse(((Label)itemRow.FindControl("lblisFolder")).Text)) { objRecord.isFile = 0; } else { objRecord.isFile = 1; } objFilesDocumentsEntities.SaveChanges(); } else { //*** add to DB DropBoxExactOnline objRecordNew = new DropBoxExactOnline(); objRecordNew.DropBoxPath = strFolderpath; objRecordNew.ExactOnlineGUID = strFolderGUID; if (bool.Parse(((Label)itemRow.FindControl("lblisFolder")).Text)) { objRecordNew.isFile = 0; } else { objRecordNew.isFile = 1; } objFilesDocumentsEntities.DropBoxExactOnlines.Add(objRecordNew); objFilesDocumentsEntities.SaveChanges(); } //******************************************************************************* //*** set Session Variable (Shared Variable) Session["SyncAllNumbers"] = grdVWDropBoxFilesFolderList.Rows.Count.ToString() + "," + intCount.ToString() + "," + intSuccess.ToString() + "," + intFailed.ToString(); } //*** Rebind Exact Online Grid ExactOnlineGridDataBind(Session["CurrentExactFolderGUID"].ToString()); } catch (Exception e) { lblExactOnlineMsg.Text = e.ToString(); //*** Show Error divExactOnlineAlert.Visible = true; } }
//*** Delete Button protected async void lnkbtnDelete_Click(object sender, ImageClickEventArgs e) { //*** Construct Parent Folder Path String string strParentFolderpath = ""; string strFolderName = ""; bool isFolder = false; if ((List <string>)Session["FolderPath"] != null) { foreach (var item in (List <string>)Session["FolderPath"]) { strParentFolderpath += "/" + item; } } //*** Loop on Items to see who is checked to delete foreach (GridViewRow row in grdVWDropBoxFilesFolderList.Rows) { if (((CheckBox)row.FindControl("chkItem")).Checked) { //*** Check if file or Folder isFolder = bool.Parse(((Label)row.FindControl("lblisFolder")).Text); string strPath = strParentFolderpath + "/" + ((Label)row.FindControl("lblFileName")).Text; strFolderName = ((Label)row.FindControl("lblFileName")).Text; //*** Create Folder Function bool fnResult = await DropBoxConnector.DeleteFileOrFolder(Application["dropBoxClientObj"], strPath); if (!fnResult) //*** If error { lblDropBoxMsg.Text = DropBoxConnector.MsgError; //*** Show Error divDropBoxAlert.Visible = true; //*** Exit loop function break; } //**************************************************************** //**** Exact Online Part //**************************************************************** string strFileFolderGUID = ""; if (divExactOnlineFileGrid.Visible) //**** If Exact Online Grid Shown { //*** 1. Get Folder Guid (If not root) if (strFolderName != "") { //*** Fitch Grid to get GUID foreach (GridViewRow itemRow in grdVWExactOnlineFilesFolderList.Rows) { if (isFolder) //**** If Folder { //*** Check if name & Is Folcder if (bool.Parse(((Label)itemRow.FindControl("lblisFolder")).Text) && ((Label)itemRow.FindControl("lblFileName")).Text.ToLower() == strFolderName.ToLower()) { strFileFolderGUID = ((Label)itemRow.FindControl("lblFolderID")).Text; break; } } else { //*** Check if name & Is File if (!bool.Parse(((Label)itemRow.FindControl("lblisFolder")).Text) && ((Label)itemRow.FindControl("lblFileName")).Text.ToLower() == strFolderName.ToLower()) { strFileFolderGUID = ((Label)itemRow.FindControl("lblFolderID")).Text; break; } } } } if (strFileFolderGUID != "") { //*** Create Folder on Exact Online also //**** Construct Exact Online Class ExactOnlineConnector objExactOnlineConnector = new ExactOnlineConnector(System.Configuration.ConfigurationManager.AppSettings["exactOnlineClientId"], System.Configuration.ConfigurationManager.AppSettings["exactOnlineClientSecret"], System.Configuration.ConfigurationManager.AppSettings["exactOnlineEndPoint"], new Uri(HttpContext.Current.Session["exactOnlineReturnBackURL"].ToString()), Session["ExactOnlineReturnCode"].ToString()); if (Application["ExactOnlineAccessToken"] != null) { objExactOnlineConnector.AccessToken = Application["ExactOnlineAccessToken"].ToString(); } if (isFolder) //*** This is Folder { //*** Call Delete Folder if (!objExactOnlineConnector.DeleteDocumentFolder(strFileFolderGUID)) { //*** If Error returned lblExactOnlineMsg.Text = objExactOnlineConnector.MsgError; //*** Show Error divExactOnlineAlert.Visible = true; } } else //**** If File { //**** Call Delete Document if (!objExactOnlineConnector.DeleteDocument(strFileFolderGUID)) { //*** If Error returned lblExactOnlineMsg.Text = objExactOnlineConnector.MsgError; //*** Show Error divExactOnlineAlert.Visible = true; } } } } //*************************************************************************** //*** Then Delete Records from DB FilesDocumentsEntities objFilesDocumentsEntities = new FilesDocumentsEntities(); //*** Check First if File Already exisit into DB DropBoxExactOnline objRecord = objFilesDocumentsEntities.DropBoxExactOnlines.Where(i => i.DropBoxPath == strPath).FirstOrDefault(); if (objRecord != null) { objFilesDocumentsEntities.DropBoxExactOnlines.Remove(objRecord); objFilesDocumentsEntities.SaveChanges(); } } } //*** Rebind Data Grid Again DropBoxGridDataBind(((List <string>)Session["FolderPath"])); }
//*** Upload File Button protected async void Uploader1_UploadCompleted(object sender, UploaderEventArgs[] args) { //*** Construct Parent Folder + File Path String string strUploadedFilePath = ""; string strFolderName = ""; string DocumentGUID = ""; if ((List <string>)Session["FolderPath"] != null) { foreach (var item in (List <string>)Session["FolderPath"]) { strUploadedFilePath += "/" + item; strFolderName = item; } } strUploadedFilePath += "/" + args[0].FileName; //*** Upload File to DropBox Function bool blnUploadResult = await DropBoxConnector.Upload(Application["dropBoxClientObj"], strUploadedFilePath, args[0].OpenStream()); if (!blnUploadResult) //*** If error { lblDropBoxMsg.Text = DropBoxConnector.MsgError; //*** Show Error divDropBoxAlert.Visible = true; //*** Exit from function return; } else //**** Success { //**************************************************************** //**** Exact Online Part //**************************************************************** if (divExactOnlineFileGrid.Visible) //**** If Exact Online Grid Shown { //*** Create Folder on Exact Online also //**** Construct Exact Online Class ExactOnlineConnector objExactOnlineConnector = new ExactOnlineConnector(System.Configuration.ConfigurationManager.AppSettings["exactOnlineClientId"], System.Configuration.ConfigurationManager.AppSettings["exactOnlineClientSecret"], System.Configuration.ConfigurationManager.AppSettings["exactOnlineEndPoint"], new Uri(HttpContext.Current.Session["exactOnlineReturnBackURL"].ToString()), Session["ExactOnlineReturnCode"].ToString()); if (Application["ExactOnlineAccessToken"] != null) { objExactOnlineConnector.AccessToken = Application["ExactOnlineAccessToken"].ToString(); } DocumentGUID = objExactOnlineConnector.CreateDocumentWithAttachment(args[0].FileName, Session["CurrentExactFolderGUID"].ToString(), Common.ConvertStreamtoByteArr(args[0].OpenStream())); if (DocumentGUID == "") { //*** If Error returned lblExactOnlineMsg.Text = objExactOnlineConnector.MsgError; //*** Show Error divExactOnlineAlert.Visible = true; } else { //*** Add Entity to DB FilesDocumentsEntities objFilesDocumentsEntities = new FilesDocumentsEntities(); //*** Check First if File Already exisit into DB DropBoxExactOnline objRecord = objFilesDocumentsEntities.DropBoxExactOnlines.Where(i => i.DropBoxPath == strUploadedFilePath).FirstOrDefault(); if (objRecord != null) { //**** Update DB objRecord.DropBoxPath = strUploadedFilePath; objRecord.ExactOnlineGUID = DocumentGUID; objRecord.isFile = 1; objFilesDocumentsEntities.SaveChanges(); } else { //*** add to DB DropBoxExactOnline objRecordNew = new DropBoxExactOnline(); objRecordNew.DropBoxPath = strUploadedFilePath; objRecordNew.ExactOnlineGUID = DocumentGUID; objRecordNew.isFile = 1; objFilesDocumentsEntities.DropBoxExactOnlines.Add(objRecordNew); objFilesDocumentsEntities.SaveChanges(); } } } //*************************************************************************** //*** Rebind Data Grid Again DropBoxGridDataBind(((List <string>)Session["FolderPath"])); } }
//**** Create Folder Button protected async void btnPanel2Yes_Click(object sender, EventArgs e) { //*** Check First if Folder Exist/ Fitsh foreach (GridViewRow row in grdVWDropBoxFilesFolderList.Rows) { if (((Label)row.FindControl("lblFileName")).Text.ToLower() == txtFolderName.Text.ToLower() && bool.Parse(((Label)row.FindControl("lblisFolder")).Text.ToLower())) { //*** Folder Already Exist lblDropBoxMsg.Text = "Folder already exists with same name"; //*** Show Error with grid divDropBoxAlert.Visible = true; DivDropBoxFileGrid.Visible = true; //*** Exit from function return; } } //*** Construct Parent Folder Path String string strFolderpath = ""; string strFolderName = ""; if ((List <string>)Session["FolderPath"] != null) { foreach (var item in (List <string>)Session["FolderPath"]) { strFolderpath += "/" + item; strFolderName = item; } } strFolderpath += "/" + txtFolderName.Text; //*** Create Folder Function bool fnResult = await DropBoxConnector.CreateFolder(Application["dropBoxClientObj"], strFolderpath); if (!fnResult) //*** If error { lblDropBoxMsg.Text = DropBoxConnector.MsgError; //*** Show Error divDropBoxAlert.Visible = true; } else { //**************************************************************** //**** Exact Online Part //**************************************************************** if (divExactOnlineFileGrid.Visible) //**** If Exact Online Grid Shown { //*** Create Folder on Exact Online also //**** Construct Exact Online Class ExactOnlineConnector objExactOnlineConnector = new ExactOnlineConnector(System.Configuration.ConfigurationManager.AppSettings["exactOnlineClientId"], System.Configuration.ConfigurationManager.AppSettings["exactOnlineClientSecret"], System.Configuration.ConfigurationManager.AppSettings["exactOnlineEndPoint"], new Uri(HttpContext.Current.Session["exactOnlineReturnBackURL"].ToString()), Session["ExactOnlineReturnCode"].ToString()); if (Application["ExactOnlineAccessToken"] != null) { objExactOnlineConnector.AccessToken = Application["ExactOnlineAccessToken"].ToString(); } string strFolderGUID = objExactOnlineConnector.CreateDocumentFolder(txtFolderName.Text, Session["CurrentExactFolderGUID"].ToString()); if (strFolderGUID == "") { //*** If Error returned lblExactOnlineMsg.Text = objExactOnlineConnector.MsgError; //*** Show Error divExactOnlineAlert.Visible = true; } else //*** Add Entry To DB { FilesDocumentsEntities objFilesDocumentsEntities = new FilesDocumentsEntities(); DropBoxExactOnline objRecord = new DropBoxExactOnline(); objRecord.DropBoxPath = strFolderpath; objRecord.ExactOnlineGUID = strFolderGUID; objRecord.isFile = 0; objFilesDocumentsEntities.DropBoxExactOnlines.Add(objRecord); objFilesDocumentsEntities.SaveChanges(); } } //*************************************************************************** //*** Success & Rebind Data Grid Again & Initialize txtFolderName.Text = ""; DropBoxGridDataBind(((List <string>)Session["FolderPath"])); } }