Quantcast
Channel: Forum CRM Development
Viewing all articles
Browse latest Browse all 1000

Stream SSRS report to PDF from plugin

$
0
0

Hello,

I'm having problems finding CRM 2011 examples of streaming an SSRS report to PDF format from a plugin.  I want to be able to stream an SSRS report to PDF and store the PDF in a server location.

I'm trying to make this code work with the canned CRM 2011, reports but ultimately I want to be able to use the same process against custom CRM reports.

All the examples I find are related to CRM 4.0 and don't work in CRM 2011.

I've been trying to use the following code as a basis for the PDF stream:

ReportExecutionService rs = new ReportExecutionService();
            rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
            rs.Url = "http://SQL1/reportserver/ReportExecution2005.asmx";// Setting Report Parameters// 1. Report path from report server// 2. Output format// 3. Device Infostring reportPath = "/SharedReports/5.0.xxxx/1033{3C3CA962-D4E0-4E62-A9B4-36F00C484D36}";string format = "PDF";string historyID = null;string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";// Passing parameters to report
            ParameterValue[] parameters = new ParameterValue[7]; // Define parameters
            parameters[0] = new ParameterValue();
            parameters[0].Name = "CRM_FilterText";
            parameters[0].Value = "";
            parameters[1] = new ParameterValue();
            parameters[1].Name = "CRM_FormatDate";
            parameters[1].Value = "";
            parameters[2] = new ParameterValue();
            parameters[2].Name = "CRM_FormatTime";
            parameters[2].Value = "";
            parameters[3] = new ParameterValue();
            parameters[3].Name = "CRM_FullName";
            parameters[3].Value = "AccountOverview";
            parameters[4] = new ParameterValue();
            parameters[4].Name = "CRM_URL";
            parameters[4].Value = "http://SQL1/reportserver/ReportService2005.asmx";
            parameters[4].Value = "String";
            parameters[5] = new ParameterValue();
            parameters[5].Name = "SubEntities";
            parameters[5].Value = "";
            parameters[6] = new ParameterValue();
            parameters[6].Name = "CRM_NumberLanguageCode"; 
            parameters[6].Value = "";string encoding;string mimeType;string extension;
            Warning[] warnings = null;string[] streamIDs = null;
            ExecutionInfo execInfo = new ExecutionInfo();           
            ExecutionHeader execHeader = new ExecutionHeader();
            rs.ExecutionHeaderValue = execHeader;
            execInfo = rs.LoadReport(reportPath, historyID);
            rs.SetExecutionParameters(parameters, "en-us");
            String SessionId = rs.ExecutionHeaderValue.ExecutionID;// Data Source settings
            DataSourceCredentials[] credentials = new DataSourceCredentials[1];
            DataSourceCredentials dsc = new DataSourceCredentials();
            dsc.DataSourceName = "CRM";
            dsc.UserName = "kcguise";
            dsc.Password = "password";
            credentials[0]=dsc;
            rs.SetExecutionCredentials(credentials);// Generating reportbyte[] result = null;try
            {          
                result = rs.Render(format, devInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);
            }catch (SoapException err)
            {thrownew Exception(err.Detail.OuterXml);
            }//Encoded data is pdf
            String encodedData = System.Convert.ToBase64String(result);       

The main problem I have is that I don't know how I should set the DataSourceCredentials, and that is where I'm getting a problem. With the above code I get the following error:

{"An error has occurred during report processing. ---> 
Microsoft.ReportingServices.ReportProcessing.ProcessingAbortedException: 
An error has occurred during report processing. ---> Microsoft.ReportingServices.ReportProcessing.ReportProcessingException: 
Cannot create a connection to data source 'CRM'. ---> Microsoft.Crm.Reporting.DataExtensionShim.Common.ReportExecutionException: \n
System.FormatException: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx). ---> 
Microsoft.Crm.Reporting.DataExtensionShim.Common.
ReportExecutionException: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)."}

Supposedly, I'm to use the following code to figure out the DataSourceCredentials, but the problem is that the CrmAuthenticationToken does not seem to be supported in CRM 2011 anymore.

CrmAuthenticationToken token = new CrmAuthenticationToken();
            token.AuthenticationType = 0;
            token.OrganizationName = "CRM";
            CrmService.CrmService service = new CrmService.CrmService();
            service.CrmAuthenticationTokenValue = token;
            service.UseDefaultCredentials = true;//Get Credentials
            WhoAmIRequest req = new WhoAmIRequest();
            WhoAmIResponse whoami = (WhoAmIResponse)_service.Execute(req);string userName = whoami.UserId.ToString();string password = whoami.OrganizationId.ToString();//Create DataSourceCredentials for SRS
            DataSourceCredentials dsc = new DataSourceCredentials();
            dsc.DataSourceName = "CRM";
            dsc.UserName = userName;
            dsc.Password = password;

Does anyone have a working example of streaming an SSRS report in PDF format. Or could you provide some insight on how to get around the DataSourceCredentials issue?


Viewing all articles
Browse latest Browse all 1000

Trending Articles