/// <summary> /// User elected to continue, upload the Create and Update list of files /// </summary> /// <param name="userCanceled"></param> /// <param name="createTemplate"></param> /// <param name="updateTemplate"></param> /// <param name="args"></param> private void UploadSummary_OnComplete(bool userCanceled, List <FileUpload> createTemplate, List <FileUpload> updateTemplate, EventArgs args) { _uploadSummary.Dispose(); _uploadSummary = null; ParentForm.Resize -= ParentForm_Resize; // disable the main UI so they don't monkey with stuff ToggleMainControlsEnabled(true); if (!userCanceled) { WorkAsync(new WorkAsyncInfo { Message = "Uploading selected Documents", Work = (w, e) => { // read the file from disk try { var errorMessages = new StringBuilder(); var counter = 1; var templateCount = updateTemplate.Count + createTemplate.Count; // make the content updates // don't use bulk update to trap errors and not overload the request foreach (var file in updateTemplate) { var request = new UpdateRequest() { Target = new Entity() { Id = file.TemplateId, LogicalName = "documenttemplate", EntityState = EntityState.Changed } }; request.Target.Attributes["content"] = Convert.ToBase64String(file.FileContents); w.ReportProgress(Convert.ToInt32(counter++ / templateCount) * 100, "Updating template: " + file.TemplateName); // gather up error messages and continue try { Service.Execute(request); } catch (Exception ex) { errorMessages.AppendFormat("Error updating template: {0}, Error Message: {1}\n", file.FileName, ex.Message); } } foreach (var file in createTemplate) { var request = new CreateRequest() { Target = new Entity() { LogicalName = "documenttemplate", EntityState = EntityState.Created } }; request.Target.Attributes["name"] = file.TemplateName; request.Target.Attributes["documenttype"] = new OptionSetValue(file.TemplateTypeValue); request.Target.Attributes["description"] = "New " + file.TemplateType + " template. Source file: " + file.FileName; request.Target.Attributes["content"] = Convert.ToBase64String(file.FileContents); w.ReportProgress(Convert.ToInt32(counter++ / templateCount) * 100, "Creating template: " + file.TemplateName); // gather up error messages and continue try { Service.Execute(request); } catch (Exception ex) { errorMessages.AppendFormat("Error creating template: {0}, Error Message: {1}\n", file.FileName, ex.Message); } } // if we have error messages, show them the error of their ways if (errorMessages.Length > 0) { MessageBox.Show("The following errors occured attempting to upload the files\n" + errorMessages.ToString(), "Errors During Upload", MessageBoxButtons.OK); } } catch (Exception ex) { MessageBox.Show("An error occured uploading the files\n" + ex.Message); } }, ProgressChanged = e => { // If progress has to be notified to user, use the following method: SendMessageToStatusBar?.Invoke(this, new StatusBarMessageEventArgs(e.ProgressPercentage, e.UserState.ToString())); }, PostWorkCallBack = e => { LoadDocumentTemplates(); }, AsyncArgument = null, IsCancelable = true, MessageWidth = 340, MessageHeight = 150 }); } }
/// <summary> /// Upload multiple document template files /// </summary> private void PerformUploadMultiple() { // allow user to select one or more files using (var openDlg = new OpenFileDialog() { Filter = Constants.GetFileOpenFilter(), FilterIndex = 1, RestoreDirectory = true, Multiselect = true }) { if (openDlg.ShowDialog() == DialogResult.OK) { var templateNameList = new List <string>(); var templateList = new List <DocumentTemplateEdit>(); // grab the list of existing documents from the list view. foreach (ListViewItem listItem in listViewDocumentTemplates.Items) { var template = (DocumentTemplateEdit)listItem.Tag; templateNameList.Add(template.Name.ToLower() + "_" + template.Type); templateList.Add(template); } var createTemplate = new List <FileUpload>(); var updateTemplate = new List <FileUpload>(); var ignoreTemplate = new List <FileUpload>(); // iterate on the list of files. match each file name and extension to the template name and type foreach (var file in openDlg.FileNames) { var fileUpload = new FileUpload(file, Service); // if the file extension is not .docx nor .xlsx, ignore if (fileUpload.IsIgnored) { ignoreTemplate.Add(fileUpload); } else { var key = fileUpload.TemplateName.ToLower() + "_" + fileUpload.TemplateType; var foundItems = templateNameList.FindAll(s => s == key); // match on the file name and type if (foundItems.Count == 0) { // if the template is not found, then create createTemplate.Add(fileUpload); } else { // only one instance if (foundItems.Count == 1) { var template = templateList.Find(t => t.Name.ToLower() == fileUpload.TemplateName.ToLower()); fileUpload.TemplateId = template.Id; fileUpload.Note = "Update Template with id: " + template.Id.ToString("b"); updateTemplate.Add(fileUpload); } else { fileUpload.IsIgnored = true; fileUpload.Note = "Duplicate template name found"; ignoreTemplate.Add(fileUpload); } } } } // present a summary, allow user to confirm/cancel _uploadSummary = new UploadMultipleSummary(Service, createTemplate, updateTemplate, ignoreTemplate) { Parent = this }; ResizeSummaryScreen(); ParentForm.Resize += ParentForm_Resize; // disable the main UI so they don't monkey with stuff ToggleMainControlsEnabled(false); // handle the on complete event so we can reload and clean up control _uploadSummary.OnComplete += UploadSummary_OnComplete; _uploadSummary.BringToFront(); _uploadSummary.Show(); } } }