Answered by:
"Invalid User Authorization" when tring to display a picture in IFrame - CRM Online..

Question
-
Hi All,
I am trying to display a picture on the account form. The file is uploaded as an attachement on the entity and would like to call the attached picture at the "onload" event.
I found this code thanks to "Adi Katz" explaining how to do just that. http://mscrm4ever.blogspot.com/2009/02/displaying-image-in-iframe.html
// *********************************************************
// To fetch the picture file from the Notes Entity related to this account
// *********************************************************var xml = "" +
"<?xml version='1.0' encoding='utf-8'?>" +
"<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
GenerateAuthenticationHeader() +
"<soap:Body>" +
"<Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
"<fetchXml>" +
"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+
"<entity name='annotation'>"+
"<attribute name='subject'/>"+
"<attribute name='notetext'/>"+
"<attribute name='filename'/>"+
"<attribute name='annotationid'/>"+
"<order attribute='subject' descending='false'/>"+
"<filter type='and'>"+
"<condition attribute='isdocument' operator='eq' value='1'/>"+
"<condition attribute='filename' operator='eq' value='mex9.jpg'/>"+
"</filter>"+
"</entity>"+
"</fetch>"+
" </fetchXml>" +
" </Fetch>" +
" </soap:Body>" +
"</soap:Envelope>";// Prepare the xmlHttpObject and send the request.
var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Fetch");
xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xHReq.setRequestHeader("Content-Length", xml.length);
xHReq.send(xml);// Capture the result.
var resultXml = xHReq.responseXML;// Check for errors.
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0)
{
var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
alert(msg);
}// Process and display the results.
else
{// Capture the result and UnEncode it.
var resultSet = new String();
resultSet = resultXml.text;
resultSet.replace('<','<');
resultSet.replace('>','>');// Create an XML document that you can parse.
var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
oXmlDoc.async = false;
// Load the XML document that has the UnEncoded results.
oXmlDoc.loadXML(resultSet);// Display the results.
var results = oXmlDoc.getElementsByTagName('result');for (i=0;i < results.length;i++)
{
var idValue = results[i].selectSingleNode('./annotationid').nodeTypedValue;
}alert(idValue);
var iframeDoc = crmForm.all.IFRAME_Student_Picture.contentWindow.document;
//alert(iframeDoc);
var image = iframeDoc.createElement('img');
//alert(image);
image.src = "/Activities/Attachment/download.aspx?AttachmentType=5&AttachmentId=" + idValue;
}// *************************************************************************************
I am trying to omplement this code with CRM Online. Am I missing something? How do I pass security credentials via JScript to the Download.Aspx?
Thanks in advance for your help....Tito
Saturday, October 16, 2010 1:37 PM
Answers
-
PS: if you finalize a working set of code for this, please post back to this thread or make a blog posting that I can point people to. It would be very exciting to finally have a Javascript-only resolution to this issue for many developers.
Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com
Thank you for your input.... I used Data URI and here is the final code.... You are right IE5-7 do not support this but working fine in IE8.=======================================================================================================
// *********************************************************
// To fetch the picture file from the Notes Entity
// *********************************************************var xml = "" +
"<?xml version='1.0' encoding='utf-8'?>" +
"<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
GenerateAuthenticationHeader() +
"<soap:Body>" +
"<Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
"<fetchXml>" +
"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+
"<entity name='annotation'>"+
"<attribute name='filename'/>"+
"<attribute name='documentbody'/>"+
"<attribute name='annotationid'/>"+
"<order attribute='subject' descending='false'/>"+
"<filter type='and'>"+
"<condition attribute='isdocument' operator='eq' value='1'/>"+
"<condition attribute='filename' operator='eq' value='mex9.jpg'/>"+
"</filter>"+
"</entity>"+
"</fetch>"+
" </fetchXml>" +
" </Fetch>" +
" </soap:Body>" +
"</soap:Envelope>";// Prepare the xmlHttpObject and send the request.
var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Fetch");
xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xHReq.setRequestHeader("Content-Length", xml.length);
xHReq.send(xml);// Capture the result.
var resultXml = xHReq.responseXML;// Check for errors.
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0)
{
var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
alert(msg);
}// Process and display the results.
else
{// Capture the result and UnEncode it.
var resultSet = new String();
resultSet = resultXml.text;
resultSet.replace('<','<');
resultSet.replace('>','>');
// Create an XML document that you can parse.
var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
oXmlDoc.async = false;
// Load the XML document that has the UnEncoded results.
oXmlDoc.loadXML(resultSet);// Display the results.
var results = oXmlDoc.getElementsByTagName('result');for (i=0;i < results.length;i++)
{var docBody= results[i].selectSingleNode('./documentbody').nodeTypedValue;}
crmForm.all.IFRAME_Picture.src="";// BE CAREFULL WITH THE QUOTES
var image = "<img alt='', src= 'data:image/jpeg;base64," + docBody + "'> </img>";
ff = document.getElementById("IFRAME_Picture");
ff.contentWindow.document.write(image);
}======================================================================================================
- Proposed as answer by DavidBerryMVP, Moderator Thursday, October 21, 2010 3:03 PM
- Marked as answer by Jim Glass Jr Friday, October 22, 2010 7:27 PM
Thursday, October 21, 2010 2:00 PM
All replies
-
Adi's code is out of date. Since Update Rollup 7 a new security mechanism was put in place for managing attachment downloads. Read up on David Jennaway's posting: http://mscrmuk.blogspot.com/2009/11/ur-7-breaks-attachment-download-code.html
That should help you modify the code with the necessary changes to make it work.
Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com- Proposed as answer by DavidBerryMVP, Moderator Thursday, October 21, 2010 3:02 PM
Sunday, October 17, 2010 7:28 PMModerator -
Adi's code is out of date. Since Update Rollup 7 a new security mechanism was put in place for managing attachment downloads. Read up on David Jennaway's posting: http://mscrmuk.blogspot.com/2009/11/ur-7-breaks-attachment-download-code.html
That should help you modify the code with the necessary changes to make it work.
Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com
Thank you for your reply.... I now know the direction I have to followI am able to retreive the "documentbody" but according to your link and the sample in SDK, I will have to use a file stream in order to encode the info and display in IFRAME since there is no Download.ASPX anymore.
The following is the C# code used in the SDK
================================================================================================
// Download the attachement in the current execution folder.
using (FileStream fileStream = new FileStream(annotationAttachment.filename, FileMode.OpenOrCreate))
{
byte[] fileContent = new UTF8Encoding(true).GetBytes(annotationAttachment.documentbody);
fileStream.Write(fileContent, 0, fileContent.Length);
}
==================================================================================================My question is.... how can I perform this same functionality in Jscript? Is it doable?
I need to be able to create a file stream in JScript and please remember that I am using CRM Online.
I appreciate any help....thanks
Monday, October 18, 2010 4:17 PM -
There appears to be a limit of what IE supports, based on some of the threads I've happened across while researching this. DavidJennaway's response in the thread to which he linked from the posting I sent you suggests that the easiest way would be to use an ASPX page to present the byte-array as an Image to the <img> tag through the "src" attribute. However, understanding that this is CRM Online, I'm aware that you're not able to upload such pages. There may be a clue in this Wikipedia entry: http://en.wikipedia.org/wiki/Data:_URI_scheme#HTML . Even still, you'd have to convert the byte array into base64 encoding. You're welcome to try, however.
Apparently IE 5 - 7 don't support inline data URIs, but there is some suggestion that IE 8 does: http://www.websiteoptimization.com/speed/tweak/inline-images/ and http://dean.edwards.name/weblog/2005/06/base64-ie/
Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com- Proposed as answer by DavidBerryMVP, Moderator Thursday, October 21, 2010 3:02 PM
Monday, October 18, 2010 4:49 PMModerator -
PS: if you finalize a working set of code for this, please post back to this thread or make a blog posting that I can point people to. It would be very exciting to finally have a Javascript-only resolution to this issue for many developers.
Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.comMonday, October 18, 2010 4:52 PMModerator -
PS: if you finalize a working set of code for this, please post back to this thread or make a blog posting that I can point people to. It would be very exciting to finally have a Javascript-only resolution to this issue for many developers.
Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com
Thank you for your input.... I used Data URI and here is the final code.... You are right IE5-7 do not support this but working fine in IE8.=======================================================================================================
// *********************************************************
// To fetch the picture file from the Notes Entity
// *********************************************************var xml = "" +
"<?xml version='1.0' encoding='utf-8'?>" +
"<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
GenerateAuthenticationHeader() +
"<soap:Body>" +
"<Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
"<fetchXml>" +
"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+
"<entity name='annotation'>"+
"<attribute name='filename'/>"+
"<attribute name='documentbody'/>"+
"<attribute name='annotationid'/>"+
"<order attribute='subject' descending='false'/>"+
"<filter type='and'>"+
"<condition attribute='isdocument' operator='eq' value='1'/>"+
"<condition attribute='filename' operator='eq' value='mex9.jpg'/>"+
"</filter>"+
"</entity>"+
"</fetch>"+
" </fetchXml>" +
" </Fetch>" +
" </soap:Body>" +
"</soap:Envelope>";// Prepare the xmlHttpObject and send the request.
var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Fetch");
xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xHReq.setRequestHeader("Content-Length", xml.length);
xHReq.send(xml);// Capture the result.
var resultXml = xHReq.responseXML;// Check for errors.
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0)
{
var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
alert(msg);
}// Process and display the results.
else
{// Capture the result and UnEncode it.
var resultSet = new String();
resultSet = resultXml.text;
resultSet.replace('<','<');
resultSet.replace('>','>');
// Create an XML document that you can parse.
var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
oXmlDoc.async = false;
// Load the XML document that has the UnEncoded results.
oXmlDoc.loadXML(resultSet);// Display the results.
var results = oXmlDoc.getElementsByTagName('result');for (i=0;i < results.length;i++)
{var docBody= results[i].selectSingleNode('./documentbody').nodeTypedValue;}
crmForm.all.IFRAME_Picture.src="";// BE CAREFULL WITH THE QUOTES
var image = "<img alt='', src= 'data:image/jpeg;base64," + docBody + "'> </img>";
ff = document.getElementById("IFRAME_Picture");
ff.contentWindow.document.write(image);
}======================================================================================================
- Proposed as answer by DavidBerryMVP, Moderator Thursday, October 21, 2010 3:03 PM
- Marked as answer by Jim Glass Jr Friday, October 22, 2010 7:27 PM
Thursday, October 21, 2010 2:00 PM -
If you don't have your own blog to put this code in, do you mind if I post it in mine (with credits to you, of course)? I have no other contact information for you, so if you could reach me through my blog profile, I'd love to showcase your work in some way: http://crmentropy.blogspot.com
Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.comThursday, October 21, 2010 3:05 PMModerator -
I do not have a blog yet and you are welcome to post this on yours.... I will direct ppl to your blog when I need to brag about my work... :)
Thanks again for your help...
Friday, October 22, 2010 1:46 PM -
Thank you again, Tito-Z! I've posted your code, and a story about this adventure up at http://crmentropy.blogspot.com/2010/10/tito-zs-iframe-embedded-image-code.html If at any point you would like to host the content yourself, and have me take it down, all you need do is tell me.
Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.comMonday, November 1, 2010 2:24 AMModerator