Ejemplo n.º 1
0
        private void CreateTask(Outlook.MailItem mail)
        {
            string tempPath = Path.GetTempPath();
            string mailTempPath = Path.Combine(tempPath, MakeValidFileName(mail.Subject) + ".msg");

            try
            {
                if (File.Exists(mailTempPath))
                    File.Delete(mailTempPath);

                mail.SaveAs(mailTempPath, Outlook.OlSaveAsType.olMSG);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error saving Mail content", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            try
            {
                using (var tfs = new TfsTeamProjectCollection(new Uri(Settings.Default.TFSUrl)))
                {
                    WorkItemStore wis = tfs.GetService(typeof(WorkItemStore)) as WorkItemStore;

                    var projectQuery = from prj in wis.Projects.Cast<Project>()
                                       where prj.HasWorkItemWriteRights
                                       select prj.Name;

                    var projectForm = new SelectProjectForm(projectQuery);

                    try
                    {
                        var pjResult = projectForm.ShowDialog();

                        if (pjResult != DialogResult.OK)
                            return;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error selecting Team Project", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    WorkItem wi = null;

                    try
                    {
                        var project = wis.Projects[projectForm.SelectedProject] as Project;

                        var tasktype = project.WorkItemTypes["Task"];
                        wi = new WorkItem(tasktype);

                        wi.Description = mail.Body;
                        wi.Reason = "New";
                        wi.Title = mail.Subject;

                        wi.Attachments.Add(new Attachment(mailTempPath, "Mail"));

                        foreach (Outlook.Attachment attachment in mail.Attachments)
                        {
                            string fileName = attachment.FileName;
                            int i = 1;

                            while (wi.Attachments.Cast<Attachment>().Where(a => a.Name == fileName).Count() > 0)
                                fileName = string.Format("{0}_{1}.{2}", Path.GetFileNameWithoutExtension(attachment.FileName), i++, Path.GetExtension(attachment.FileName));

                            string attachmentPath = Path.Combine(tempPath, fileName);

                            if (File.Exists(attachmentPath))
                                File.Delete(attachmentPath);

                            attachment.SaveAsFile(attachmentPath);

                            wi.Attachments.Add(new Attachment(attachmentPath, string.Format("Mail Attachment: {0}", attachment.DisplayName)));
                        }

                        wi.IterationPath = project.Name;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error creating Task", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    try
                    {
                        var wiForm = new WorkItemForm(wi);
                        var wiResult = wiForm.ShowDialog();

                        if (wiResult == DialogResult.OK)
                            wi.Save();

                        wi.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error saving Task", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error connecting to TFS", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Closes this WorkItem instance and frees memory that is associated with it.
 /// </summary>
 public void Close()
 {
     _item.Close();
 }