我正在尝试在 Outlook 中创建一个新按钮,该按钮查找任务 ID,获取连接的 PBI 并将邮件项添加为附件。
非常简单:
public void mnuAttachToPBI_Clicked(Office.IRibbonControl control)
{
Microsoft.Office.Interop.Outlook.Selection sel = Global.CurrentApplication.ActiveExplorer().Selection;
if (sel != null && sel.Count > 0)
{
string subj;
foreach (MailItem mailItem in sel)
{
subj = mailItem.Subject.Substring(mailItem.Subject.IndexOf('['));
subj = subj.Substring(0, subj.IndexOf(']'));
subj = subj.Replace("[", String.Empty);
subj = subj.Replace("]", string.Empty);
int taskId;
if (Int32.TryParse(subj, out taskId))
{
WorkItem wi = TfsHandler.GetPbi(taskId);
if (wi != null)
{
string fileName = Path.GetInvalidFileNameChars().Aggregate(mailItem.Subject + ".msg", (current, c) => current.Replace(c.ToString(), "_"));
string path = Path.Combine(Path.GetTempPath(), fileName);
mailItem.SaveAs(path, OlSaveAsType.olMSG);
wi.Attachments.Add(new Microsoft.TeamFoundation.WorkItemTracking.Client.Attachment(path));
wi.Save();
wi.Close();
File.Delete(path);
}
}
}
}
}
静态 TFS 处理程序类如下所示:
private static List<WorkItem> _allWorkItems;
private const string ITEM_TYPE_NAME_PBI = "PBI";
private const string ITEM_TYPE_NAME_TASK = "Task";
private static List<WorkItem> AllWorkItems
{
get
{
if (_allWorkItems == null)
{
WorkItemCollection wc = new WorkItemStore(TFS_SRV).Query(
" SELECT [System.Id], [System.WorkItemType]," +
" [System.State], [System.AssignedTo], [System.Title] " +
" FROM WorkItems");
_allWorkItems = new List<WorkItem>();
foreach (WorkItem w in wc)
_allWorkItems.Add(w);
}
return _allWorkItems;
}
}
/// <summary>
/// Gets the PBI, if the workitem is a task id, it checks parents
/// </summary>
/// <param name="workItemId"></param>
/// <returns></returns>
public static WorkItem GetPbi(int workItemId)
{
WorkItem result = AllWorkItems.FirstOrDefault(f => f.Id == workItemId);
if (result.Type.Name == ITEM_TYPE_NAME_TASK)
{
result = AllWorkItems.FirstOrDefault(f => f.Id == result.WorkItemLinks[0].TargetId);
}
return result;
}
工作正常:我检查邮件项目标题中是否有 [ID],获取任务,如果是任务,我获取 PBI,将邮件另存为附件并添加。
但是如果我尝试从 Outlook 打开 PBI,我会得到:
团队基础错误
跨线程操作无效:控件''从创建它的线程以外的线程访问。
好的
由于我正在关闭并保存工作项,即 PBI,我真的希望这能处理这些事情。
我没有为这个问题找到任何好的解决方案,而且由于我是一个初学者,我没有自己看到这个问题的知识。
我的网站是否有错误,或者我的方法根本不可能?