使用TFS 2012 API,如何获取用户的电子邮件地址?

我正在尝试使用API​​获取TFS 2012中特定用户的电子邮件地址。 我在“配置文件”部分设置了用户首选电子邮件地址。 我已经在网上进行了大量搜索并获得了以下代码。

var userId = "myUserId"; var collection = new TfsTeamProjectCollection(tfsUri, tfsCerd); var managementService = collection.GetService(); var member = managementService .ReadIdentity( IdentitySearchFactor.AccountName, userId, MembershipQuery.Direct, ReadIdentityOptions.ExtendedProperties); var emailAddress = member.GetAttribute("Mail", null) 

这段代码既是成功又是失败。 它成功地检索了指定的用户; 但是,问题是Email属性为空。 当我分析成员变量时,我注意到那里列出了“Mail”属性并且它是空的。 然后我注意到还有另外两个名为“ConfirmedNotificationAddress”和“CustomNotificationAddress”的属性,它们在那里正确地使用了我喜欢的电子邮件地址。

我想知道为什么我似乎无法使用首选电子邮件地址正确加载“Mail”变量,因为我将需要此代码在许多人服务器上工作。

尝试使用Mail而不是Email来获取属性名称 – 这对我有用。

此外,如果这不起作用,请检查member.GetProperties()的结果 – 也许这将为您提供正确的名称。

对我来说, GetProperty("Mail")也有效。

我遇到了同样的问题,我通过使用以下代码从AD获取用户的电子邮件地址找到了解决方法。

  public string GetUserEmail(string username) { using (var pctx = new PrincipalContext(ContextType.Domain)) { using (UserPrincipal up = UserPrincipal.FindByIdentity(pctx, username)) { return up != null && !string.IsNullOrEmpty(up.EmailAddress) ? up.EmailAddress : string.Empty; } } } 

但后来我发现当我的用户不在我的域中时会抛出exception。 所以这段代码帮助我得到了第二个来源。 如果我没有在AD中找到,我会去使用IdentityManagementService。

  public TeamFoundationIdentity GetUserByAccountName(string account) { var ims = _tfServer.GetService(); return ims.ReadIdentity(IdentitySearchFactor.DisplayName, account, MembershipQuery.Expanded, ReadIdentityOptions.ExtendedProperties); } 

然后我会简单地使用这个执行。

  var ownerMail = GetUserEmail(checkinEvent.Resource.CheckedInBy.DisplayName); if (string.IsNullOrEmpty(ownerMail)) { ownerMail = GetUserByAccountName(checkinEvent.Resource.CheckedInBy.DisplayName).GetProperty("Mail").ToString(); }