Hi
I have written the below code and created a custom entity ie Recurring Process where iam storing the report url .i have hardcoded the code for testing to generate the report in pdf ,The event is on save of the recurring process.Getting 400 error
function sendReportAsPDFInEmail() {
//1. Execution starts here.
var arrReportSession = EmailReport(); //2. Execute the ssrs report and capture the response. Response will be an array.
convertResponseToPDF(arrReportSession); //3. Convert the response in base 64 string i.e. PDF.
}
function EmailReport() {
var selectedid = null;
var reportid = null;
var newPth = null;
var req = new XMLHttpRequest();
var reportName = "Report Test";
var context = Xrm.Page.context;
var serverUrl = context.getClientUrl();
var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
var retrieveResult = new XMLHttpRequest();
retrieveResult.open("GET", ODataPath + "/ReportSet?$select=Name,ReportId&$filter=Name eq'" + reportName + "'", false);
retrieveResult.setRequestHeader("Accept", "application/json");
retrieveResult.setRequestHeader("Content-Type", "application/json; charset=utf-8?");
retrieveResult.send();
if (retrieveResult.readyState == 4 /* complete */)
{
if (retrieveResult.status == 200)
{
var retrieved = this.parent.JSON.parse(retrieveResult.responseText).d;
var Result = retrieved.results;
if (typeof Result !== "undefined")
{
reportid = Result[0].ReportId;
var params = getReportingSession(reportName, reportid);
// newPth = Xrm.Page.context.getClientUrl() + "/Reserved.ReportViewerWebControl.axd?ReportSession=" + params[0] + "&Culture=1033&CultureOverrides=True&UICulture=1033&UICultureOverrides=True&ReportStack=1&ControlID=" + params[1] + "&OpType=Export&FileName=" + reportName + "&ContentDisposition=OnlyHtmlInline&Format=PDF";
newPth = Xrm.Page.context.getClientUrl() + "/Reserved.ReportViewerWebControl.axd?ReportSession=" + params[0] + "&Culture=1033&CultureOverrides=True&UICulture=1033&UICultureOverrides=True&ReportStack=1&ControlID=" + params[1] + "&OpType=Export&FileName=Public&ContentDisposition=OnlyHtmlInline&Format=PDF";
//convertResponseToPDF(newPth);
}
}
}
return newPth;
}
function getReportingSession(reportName, reportGuid)
{
var reportPrefilter = " ";
reportName = "Report Test";
reportGuid = "2B5A0201-D382-E911-A973-000D3AF06590";
var selectedIds = Xrm.Page.data.entity.getId();
selectedIds = selectedIds.replace('{', '');
selectedIds = selectedIds.replace('}', '');
var pth = Xrm.Page.context.getClientUrl() + "/CRMReports/rsviewer/reportviewer.aspx";
var retrieveEntityReq = new XMLHttpRequest();
//var strParameterXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'><entity name='quote'><all-attributes /><filter type='and'><condition attribute='quoteid' operator='eq' value='" + selectedIds + "' /> </filter></entity></fetch>";
retrieveEntityReq.open("POST", pth, false);
retrieveEntityReq.setRequestHeader("Accept", "*/*");
retrieveEntityReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
retrieveEntityReq.send("id=%7B" + reportGuid + "%7D&uniquename=" + Xrm.Page.context.getOrgUniqueName()+ "&reportnameonsrs=&reportName=" + reportName);
var x = retrieveEntityReq.responseText.lastIndexOf("ReportSession=");
var y = retrieveEntityReq.responseText.lastIndexOf("ControlID=");
var ret = new Array();
ret[0] = retrieveEntityReq.responseText.substr(x + 14, 24);
ret[1] = retrieveEntityReq.responseText.substr(x + 10, 32);
return ret;
}
function convertResponseToPDF(pth) {
//Create query string that will be passed to Report Server to generate PDF version of report response.
//Create request object that will be called to convert the response in PDF base 64 string.
var retrieveEntityReq = new XMLHttpRequest();
retrieveEntityReq.open("GET", encodeURIComponent(pth), true);
retrieveEntityReq.setRequestHeader("Accept", "*/*");
retrieveEntityReq.responseType = "arraybuffer";
retrieveEntityReq.onreadystatechange = function () { // This is the callback function.
if (retrieveEntityReq.readyState == 4 && retrieveEntityReq.status == 200)
{
var binary = "";
var bytes = new Uint8Array(this.response);
for (var i = 0; i < bytes.byteLength; i++)
{
binary += String.fromCharCode(bytes[i]);
}
//This is the base 64 PDF formatted string and is ready to pass to the action as an input parameter.
var base64PDFString = btoa(binary);
//set the value of binary string in a string field in dynamics crm
alert(base64PDFString);
Xrm.Page.getAttribute("im360_binarystringmultilinetext").setValue(base64PDFString);
//4. Call Action and pass base 64 string as an input parameter. That’s it.
createNoteWithAttachment(base64PDFString);
}
};
//This statement sends the request for execution asynchronously. Callback function will be called on completion of the request.
retrieveEntityReq.send();
}
//Here this function attach the report as pdf in the existing task id.
function createNoteWithAttachment(bdy){
var entity = {};
entity.mimetype = "application/pdf";
entity.filename = "Test File Name";
entity["objectid_im360_recurringprocess@odata.bind"] = "/im360_recurringprocesses(85ee2976-8c7c-e911-a973-000d3af06590)";
entity.documentbody = bdy;
entity.subject = "Test Subject";
Xrm.WebApi.online.createRecord("annotation", entity).then(
function success(result) {
var newEntityId = result.id;
},
function(error) {
Xrm.Utility.alertDialog(error.message);
}
);
}
Can anyone please help me on this?
*This post is locked for comments