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) { 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>())); } }