How To Show GAL Pictures For Internal Emails In Exchange 2010 Outlook Web App

[ 30 ] Comments
Share

A while back I wrote an article explaining how it is possible to modify OWA 2010 to show GAL photos in the GAL details page. Recently, I was asked if it’s possible to add the GAL image to the Read Message page (for internal emails only). I had a quick look at the OWA code, and discovered that it is possible with a fairly simple mod.

As in the original article, it is first necessary to add a file to the server. This is a file that gets the GAL image from AD, and returns it as if it was an image file on the server.

It makes sense to discuss the extra file first. Look for the folder at this location

C:\Program Files\Microsoft\Exchange Server\V14\ClientAccess\Owa\forms\premium

this is where you’ll be working. Within the folder, create a new file named ShowGALPicture.aspx and then open it in Notepad. Insert the following text, and then save it back and close the file. Note that instead of yourdomain.com, you will need to insert your own domain name.

<%@ Page language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Common" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%
try
{
  string strEmail = Request.QueryString["ID"];
  OleDbConnection cn = new OleDbConnection("Provider=ADsDSOObject;dsn=Active Directory Provider");
  cn.Open();
  string strSQL = "SELECT thumbnailPhoto FROM 'LDAP://yourdomain.com'" +
    " WHERE mail='" + strEmail + "'";
  OleDbCommand cmd = new OleDbCommand(strSQL, cn);
  OleDbDataReader rs = cmd.ExecuteReader();
  if(rs.Read())
  {
    if(rs.GetValue(0) != System.DBNull.Value)
    {
      Byte[] arrB = (Byte[])rs.GetValue(0);
      Response.ContentType = "application/octet-stream";
      Response.AddHeader("content-disposition", "attachment; filename=GALPicture.jpg");
      Response.BinaryWrite(arrB);
    }
    else
    {
      Response.Redirect("DefaultGALPicture.gif");
    }
  }
}
catch{}
%>

Next, look for the file named readmessage.ascx. Make a backup copy (in case you break it), and then open it in Notepad. Now, all you need to do is insert the following block of code at the end. Again, remember to use the name of your own domain, instead of @yourdomain.com.

<script type="text/javascript">
var e = document.getElementById("spnFrom");
var s = e.getAttribute("_em");
if (s.indexOf("@yourdomain.com") != -1)
{
  var e2 = document.getElementById("divSender");
  e2.setAttribute("style", "width:100%;");
  e2.parentNode.setAttribute("style", "height:100px");
  var s2 = e2.innerHTML;
  s2 = s2 + "<div style=\"position:fixed;right:0;top:0;z-index:100;\"><img src=\"/owa/forms/premium/ShowGALPicture.aspx?ID=" + s + "\"></div>";

  e2.innerHTML = s2;
}
</script>

Save the file back, and we’re nearly finished. Note the Response.Redirect within the aspx code. This is there to handle GAL entries that do not have an image uploaded. Place an image file named DefaultGALPicture.gif in the same folder, and this will appear for users without an image.

As with all these OWA mods, make sure that they are still there after each service pack and rollup. Sometimes your modified files get replaced with new ones.


