Using the TFS 2012 API, how do I get the email address of a user?

风格不统一 提交于 2019-12-06 11:09:49

Try using Mail instead of Email for the attribute name - that works for me.

Also, if that doesn't work, check the results of member.GetProperties() - maybe that will give you the right name to use.

For me, GetProperty("Mail") also worked.

I bumped into the same problem, I found a work around by getting my users email address from AD using the following code.

    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;
            }
        }
    }

But then I found that it would throw an exception when my user was not in my domain. So this code helped me have an a second source. If I didn't find in AD i would go and use the IdentityManagementService.

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

Then I would simply use this execution.

    var ownerMail = GetUserEmail(checkinEvent.Resource.CheckedInBy.DisplayName);
    if (string.IsNullOrEmpty(ownerMail))
    {
       ownerMail = GetUserByAccountName(checkinEvent.Resource.CheckedInBy.DisplayName).GetProperty("Mail").ToString();
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!