/// <summary> /// Load all of the system document templates from the current connection /// </summary> private void LoadDocumentTemplates(Action onCompleteCallback) { WorkAsync(new WorkAsyncInfo { Message = "Retrieving the list of Document Templates", Work = (w, e) => { try { w.ReportProgress(0, "Loading Templates from CRM"); e.Result = DocumentTemplateEdit.GetDocumentTemplates(Service); } catch (Exception ex) { MessageBox.Show(ex.Message); } }, ProgressChanged = e => { SendMessageToStatusBar?.Invoke(this, new StatusBarMessageEventArgs(e.ProgressPercentage, e.UserState.ToString())); }, PostWorkCallBack = e => { var templates = e.Result as List <DocumentTemplateEdit>; // load the templates LoadTemplatesComplete(templates); // now update the UI with the new selection UpdateControlsForSelection(); // invoke the callback if passed onCompleteCallback?.Invoke(); }, AsyncArgument = null, IsCancelable = true, MessageWidth = 340, MessageHeight = 150 }); }
/// <summary> /// Download the selected templates /// </summary> /// <param name="saveFolder"></param> private void DownloadDocumentTemplates(string saveFolder) { var templateIds = new List <Guid>(); // grab the list of selected template Ids foreach (ListViewItem item in this.listViewDocumentTemplates.SelectedItems) { var template = item.Tag as DocumentTemplateEdit; templateIds.Add(template.Id); } WorkAsync(new WorkAsyncInfo { Message = "Retrieving the Document Template Content...", Work = (w, e) => { try { var renameCount = 1; var templateIdList = e.Argument as List <Guid>; // retrieve the doc template contents var templates = DocumentTemplateEdit.GetDocumentTemplates(Service, templateIdList, true); byte[] bytesToSave; var downloadFileName = ""; Dictionary <string, byte[]> files = new Dictionary <string, byte[]>(); var fileCount = templates.Count; foreach (var template in templates) { var folder = template.Status + "\\" + template.Type + "\\"; var fileName = template.FileName; var ext = Path.GetExtension(template.FileName); // if we have more than one file, add a folder for the status if (fileCount > 1) { fileName = folder + fileName; } renameCount = 1; while (files.ContainsKey(fileName)) { if (fileCount > 1) { fileName = folder + template.Name + " (" + (renameCount++).ToString() + ")" + ext; } else { fileName = template.Name + " (" + (renameCount++).ToString() + ")" + ext; } } // add the file to the list for download. files.Add(fileName, template.Content); } // if only one file was selected, save just that... otherwise, package things up in a zip! if (files.Count > 1) { var zipper = new ZipBuilder(); downloadFileName = string.Format("Document Templates - {0:M-d-yyyy HH-mm}.zip", DateTime.Now); bytesToSave = zipper.zipBytes(files); } else { var file = files.First(); bytesToSave = file.Value; downloadFileName = file.Key; } // append the selected folder... downloadFileName = Path.Combine(saveFolder, downloadFileName); renameCount = 1; // delete file if it exists while (File.Exists(downloadFileName)) { downloadFileName = Path.GetFileNameWithoutExtension(downloadFileName) + " (" + (renameCount++).ToString() + ")" + Path.GetExtension(downloadFileName); downloadFileName = Path.Combine(saveFolder, downloadFileName); } // write using (var saveMe = new FileStream(downloadFileName, FileMode.OpenOrCreate, FileAccess.Write)) { saveMe.Write(bytesToSave, 0, bytesToSave.Length); saveMe.Close(); } } catch (Exception ex) { MessageBox.Show("The following error occurred attempting to download your templates.\n" + ex.Message); } }, ProgressChanged = e => { // If progress has to be notified to user, use the following method: // SetWorkingMessage("Downloading your templates now..."); }, PostWorkCallBack = e => { }, AsyncArgument = templateIds, IsCancelable = true, MessageWidth = 340, MessageHeight = 150 }); }
public EditTemplateControl(IOrganizationService service, DocumentTemplateEdit docTemplate) { _docTemplate = docTemplate; _service = service; InitializeComponent(); }