如何获得TFS工作项的分配给C#中的电子邮件ID?

时间:2014-11-18 08:24:40

标签: c# tfs tfs-workitem

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

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,以便我可以通过向他的电子邮件地址发送邮件来通知他。我没有找到任何关于。如果有人可以为此建议一些代码示例,那么它将是大。

1 个答案:

答案 0 :(得分:0)

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

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

希望它有所帮助!

 /// <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();

    }