30 Responses to How To Show GAL Pictures For Internal Emails In Exchange 2010 Outlook Web App

  1. Tamas says:

    First off, thanks for coming back with this so quickly after my question, you’re awesome. Unfortunately, this doesn’t seem to work for me. Where is the photo supposed to be loading?

    • admin says:

      When you open an internal email, it should show a GAL picture for the sender, if you uploaded one, next to their address. Did you change the code to put your own domain name in, instead of the ‘yourdomain.com’ that I put there?

  2. Tamas says:

    Yes, I changed it to @mclendons.com but I’m not seeing photos either in the reading pane or when I open an e-mail in a new window. Photos still show up when opening the contact page like you set up in the first tutorial.

    • admin says:

      Okay, first, I’ll check that I understood your first message. This is to show the photo alongside the sender name when you open a message you received from an internal sender.

      Next, if this assumption is correct, we’ll check that your server isn’t using a cached copy of the file, or even a different version. When you are looking at an internal email that you’d expect to show the image, see if you can get the browser to show you the page source. In IE, you might need to right-click the message GUI in various places to get the correct menu. It may be easier to use Chrome – I think the view source option is more readily available.

  3. Tamas says:

    “This is to show the photo alongside the sender name when you open a message you received from an internal sender.”

    Correct, that’s what I was after.

    I’m looking at the source and the javascript is there, but no picture is being loaded. Here’s what I see in there:

    <script type="text/javascript">
    var e = document.getElementById("spnFrom");
    var s = e.getAttribute("_em");
    if (s.indexOf("@mclendons.com") != -1)
    {
    var e2 = document.getElementById("divSender");
    e2.setAttribute("style", "width:100%;");
    e2.parentNode.setAttribute("style", "height:100px");
    var s2 = e2.innerHTML;
    s2 = s2 + "<img src=\"/owa/forms/premium/ShowGALPicture.aspx?ID=" + s + "\">";
    e2.innerHTML = s2;
    }
    </script>

    • admin says:

      That looks okay, but I could easily be missing something. To make sure that the code is even working at all, after the line that looks like this

      var s = e.getAttribute("_em");

      add another line

      alert(s);

      it should show the sender email address in a pop-up dialog box when you open each message. If not, then something is preventing the code from running at all. If it does, then we have a logic error somewhere.

      BTW, sorry for the delay – I’m on vacation at the moment, so I don’t even have a connection very often.

  4. Tamas says:

    The alert popped up, so the code is running. There’s probably a logic error somewhere.

    Could it be a conflict between our e-mail address and the AD domain name? Our e-mails are @mclendons.com, but the domain is corp.mclendons.com.

  5. Tamas says:

    Oh, you know what, I think I may see the problem. I ran the alert(s); test again to see what it was reporting as. It doesn’t display the e-mail address, instead it displays the following:

    /O=MCLENDONS ORGANIZATION/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=JOE COLASURDOC07

    Instead of pulling the e-mail address, it appears to be pulling LDAP strings.

    • admin says:

      Okay, I see. In mine, it just shows the email addresses, but if you’ve had older versions of Exchange, it will use the X400 address (I think that’s what that is). That means you’ll need to use something like “MCLENDONS ORGANIZATION” instead of “mclendons.com” in the next line. But I’m not sure if you’ll also need to change the ShowGALPicture.aspx file too. Try it, and see if you can see the GAL picture, or if you get an image that suggests there is something going wrong, like a red X, or a ‘broken picture frame’ or something.

  6. Tamas says:

    When I changed it from “@mclendons.com” to “MCLENDONS ORGANIZATION” I get a red X.

    • admin says:

      Okay, it is at least trying to show a picture now. I’m still not sure if that long address it shows is what stored in AD for your email addresses, but we’ll find out. There are two things to do now. The first may be enough by itself, but if not, we’ll make the second change.

      Try this for now. Change the line that says
      s2 = s2 + "<img src=\"/owa/forms/premium/ShowGALPicture.aspx?ID=" + s + "\">";
      to
      s2 = s2 + "<img src=\"/owa/forms/premium/ShowGALPicture.aspx?ID=" + encodeURIComponent(s) + "\">";

  7. Tamas says:

    Okay, I tried that, still getting a red X. (Sorry for the delay in response, the weekend and all that..)

    • admin says:

      I sort of realised that it might not work, since you said it was working okay with the GAL details page, which I know does send an email address.

      But here’s something to try. Change the bit of the ShowGALPicture.aspx that says

      " WHERE mail='" + strEmail + "'";

      to

      " WHERE legacyExchangeDN='" + strEmail + "'";

      that will probably stop it working with the GAL details page, though. So hopefully, the next line will make it work with both

      " WHERE mail='" + strEmail + "' OR legacyExchangeDN='" + strEmail +"'";

  8. Tamas says:

    Okay, the photos are loading now (both in GAL details page and in the message window), so we’re almost out of the woods! But we’ve got some odd cropping issues and the sender name disappears in the message window. Here are screenshots of the same message, without photo and with, to see the problem. I would assume it’s something to do with the CSS of the sender line.

    http://imgur.com/a/GW0BC

    • admin says:

      It’s probably struggling to fit it into the available space. I was quite difficult (or, perhaps, just rather annoying) to get it to put the picture there. I’d suggest, for now, inserting something width=10 after the word img in the image tag. Just to see if it’s just a formatting problem.

  9. Tamas says:

    Yup, I tried it with several different widths. It looks like anything wider than 30px starts cutting off the sender’s name and forcing it below the frame it’s in.

    It’s also not showing at all in the e-mail preview pane, which is how most employees read their e-mails instead of double-clicking to open in a new window. Is that a separate form I need to add the script to so it’ll show up in both places?

    • admin says:

      I’m afraid it’s probably going to take me a while to find a completely satisfactory way of showing the image next to the address. I haven’t yet been able to make the surrounding div any larger, but I’ll have another look when I get back home.

      The preview pane is going to have to be a separate project.

  10. Tamas says:

    It’s also creating a big gap between the sender field and the recipient field, like it’s trying to make room for the picture, but the picture doesn’t make use of it.

  11. Tamas says:

    I managed to get the picture to fit. Looking at the script, you were making the parent div 100px high but this was not affecting the height of the existing div, so I changed the following line:

    e2.setAttribute("style", "width:100%;");

    to

    e2.setAttribute("style", "width:100%; height:100px");

    Photos are now showing up in individual e-mail windows in OWA at the proper size! Now I just need to figure out how to get it to work in the reading pane.

  12. Tamas says:

    Never mind, that only worked in Chrome. IE was still getting cut off.

    • admin says:

      It’s annoying that they all interpret the code differently. I’m sure I’ll be able to work something out, but I only had a few minutes to look at this before I went away on vacation. But in IE, the div sizes seemed to remain stubbornly fixed, even though I went up through the parent divs trying various things. If you want to carry on playing with it in IE, press the F12 key to get the console window – it’s pretty good in the latest version, and you can see where styles are explicitly defined, and where they are inherited.

      I’d guess that the place to start looking for the preview window would be an iframe in startpage.aspx .

  13. Tamas says:

    I figured out the problem. IE was not reading your javascript format for applying the CSS. I changed this:

    e2.setAttribute("style", "width:100%;");
    e2.parentNode.setAttribute("style", "height:100px");

    to this:

    e2.style.width = '100%';
    e2.style.height = '100px';
    e2.parentNode.style.height = '100px';

    The format works now. I also notice that when logging on to OWA, the first message that comes up in the reading pane displays the picture, but as soon as you change the message you’re viewing, it goes away and doesn’t come back until you log out and back in.

    • admin says:

      style.width etc. is the way I always used to do it, until I discovered that that syntax didn’t work for all properties in IE. .setAttribute should always work, though, AFAIK. Except I don’t think it works in older versions of IE. It’s not easy to find something that works every time, and sometimes I give up even trying, after all, this is supposed to be fun.

      Last night, I thought it might be an idea to use getElementsByTagName to get all the divs, and then just clear the heights of all of them. But without the server to hand, I’ve no idea how that would work out.

      I’ve no idea why the reading pane behaves like that, but I haven’t even looked at that yet.

  14. Tamas says:

    Also, is there a way to have the ShowGALPicture.aspx display a default picture? Some of our users are missing photos, and we have several service accounts and other automated e-mail systems, and it would be nice if we could get a default picture instead of a broken image when they don’t have one.

    • admin says:

      Should be possible. I’ve thought about this situation, of course, but not yet decided how to handle it. The first thing I’d try would be to put default image in the same folder, and if the AD recordset is empty after the search, just do a Response.Redirect to it.

  15. admin says:

    Okay, I think I have something for the image position and size:

    var e2 = document.getElementById(“divSender”);
    var s2 = e2.innerHTML;
    s2 = s2 + “<div style=\”position:fixed;right:0;top:0;z-index:100;\”><img src=\”/owa/forms/premium/ShowGALPicture.aspx?ID=” + s + “\”></div>”;
    e2.innerHTML = s2;

  16. Dorado185 says:

    Thank you for the guide(s) – they have been very helpful. So, I was able to get everything to work in the read message page. I noticed that some users were mentioning that they were adding the images to the Reading Pane (not the reading page). How do you get this to work?

    Thank you in advance.

    • admin says:

      Hello Daniel. It’s a while since I looked at this, but I think it appears in the reading pane because the reading pane uses the same page as the reading page. I think it’s embedded. If your users are already seeing it in the reading pane, then there’s probably no more to be done. I certainly never found a separate block of code for the reading pane.

      Or are you saying that it isn’t working in all cases where you’d expect it to?

  17. dorado185 says:

    Using your guides, I have user photos working in three places:

    1. When you double click an email message in OWA you see the user’s thumbnail photo in the email/page that opens open.

    2. When you are writing a new message and open the address book – the user image shows under their contact details.

    3. When double clicking the user name in a message – their image appears in their contact details (pretty much #2 again) :-)

    However, no images are appearing in the reading pane. A user must double click an email to view the user’s thumbnail photo. I just wanted to know if I am doing something wrong or if I need to edit the code in anyway.

    Thank you for the quick reply and for the assistance.

    Have a good one.

    • admin says:

      I think I originally overlooked the reading pane, because it sort of annoys me, and I always switch it off. If I get some time in the next few days, I’ll have another look at it.




Leave a Reply to Dorado185 Cancel reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>