Thursday, October 23, 2008

SP2007: Adding Photos to the Contacts List

Adding photos to a Contacts List is very cumbersome. Your options are:
1) Add a picture column to the contacts list, create a picture library, upload your pictures to the picture library, and copy and paste the URL of each picture into the picture column in the contacts list... very cumbersome.
2) Create a Picture Library to use for contacts. Add columns for name, email, address, and phone. Add a picture of the user and then fill in the columns for their contact information. Just hope you never have to switch out a photo or you'll have to re-enter the contact info.
3) Use a Contacts List, and just attach the photos to each contact. This is the easiest to set up, but requires users to click to open the attachment to see the photo.

The third option is close, but the view needs to be improved.

Ideally if you attach a photo to a contact in the Contacts List it would be nice if that photo was displayted when you were viewing the contact's details. This can be done with a bit of javascript. Launch SharePoint Designer, edit the DispForm.aspx page, and add the following javascript. There may be better places to add this, but putting it in the content place holder "PlaceHolderBodyAreaClass" seems to work fine. It may not work with multiple attachments, and it could also be improved to only execute if the attachment is a jpg or gif, but that wasn't needed for this project.

if (document.getElementById("idAttachmentsTable").rows.length > 0)
myRow = document.getElementById("idAttachmentsTable").rows[0];
myHref = myRow.firstChild.firstChild.firstChild.href;
myRow.firstChild.firstChild.firstChild.innerHTML = '<IMG SRC="' + myHref + '">';