Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

API to update users image - Identity Extended Properties not saving

Tags:

tfs-sdk

I'm trying to write a small script to set all users images to their AD image, I did some jumping around in ILSpy and found out what to set using the TFS Server API, however the code needs to be a bit different because I'm using the client API instead.

The code I have below can succesfully iterate through all the users in tfs, look them up in AD, grab the thumbnail, set the property on the TFS identity. But I can't for the life of me figure get the extended property to save back into TFS.

The code doesn't exception, but the property isn't set to the value I set it to when I next run the application.

Does anyone know the way to save extended properties via the client api?

Microsoft.TeamFoundation.Client.TeamFoundationServer teamFoundationServer = new Microsoft.TeamFoundation.Client.TeamFoundationServer("{URL TO TFS}");

FilteredIdentityService service = teamFoundationServer.GetService<FilteredIdentityService>(); ;
IIdentityManagementService2 service2 = teamFoundationServer.GetService<IIdentityManagementService2>();

foreach (var identity in service.SearchForUsers(""))
{
    var user = UserPrincipal.FindByIdentity(new PrincipalContext(ContextType.Domain), identity.UniqueName);
    if (user == null) continue;
    var de = new System.DirectoryServices.DirectoryEntry("LDAP://" + user.DistinguishedName);
    var thumbNail = de.Properties["thumbnailPhoto"].Value as byte[];

    identity.SetProperty("Microsoft.TeamFoundation.Identity.CandidateImage.Data", thumbNail);
    identity.SetProperty("Microsoft.TeamFoundation.Identity.CandidateImage.UploadDate", DateTime.UtcNow);

    service2.UpdateExtendedProperties(identity);
}
like image 318
Betty Avatar asked Sep 28 '12 04:09

Betty


1 Answers

Figured it out, needed to set some additional properties.

Microsoft.TeamFoundation.Client.TeamFoundationServer teamFoundationServer = new Microsoft.TeamFoundation.Client.TeamFoundationServer("http://urltotfs");

FilteredIdentityService service = teamFoundationServer.GetService<FilteredIdentityService>(); ;
IIdentityManagementService2 service2 = teamFoundationServer.GetService<IIdentityManagementService2>();

foreach (var identity in service.SearchForUsers(""))
{
    var user = UserPrincipal.FindByIdentity(new PrincipalContext(ContextType.Domain), identity.UniqueName);
    if (user == null) continue;
    var de = new System.DirectoryServices.DirectoryEntry("LDAP://" + user.DistinguishedName);
    var thumbNail = de.Properties["thumbnailPhoto"].Value as byte[];

    identity.SetProperty("Microsoft.TeamFoundation.Identity.Image.Data", thumbNail);
    identity.SetProperty("Microsoft.TeamFoundation.Identity.Image.Type", "image/png");
    identity.SetProperty("Microsoft.TeamFoundation.Identity.Image.Id", Guid.NewGuid().ToByteArray());
    identity.SetProperty("Microsoft.TeamFoundation.Identity.CandidateImage.Data", null);
    identity.SetProperty("Microsoft.TeamFoundation.Identity.CandidateImage.UploadDate", null);

    service2.UpdateExtendedProperties(identity); 
}
like image 108
Betty Avatar answered Oct 16 '22 19:10

Betty