CRM 2011: Display a picture stored in note on a form

With CRM, you can store a picture as a file attached to a note, and display that picture on the form. This is quite handy!

1/ First, create a wab resource of the picture type, with a default picture, and add it to the form

2/ Maje sure you use the CrmRestKit and it to your form

3/ Then, add this code in the onLoad JS:

function updatePicture(imageTitle, controlName) {
    var entityId = Xrm.Page.data.entity.getId();
    if (entityId) {
        var cols = [“AnnotationId”, “DocumentBody”, “MimeType”, “NoteText”];
        var filter = “ObjectId/Id eq guid'” + entityId + “‘ and substringof(‘” + imageTitle + “‘,NoteText)”;
        var note = GetFirstRecord(“Annotation”, cols, filter);
        if (note) {
            var src = “data:” + note.MimeType + “;base64,” + note.DocumentBody;
            var pictureControl = document.getElementById(controlName);
            pictureControl.setAttribute(“src”, src);
        }
    }
}
function GetFirstRecord(entityName, columns, filter) {
    var record = null;
    CrmRestKit.ByQuery(entityName, columns, filter, false)
        .fail(function (xhr, status, e) { alert(‘Error’); })
        .then(function(data) {
            if (data != null && data.d != null && data.d.results != null && data.d.results.length > 0)
                record = data.d.results[0];
        });
    return record;
}
And you’re done!

Leave a Reply

Your email address will not be published. Required fields are marked *