web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Generating report as pdf in Dynamics 365 online version

(0) ShareShare
ReportReport
Posted on by 105

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

I have the same question (0)
  • Suggested answer
    Kalpavruksh D365 CoE Profile Picture
    2,545 on at
    RE: Generating report as pdf in Dynamics 365 online version

    Hi Arunav,

    Please refer to this link for more details: community.dynamics.com/.../270356

    If found useful, please mark this answer as verified.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

#3
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans