Example #1
0
        public int AddWorkItem(MailMessage mail)
        {
            var workItemType = workItemStore.Projects[projectName].WorkItemTypes[workItemTypeName];

            string wiql = string.Format(
                @"SELECT [System.Id], [System.WorkItemType], [System.Title]
FROM WorkItems
WHERE [System.TeamProject] = '{0}'
AND  [System.WorkItemType] = '{1}'
AND  [System.Title] = '{2}'
AND  [System.HyperLinkCount] >= 1
AND  [System.AttachedFileCount] >= 1"
                , projectName
                , workItemTypeName
                , mail.Subject);
            var queryResult = workItemStore.Query(wiql);

            if (queryResult.Count > 0)
            {
                int existingId = queryResult[0].Id;
                logger.AWorkItemAlreadyExistsFor(existingId, mail);
                return(0);
            }

            var workItem = new WorkItem(workItemType);

            workItem.Open();
            workItem.Title       = mail.Subject;
            workItem.Description = mail.Body;
            // HACK this is to avoid pocessing the same mail twice
            var link = new Hyperlink("mailto:" + mail.MessageId);

            workItem.Links.Add(link);

            Attachment originalMailAsAttachment = new Attachment(mail.LocalDumpFile, "Original email");

            workItem.Attachments.Add(originalMailAsAttachment);

            foreach (var mailAttachment in mail.Attachments)
            {
                Attachment newAttachment = new Attachment(mailAttachment.LocalDumpFile, "Attachment from email");
                workItem.Attachments.Add(newAttachment);
            }//for

            if (!workItem.IsValid())
            {
                logger.InvalidWorkItem(workItem);
            }
            else
            {
                logger.SavingWorkItem(workItem);
                workItem.Save();
                logger.WorkItemSaved(workItem);
            }

            return(workItem.Id);
        }