Skip to content

Instantly share code, notes, and snippets.

@bzbetty
Created February 7, 2013 21:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bzbetty/4734222 to your computer and use it in GitHub Desktop.
Save bzbetty/4734222 to your computer and use it in GitHub Desktop.
using Microsoft.TeamFoundation.Framework.Server;
using System;
using System.Collections.Generic;
using System.DirectoryServices.AccountManagement;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.Framework.Common;
using System.Threading.Tasks;
namespace Enlighten.Tfs.ActiveDirectoryImageSync
{
public enum ImageFormat
{
bmp,
jpeg,
gif,
tiff,
png,
unknown
}
public class SyncImagesJob : ITeamFoundationJobExtension
{
public TeamFoundationJobExecutionResult Run(TeamFoundationRequestContext requestContext, Microsoft.TeamFoundation.Framework.Server.TeamFoundationJobDefinition jobDefinition, DateTime queueTime, out string resultMessage)
{
resultMessage = "";
try
{
var service = requestContext.GetService<TeamFoundationIdentityService>();
var identities = service.ReadFilteredIdentities(requestContext, "Microsoft.TeamFoundation.Identity.DisplayName CONTAINS '' AND Microsoft.TeamFoundation.Identity.Type == 'User'", 5000, null, true, MembershipQuery.None);
if (identities != null)
{
foreach (var identity in identities.Items)
{
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[];
if (thumbNail == null)
continue;
var imageFormat = GetImageFormat(thumbNail);
var propertyUpdates = new List<PropertyValue>() {
new PropertyValue("Microsoft.TeamFoundation.Identity.Image.Data", thumbNail),
new PropertyValue("Microsoft.TeamFoundation.Identity.Image.Type", "image/" + imageFormat.ToString()),
new PropertyValue("Microsoft.TeamFoundation.Identity.Image.Id", Guid.NewGuid().ToByteArray()),
new PropertyValue("Microsoft.TeamFoundation.Identity.CandidateImage.Data", null),
new PropertyValue("Microsoft.TeamFoundation.Identity.CandidateImage.UploadDate", null),
};
service.UpdateExtendedProperties(requestContext, identity.Descriptor, propertyUpdates);
service.RefreshIdentity(requestContext, identity.Descriptor);
}
}
}
catch (Exception e)
{
resultMessage = e.Message + e.StackTrace;
return TeamFoundationJobExecutionResult.Failed;
}
return TeamFoundationJobExecutionResult.Succeeded;
}
//from http://stackoverflow.com/questions/1397512/find-image-format-using-bitmap-object-in-c-sharp
private static ImageFormat GetImageFormat(byte[] bytes)
{
// see http://www.mikekunz.com/image_file_header.html
var bmp = Encoding.ASCII.GetBytes("BM"); // BMP
var gif = Encoding.ASCII.GetBytes("GIF"); // GIF
var png = new byte[] { 137, 80, 78, 71 }; // PNG
var tiff = new byte[] { 73, 73, 42 }; // TIFF
var tiff2 = new byte[] { 77, 77, 42 }; // TIFF
var jpeg = new byte[] { 255, 216, 255, 224 }; // jpeg
var jpeg2 = new byte[] { 255, 216, 255, 225 }; // jpeg canon
if (bmp.SequenceEqual(bytes.Take(bmp.Length)))
return ImageFormat.bmp;
if (gif.SequenceEqual(bytes.Take(gif.Length)))
return ImageFormat.gif;
if (png.SequenceEqual(bytes.Take(png.Length)))
return ImageFormat.png;
if (tiff.SequenceEqual(bytes.Take(tiff.Length)))
return ImageFormat.tiff;
if (tiff2.SequenceEqual(bytes.Take(tiff2.Length)))
return ImageFormat.tiff;
if (jpeg.SequenceEqual(bytes.Take(jpeg.Length)))
return ImageFormat.jpeg;
if (jpeg2.SequenceEqual(bytes.Take(jpeg2.Length)))
return ImageFormat.jpeg;
return ImageFormat.unknown;
}
}
}
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Framework.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace Enlighten.Tfs.ConsoleApp
{
public class ConsoleApplication
{
public static void Main()
{
Uri tfsCollectionUri = new Uri(String.Format("http://urltotfs"));
var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(tfsCollectionUri);
var service = tfs.GetService<ITeamFoundationJobService>();
var definition = new TeamFoundationJobDefinition(
new Guid("fa60c04e-c996-413e-8151-15933f5a2bac"),
"AD Image Sync Job",
"Enlighten.Tfs.ActiveDirectoryImageSync.SyncImagesJob",
null,
TeamFoundationJobEnabledState.Enabled);
var schedule = new TeamFoundationJobSchedule(DateTime.Now, 24 * 60 * 60);
definition.Schedule.Add(schedule);
service.UpdateJob(definition);
service.QueueJobNow(definition, false);
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment