Search

Email SSRS report using CRM SDK

I have SSRS report on my report server and i need to email that report from source code in .net.
/*Email SSRS report using CRM SDK.. 


-Add web reference in your project and set web reference url of your web service of ssrs e.g.
http://10.1.4.83/ReportServer/ReportExecution2005.asmx

-Add these references in your code editor.
*/
using Microsoft.Crm.Sdk;

using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Sdk.Query; 
using MCS.Crm.emailssrs.ReportExecution; //MCS.Crm.emailssrs is project name 
using Microsoft.Win32; 
using System.Net; 
  
Write a function and add code below.  
 public void SendSSRSReport()
{
string reportServiceUrl = "http://10.1.4.83/ReportServer/ReportExecution2005.asmx"; 

// Create the Report Service Url from the registry
RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\MSCRM", false);
if (key != null)

{

reportServiceUrl = (string)key.GetValue("SQLRSServerURL") + @"/ReportExecution2005.asmx";

}
// Report parameter Name and Value fields are strings.
ReportExecution.ParameterValue[] parameters = new ParameterValue[3];
parameters[0] = new ParameterValue();
parameters[0].Label = "RegistrationNumber";
parameters[0].Name = "RegistrationNumber";
parameters[0].Value = mcs_RegistrationNumber;

parameters[1] = new ParameterValue();
parameters[1].Label = "FromDate";
parameters[1].Name = "FromDate";
parameters[1].Value = _fdate.ToShortDateString();

parameters[2] = new ParameterValue();
parameters[2].Label = "ToDate";
parameters[2].Name = "ToDate";
parameters[2].Value = _tdate.ToShortDateString();

string reportName = "myreport";
// Specify what type of report you want to create HTML,PDF, CVS, Excel, Word, XML, MHTML, Image
string reportFormat = "PDF";

// Specify the device information to control the output of your report.
//For device information See http://msdn2.microsoft.com/en-us/library/ms155397.aspx

string deviceInformation = "2402400";

// Generate a report in a format for a CRM annotation or email attachment
byte[] generatedReport = GenerateSRSbytes(reportName, parameters, reportFormat, deviceInformation, reportServiceUrl, null, null, null);
string _str = Convert.ToBase64String(generatedReport);
string FileName = "myreport.pdf";
string Subject = "Email SSRS Report ";

mcsCrmService= new CrmService();

CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = "TestOrg";
mcsCrmService.Url ="http://10.1.4.83:5555/mscrmservices/2007/crmservice.asmx";
mcsCrmService.PreAuthenticate = true;
mcsCrmService.CrmAuthenticationTokenValue = token;
mcsCrmService.Credentials=System.Net.CredentialCache.DefaultCredentials;

Guid _to = new Guid("e1b67b12-1e8a-4bb2-9c86-fdb8f75dda4d");//To email address GUID
Guid _from = new Guid("a1e9c6a2-73e5-4117-a8c9-c15b89a92203");//From email address GUID 
  
// Create an activity party for the email 

activityparty party = new activityparty();
party.partyid = new Lookup();
party.partyid.type = EntityName.systemuser.ToString();
party.partyid.Value = _from;


activityparty party2 = new activityparty();
party2.partyid = new Lookup();
party2.partyid.type = EntityName.account.ToString();
party2.partyid.Value = _to;



//Create email template in CRM and get GUID of that template from Internet Explorer:
Guid _templateid = new Guid("a783eea1-222b-461c-9c1f-e8185fd5504d");//template GUID
InstantiateTemplateRequest _insTemplate = new InstantiateTemplateRequest();//Initialize template object.
_insTemplate.TemplateId=_templateid;
_insTemplate.ObjectId = (Guid)party2.partyid.Value;
_insTemplate.ObjectType=party2.partyid.type;

InstantiateTemplateResponse _tempResponse=(InstantiateTemplateResponse)mcsCrmService.Execute(_insTemplate);

// Create an email message.


email email = (email)_tempResponse.BusinessEntityCollection.BusinessEntities[0];


// Set email properties
email.from = new activityparty[] { party };
email.to = new activityparty[] { party2 };
email.regardingobjectid = party2.partyid;
email.subject = Subject ;
email.directioncode = new CrmBoolean(true);

Guid emailID = mcsCrmService.Create(email);
attachment.activityid = new Lookup();
attachment.activityid.Value = emailID;
attachment.activityid.type = EntityName.email.ToString();
attachment.filename = FileName;
attachment.attachmentnumber = new CrmNumber();
attachment.body = _str;
attachment.mimetype = @"application/pdf";

// Create the Attachment in CRM. 
Guid attachmentId = mcsCrmService.Create(attachment);

//Send Email.

SendEmailRequest _sendEmailReq = new SendEmailRequest();
_sendEmailReq.EmailId = emailID;
_sendEmailReq.IssueSend = true;
_sendEmailReq.TrackingToken = "";

mcsCrmService.Execute(_sendEmailReq); 
  
} 
public byte[] GenerateSRSbytes(string reportPath, ParameterValue[] parameters, string outputFormat, string deviceInformation, string ReportServiceUrl, string userName, string passWord, string domainName) 
{

string encoding;
string mimeType;
string extension;
string[] streamIDs;
string SessionId;
string historyID = null;
Warning[] warnings;

// By default the Report will run with the permissions of the AD authenticated User.
ReportExecutionService rs = new ReportExecutionService();
rs.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
rs.Url = ReportServiceUrl;

// Impersonate credentials if they are specified. 
if (userName != null && passWord != null)
{
if (domainName == null)
{
rs.Credentials = new NetworkCredential(userName, passWord);
}
else
{
rs.Credentials = new NetworkCredential(userName, passWord, domainName);
}
}

// Set timeout in seconds of the report takes a long time.
rs.Timeout = 600000;
ExecutionHeader execHeader = new ExecutionHeader();
rs.ExecutionHeaderValue = execHeader;
ExecutionInfo execInfo = new ExecutionInfo();
execInfo = rs.LoadReport(reportPath, historyID);
rs.SetExecutionParameters(parameters, "en-us");
SessionId = rs.ExecutionHeaderValue.ExecutionID;

// Render Report
return rs.Render(outputFormat, deviceInformation, out extension, out mimeType, out encoding, out warnings, out streamIDs);

}