Crm 2011 Display Image for a contact JavaScript
This is an easy way to display an image attached to a contact on contact form
I am using the XrmServiceToolkit.js from CodePlex.
Create Image.html which will be used as a html web resource, we will be adding this web resource on the contact form
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="../ClientGlobalContext.js.aspx" script type="text/javascript" ></script>
<script src="../js/jquery.js"></script>
<script src="../js/json2.js"></script>
<script src="../js/XrmPage-vsdoc.js"></script>
<script src="../js/XrmServiceToolkit.js"></script>
<script src="../js/ContactImage.js"></script>
</head>
<body onload="DisplayImage();">
<img id="imgContact" width="200" height="200" />
</body>
</html>
Create ContactImage.js File which will retrieve the image attached to the contact and render it in the image control
function DisplayImage() {
GetImage(parent.Xrm.Page.data.entity.getId());
}
function GetImage(contactId) {
XrmServiceToolkit.Rest.RetrieveMultiple(
'AnnotationSet', // entity name + 'Set'
'select=DocumentBody&filter=endwith(FileName,".png")
or endwith(FileName,".jpg") or endwith(FileName,".jpeg")
and ObjectId/Id eq guid ' + contactId, // Some raw oData goodness, assuming field is a string.
Hopefully, there will be an API for filters in further versions.
function (record) {
if (record != null && record[0] != null && record[0].DocumentBody != null)
document.getElementById("imgContact").src = "data:image/jpg;base64," + record[0].DocumentBody;
},
errorCallback,
function () { }, // this, finally,
will be called after all pages are retrieved
false // async or not?
);
}
function errorCallback() {
alert("Error.");
}
Comments
Post a Comment