11
votes

In my Team Foundation Server, I have a collection containing a Team Project. This Team Project has several contributors. The following lines of code get all contributors of that project:

TfsTeamProjectCollection collection = new TfsTeamProjectCollection(new Uri("http://tfs:8080/tfs/CollectionName"));

IGroupSecurityService groupSecurityService = collection.GetService<IGroupSecurityService>();

Identity contributors = groupSecurityService.ReadIdentity(SearchFactor.AccountName, "[ProjectName]\\Contributors", QueryMembership.Expanded);

Identity[] members = groupSecurityService.ReadIdentities(SearchFactor.Sid, contributors.Members, QueryMembership.None);

Each Identity in members has a Property MailAddress, which in my case is equal to string.Empty.

Where do I administrate those mail addresses?

My first idea was to have a look at the users in Start->Administrative Tools->Computer Management->Users

I selected one of the users and opened his properties. I thought there might be an email property that the TFS would take. But I couldn't find one.

Then I opened the TFS Administration Console, looked for Group Membership and navigated to one of the users. There is also no way of editing properties.

Does anyone know where to set that email address?

5

5 Answers

9
votes

Great question! There is a TFS job that is scheduled to run every hour to update information about security identities stored in TFS against the details in Active Directory. Some of this information includes the display name, security identifier (SID), AD distinguished name, and e-mail address, among other details. You can find out this cache of details by looking at the tbl_security_identity_cache table in the configuration database.

Warning: Querying against or changing the database puts you in a position where you will likely not be able to get support from Microsoft. It's advised that you don't do this unless instructed by a Microsoft support representative in the context of an active support case. You were actually doing it right by using the TFS SDK to get this information.

If your TFS environment is not in an Active Directory environment, then it will attempt to synchronize information from the local machine where TFS is running. It won't have details about the e-mail address to use so it will be left blank.

Starting in the next version of TFS after TFS 2010, each user will be able to update their notification e-mail address in their profile using Team Web Access.

3
votes

The following is for TFS 2013 Update 5

** WARNING ** Getting caught editing the TFS database directly
** will void your Microsoft Support Agreement. **
What follows is not for the uninitiated.  ** Proceed at your own risk. **

Locate the user or users with email addresses needing to be set. There can be duplicates in the Identities table. I found that the ones with the highest SequenceId were the active Identities.

Use Tfs_TFSConfiguration
SELECT i1.AccountName, i1.Id FROM tbl_Identity AS i1
LEFT OUTER JOIN tbl_Identity AS i2
    ON (i1.AccountName=i2.AccountName AND i1.SequenceId<i2.SequenceId)
WHERE i2.AccountName IS NULL
    AND i1.AccountName in ('<your first user>','<another user>','<and so on>')

This gives a list of the most recent the Id(s), in GUID form, for the accounts that you need to update. These GUIDs must be reformatted into ArtifactId(s), which is a transformed binary format. This is accomplished by reversing the byte order (low to high) or each of the first three parts of the GUID, but leaving the last two parts in order. E.g.:

Returned 'Id' GUID     =01020304-0506-0708-090A-0B0C0D0E0F10
Byte Swapped GUID      =04030201-0605-0807-090A-0B0C0D0E0F10
Reformatted 'ArtifacId'=0x0403020106050807090A0B0C0D0E0F10

Next, you have to find the PropertyId(s) used by TFS for email notifications. In TFS 2013 U5, this can be found with the following query:

USE Tfs_TFSConfiguration
SELECT Name, PropertyId FROM tbl_PropertyDefinition WHERE Name LIKE '%Address%'

This will give you the PropertyId(s) for ConfirmedNotificationAddress and CustomNotificationAddresses; which are the two property fields used by TFS 2013 U5 to send notification emails.

Next, you have to find the InternalKindId for the Identity Framework for the TFS DatabaseCategory

USE Tfs_TFSConfiguration
SELECT Description, InternalKindId FROM tbl_PropertyArtifactKind
WHERE Description='Identity'

Now to put it all together, ...

If the configuration records for your user(s) already exist, you can update the settings with:

USE Tfs_TFSConfiguration
UPDATE tbl_PropertyValue SET LeadingStringValue='<user's notification email address>'
WHERE ArtifactId=<ArtifactId, reformatted from tbl_Identity query>
AND PropertyId IN ('<first PropertyId from tbl_PropertyDefinition>', '<second id>')

Note: that ArtifactId is a binary value, based upon a semi-byte-swapped database GUID, and will not match a quoted value in the UPDATE query.  I.e. this part of the query will look something like:

WHERE ArtifactId=0x90D490F6BF7B31491CB894323F38A91F AND

Below I assume that the PartitionId is '1'; this should be verified before you continue by a brief scan of the records in the tbl_PropertyValue table.
If you are loading configuration settings that have not yet been set:

USE Tfs_TFSConfiguration
INSERT INTO tbl_PropertyValue
    (PartitionId, ArtifactId, InternalKindId, Version, PropertyId, LeadingStringValue)
VALUES ('1', <ArtifactId, reformatted from tbl_Identity query>,
    '<InternalKindId from tbl_PropertyArtifactKind>',
    '0',
    '<first PropertyId from tbl_PropertyDefinition>', 
    '<user's notification email address>'),

    ('1', <ArtifactId, reformatted from tbl_Identity query>,
    '<InternalKindId from tbl_PropertyArtifactKind>',
    '0',
    '<second PropertyId from tbl_PropertyDefinition>', 
    '<user's notification email address>')

Note: that ArtifactId must be an unquoted binary value, transformed from the GUID returned from the tbl_Identity as explained above.
Note: that two records are created for each ArtifactId, one for each PropertyId.

** WARNING ** Getting caught editing the TFS database directly
** will void your Microsoft Support Agreement. **
** Proceed at your own risk. **

(This works for me, ...  but, I do not have a Microsoft Support Agreement to invalidate.)

2
votes

If Active Directory does not get synched with TFS, and assuming your goal to keep email address is for sending notifications you can use the IEventService.GetEventSubscriptions() methods.

var eventService = (IEventService)collection.GetService(typeof(IEventService));

foreach (var member in members)
{
    var subscription = eventService.GetEventSubscriptions(member.DisplayName).First();
    {
        if (subscription != null && string.IsNullOrEmpty(member.MailAddress))
            member.MailAddress = subscription.DeliveryPreference.Address;
    }
}
1
votes

I believe this is kept in Active Directory.

0
votes

For TFS2017+, each user can have a preferred email address, that they can setup in their profile, on the web interface.

It can override, or replace the email set in Active Directory. It also has the benefit to be an instant change, no synchronisation needed.

The field will be initialized with the value that has been set in Active Directory. The synchronization doesn't seem to happen anymore.