如何在 C# 中获取 TFS 工作项的“分配给”电子邮件 ID

本文关键字:分配 分配给 ID 电子邮件 获取 工作 TFS | 更新日期: 2023-09-27 18:30:41

我使用以下代码来获取工作项及其属性。

public DataTable GetBugLogData(Uri tfsUri)
    {            
        string tfsPrrojectName = ConfigurationManager.AppSettings["tfsPrrojectName"].ToString();
        string tfsAreaPath = ConfigurationManager.AppSettings["tfsAreaPath"].ToString();            
        string workItemQuery = String.Format(@"SELECT * FROM WorkItems WHERE [System.TeamProject] = '{0}' AND [Work Item Type] = 'Bug'  AND [State] = 'Active'   AND [Area Path] = '{1}'ORDER BY [Assigned To]", tfsPrrojectName, tfsAreaPath);
        TfsTeamProjectCollection projCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(tfsUri);
        WorkItemStore WIS = (WorkItemStore)projCollection.GetService(typeof(WorkItemStore));
        WorkItemCollection WIC = WIS.Query(workItemQuery);
        DataTable workItemsTable = new DataTable();
        workItemsTable.Columns.AddRange(new DataColumn[6] 
                      { new DataColumn("Id", typeof(int)),
                        new DataColumn("Title", typeof(string)),
                        new DataColumn("Created By",typeof(string)),
                        new DataColumn("State",typeof(string)),
                        new DataColumn("Assigned To",typeof(string)),
                        new DataColumn("Type",typeof(string))           
        }); 
        foreach (WorkItem wi in WIC)
        {                
            workItemsTable.Rows.Add(wi.Id, wi.Title, wi.CreatedBy.ToString(), wi.State.ToString(), (wi.Fields["Assigned To"].Value).ToString(), wi.Type.Name.ToString());                
        }
        workItemsTable.DefaultView.Sort = "[Assigned To]";
        return workItemsTable;
    }

现在我的要求是获得分配给电子邮件 ID,以便我可以通过向他的电子邮件地址发送邮件来通知他。我没有找到任何关于的东西。如果有人可以为此建议一些代码示例,那就太好了。

如何在 C# 中获取 TFS 工作项的“分配给”电子邮件 ID

我们曾经设法使用以下代码获取分配给用户的电子邮件地址。

它只是使用用户的全名连接到活动目录,并为您返回电子邮件。

希望对您有所帮助!

 /// <summary>
    /// Retrieves a user's email address from Active Directory based on their display name
    /// </summary>
    private string GetEmailAddress(string userDisplayName)
    {
        DirectorySearcher ds = new DirectorySearcher();
        ds.PropertiesToLoad.Add("mail");
        ds.Filter = String.Format("(&(displayName={0})(objectCategory=person)((objectClass=user)))", userDisplayName);
        SearchResultCollection results = ds.FindAll();
        if (results.Count == 0)
        {
            return string.Empty;
        }
        ResultPropertyValueCollection values = results[0].Properties["mail"];
        if (values.Count == 0)
        {
            return string.Empty;
        }
        return values[0].ToString();
    }