Exemple #1
0
        public void Upload()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new UploadDelegate(Upload));
            }
            else
            {
                ErrorLabel.Visible = false;

                ProgressValueLabel.Text = "Started.";
                ProgressValueLabel.Refresh();
                SetButtonEnabilityOnUploading();
                int            totalRows = EmailsListView.Items.Count;
                List <EUField> fields    = SharePointManager.GetFields(_siteSetting, _webURL, _listName);
                SharePointCopyWS.CopyResult[] myCopyResultArray = null;
                string log      = String.Empty;
                bool   hasError = false;
                if (_isListItemAndAttachmentMode == false)
                {
                    for (int i = 0; i < EmailsListView.Items.Count; i++)
                    {
                        if (backgroundWorker1.CancellationPending == true)
                        {
                            ProgressValueLabel.Text = "Cancelled.";
                            ProgressValueLabel.Refresh();
                            return;
                        }
                        ListViewItem listViewItem = EmailsListView.Items[i];
                        if (listViewItem.Checked == false)
                        {
                            continue;
                        }
                        EUEmailMetaData metaData    = listViewItem.Tag as EUEmailMetaData;
                        string          filename    = listViewItem.SubItems[1].Text;
                        string          newFilename = listViewItem.Text;
                        ProgressValueLabel.Text = newFilename + " is being uploaded...";
                        ProgressValueLabel.Refresh();
                        string filePath      = _sourceFolder + "\\" + filename;
                        byte[] itemByteArray = SharePointManager.ReadByteArrayFromFile(filePath);
                        string sourceDest    = _destinationFolderUrl + "/" + newFilename;

                        string[]   sourceDests = new string[] { sourceDest };
                        EUListItem listItem;
                        SharePointManager.UploadFile(_siteSetting, _listName, _rootFolderPath, _siteURL, _webURL, newFilename, sourceDests, itemByteArray, fields, metaData, _fieldInfoArray, out listItem);

                        /*
                         * foreach (SharePointCopyWS.CopyResult copyResult in myCopyResultArray)
                         * {
                         *  if (copyResult.ErrorCode != Sobiens.Office.SharePointOutlookConnector.SharePointCopyWS.CopyErrorCode.Success)
                         *      hasError = true;
                         *  log += "DestinationUrl:" + copyResult.DestinationUrl + Environment.NewLine + "ErrorCode:" + copyResult.ErrorCode.ToString() + Environment.NewLine + "ErrorMessage:" + copyResult.ErrorMessage + Environment.NewLine;
                         * }
                         * LogManager.Log(log, EULogModes.Normal);
                         */
                        SetProgressBar(i, totalRows);
                    }
                }
                else
                {
                    Hashtable emails = new Hashtable();

                    for (int i = 0; i < EmailsListView.Items.Count; i++)
                    {
                        ListViewItem listViewItem = EmailsListView.Items[i];
                        //DataGridViewRow dataGridViewRow = EmailsDataGridView.Rows[i];
                        //bool included = (bool)dataGridViewRow.Cells["Included"].Value;
                        if (listViewItem.Checked == false)
                        {
                            continue;
                        }
                        Microsoft.Office.Interop.Outlook.MailItem emailItem = listViewItem.Tag as Microsoft.Office.Interop.Outlook.MailItem;
                        if (emails[emailItem.EntryID] == null)
                        {
                            emails[emailItem.EntryID] = new List <ListViewItem>();
                        }
                        ((List <ListViewItem>)emails[emailItem.EntryID]).Add(listViewItem);
                    }
                    totalRows = emails.Count;
                    int currentIndex = 0;
                    foreach (object attachmentsObject in emails.Values)
                    {
                        if (backgroundWorker1.CancellationPending == true)
                        {
                            ProgressValueLabel.Text = "Cancelled.";
                            ProgressValueLabel.Refresh();
                            return;
                        }
                        List <ListViewItem> attachments   = (List <ListViewItem>)attachmentsObject;
                        ListViewItem        attachmentRow = attachments[0];
                        Microsoft.Office.Interop.Outlook.MailItem emailItem = attachmentRow.Tag as Microsoft.Office.Interop.Outlook.MailItem;
                        string body = emailItem.Body;
                        int    id   = SharePointManager.CreateListItem(_siteSetting, _rootFolderPath, _webURL, _listName, emailItem.Subject, body);

                        foreach (ListViewItem attachmentRow1 in attachments)
                        {
                            //string filename = attachmentRow1.Cells["Filename"].Value.ToString();
                            //string newFilename = attachmentRow1.Cells["NewFilename"].Value.ToString();
                            string filename    = attachmentRow1.SubItems[0].Text;
                            string newFilename = attachmentRow1.Text;
                            ProgressValueLabel.Text = newFilename + " is being uploaded...";
                            ProgressValueLabel.Refresh();
                            string filePath      = _sourceFolder + "\\" + filename;
                            byte[] itemByteArray = SharePointManager.ReadByteArrayFromFile(filePath);
                            string result        = SharePointManager.AddAttachment(_siteSetting, _webURL, _listName, id, newFilename, itemByteArray);

                            log += "WebURL:" + _webURL + Environment.NewLine + "ListName:" + _listName + Environment.NewLine + "filePath:" + filePath + Environment.NewLine + "newFilename:" + newFilename + Environment.NewLine + "result:" + result + Environment.NewLine;
                            LogManager.Log(log, EULogModes.Normal);
                        }
                        SetProgressBar(currentIndex, totalRows);
                        currentIndex++;
                    }
                }
                CurrentUploadLog = log;
                if (hasError == true)
                {
                    ErrorLabel.Visible = true;
                }
            }
        }