private WorkItem MapWorkItem(Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem workItem)
        {
            var mappedItem = new WorkItem
            {
                Id = workItem.Id,
                AssignedTo = workItem.Fields[CoreField.AssignedTo].Value.ToString(),
                Description = workItem.Fields[CoreField.Description].Value.ToString(),
                Title = workItem.Fields[CoreField.Title].Value.ToString(),
                Type =
                    (workItem.Type.Name == "Bug" ? WorkItemType.Bug : WorkItemType.Requirement),
                Url = workItem.Uri.ToString(),
                ExtendedProperties =  new Dictionary<string, object>(),
                Migrated = false
            };

            mappedItem.ExtendedProperties.Add("Attachments", workItem.Attachments);
            mappedItem.ExtendedProperties.Add("Links", workItem.Links);
            mappedItem.ExtendedProperties.Add("Example.CustomField", workItem.Fields["Example.CustomField"].Value);

            return mappedItem;
        }
        public void Migrate(WorkItem item)
        {
            using (var collection = new TfsTeamProjectCollection(locator.Location))
            {
                var workItemStore = collection.GetService<WorkItemStore>();
                var workItemType = workItemStore.Projects[ProjectName].WorkItemTypes[RequirementType];

                if (item.Type == WorkItemType.Bug)
                {
                    workItemType = workItemStore.Projects[ProjectName].WorkItemTypes[BugType];
                }

                var targetWorkItem = new Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem(workItemType);
                targetWorkItem.Fields[CoreField.AssignedTo].Value = item.AssignedTo;
                targetWorkItem.Fields[CoreField.Description].Value = item.Description;
                targetWorkItem.Fields[CoreField.Title].Value = String.Format(WorkItemTitleFormat, item.Id,
                                                                             item.Title);

                if (item.Type == WorkItemType.Bug)
                {
                    targetWorkItem.Fields["Microsoft.VSTS.TCM.ReproSteps"].Value = item.Description;
                    targetWorkItem.Fields["Example.CustomField"].Value = item.ExtendedProperties["Example.CustomField"] == null ? string.Empty : item.ExtendedProperties["PageCode"].ToString();
                }

                var attachments = item.ExtendedProperties["Attachments"] as AttachmentCollection;
                var attachmentsToClean = new List<string>();
                if (attachments != null)
                {
                    var downloadClient = new WebClient { UseDefaultCredentials = true };
                    var tempDownloadPath = Path.GetTempPath();

                    foreach (var existingAttachment in attachments.Cast<Attachment>())
                    {
                        var tempFile = Path.Combine(tempDownloadPath, existingAttachment.Name);
                        downloadClient.DownloadFile(existingAttachment.Uri, tempFile);

                        var attachmentComment = string.IsNullOrWhiteSpace(existingAttachment.Comment)
                                          ? existingAttachment.Comment
                                          : string.Format("Migrated from work item {0}", item.Id);
                        var clonedAttachment = new Attachment(tempFile, attachmentComment);
                        targetWorkItem.Attachments.Add(clonedAttachment);
                        attachmentsToClean.Add(tempFile);
                    }
                }

                var links = item.ExtendedProperties["Links"] as LinkCollection;
                if (links != null)
                {
                    foreach (var linkCopy in links.Cast<Link>()
                                        .Select(link => CreateShallowCopy(link, workItemStore))
                                        .Where(link => link != null))
                    {
                        targetWorkItem.Links.Add(linkCopy);
                        targetWorkItem.Links.Add(new RelatedLink(item.Id));
                    }
                }

                if (targetWorkItem.IsValid())
                {
                    targetWorkItem.Save();

                    foreach (var filePath in attachmentsToClean)
                    {
                        File.Delete(filePath);
                    }

                    return;
                }

                throw new ArgumentException(
                    string.Format(
                        "The work item provided was not valid for migration. The following fields have issues: {0}",
                        targetWorkItem.Validate().Cast<string>()));
            }
        }
 public string MigrateTo(WorkItem item)
 {
     throw new NotImplementedException("This repository is not valid for the migrate action");
 }
        private WorkItem MapWorkItem(Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem workItem)
        {
            var mappedItem = new WorkItem
            {
                Id = workItem.Id,
                Provider = FriendlyName,
                AssignedTo = workItem.Fields[CoreField.AssignedTo].Value.ToString(),
                Description = workItem.Fields[CoreField.Description].Value.ToString(),
                Title = workItem.Fields[CoreField.Title].Value.ToString(),
                Type =
                    (workItem.Type.Name == "Bug" ? WorkItemType.Bug : WorkItemType.Requirement),
                Url = workItem.Uri.ToString(),
                ExtendedProperties = new Dictionary<string, object>(),
                Migrated = false
            };

            mappedItem.ExtendedProperties.Add("Attachments", workItem.Attachments);
            mappedItem.ExtendedProperties.Add("Links", workItem.Links);

            if (workItem.Fields.Contains("StudyGlobal.Division"))
            {
                mappedItem.ExtendedProperties.Add("Division", workItem.Fields["StudyGlobal.Division"].Value);
            }

            if (workItem.Fields.Contains("Microsoft.VSTS.Scheduling.Size"))
            {
                mappedItem.ExtendedProperties.Add("Size", workItem.Fields["Microsoft.VSTS.Scheduling.Size"].Value);
            }

            if (workItem.Fields.Contains("StudyGlobal.PageCode"))
            {
                mappedItem.ExtendedProperties.Add("PageCode", workItem.Fields["StudyGlobal.PageCode"].Value);
            }

            return mappedItem;
        }
        public string MigrateTo(WorkItem item)
        {
            var locator = serviceLocatorSelector.GetByName("TfsServiceLocator");
            using (var collection = new TfsTeamProjectCollection(locator.Location))
            {
                var workItemStore = collection.GetService<WorkItemStore>();
                var workItemType = workItemStore.Projects[ProjectName].WorkItemTypes[RequirementType];

                if (item.Type == WorkItemType.Bug)
                {
                    workItemType = workItemStore.Projects[ProjectName].WorkItemTypes[BugType];
                }

                var targetWorkItem = new Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem(workItemType);
                targetWorkItem.Fields[CoreField.AssignedTo].Value = item.AssignedTo;
                targetWorkItem.Fields[CoreField.Description].Value = item.Description;
                targetWorkItem.Fields[CoreField.Title].Value = String.Format(WorkItemTitleFormat, item.Id,
                                                                             item.Title);

                if (item.ExtendedProperties.ContainsKey("Division"))
                {
                    targetWorkItem.Fields["StudyGlobal.Division"].Value = item.ExtendedProperties["Division"].ToString();
                }

                if (item.ExtendedProperties.ContainsKey("Size"))
                {
                    targetWorkItem.Fields["Microsoft.VSTS.Scheduling.Size"].Value =
                        item.ExtendedProperties["Size"].ToString();
                }

                if (item.Type == WorkItemType.Bug)
                {
                    targetWorkItem.Fields["Microsoft.VSTS.TCM.ReproSteps"].Value = item.Description;
                    targetWorkItem.Fields["StudyGlobal.PageCode"].Value = item.ExtendedProperties["PageCode"] == null
                                                                                ? string.Empty
                                                                                : item.ExtendedProperties["PageCode"].ToString();
                }

                var attachments = item.ExtendedProperties["Attachments"] as AttachmentCollection;
                var attachmentsToClean = new List<string>();
                if (attachments != null)
                {
                    ShallowCloneAttachments(item, attachments, targetWorkItem, attachmentsToClean);
                }

                if (targetWorkItem.IsValid())
                {
                    targetWorkItem.Save();

                    foreach (var filePath in attachmentsToClean)
                    {
                        File.Delete(filePath);
                    }

                    return targetWorkItem.Id.ToString(CultureInfo.InvariantCulture);
                }

                throw new ArgumentException(
                    string.Format(
                        "The work item provided was not valid for migration. The following fields have issues: {0}",
                        targetWorkItem.Validate().Cast<string>()));
            }
        }
        private static void ShallowCloneAttachments(WorkItem item, AttachmentCollection attachments, Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem targetWorkItem,
            ICollection<string> attachmentsToClean)
        {
            var downloadClient = new WebClient {UseDefaultCredentials = true};
            var tempDownloadPath = Path.GetTempPath();

            foreach (var existingAttachment in attachments.Cast<Attachment>())
            {
                var tempFile = Path.Combine(tempDownloadPath, existingAttachment.Name);
                downloadClient.DownloadFile(existingAttachment.Uri, tempFile);

                var attachmentComment = string.IsNullOrWhiteSpace(existingAttachment.Comment)
                                            ? existingAttachment.Comment
                                            : string.Format("Migrated from work item {0}", item.Id);
                var clonedAttachment = new Attachment(tempFile, attachmentComment);
                targetWorkItem.Attachments.Add(clonedAttachment);
                attachmentsToClean.Add(tempFile);
            }
        }