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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Unanswered

on Demand Custom WorkFlow for send SSRS Email Report

(0) ShareShare
ReportReport
Posted on by 65

Dears

I created a custom workflow activity to run SSRS report and generate pdf file then attach it in Email .

when I run this On demand  workflow for one entity it is working fine and generate the ssrs report and attached fine.

but if I select multiple entities it is giving error as below

my code is here

public sealed class RenderQuoteReport : CodeActivity
{

[Input("E-Mail")]
[ReferenceTarget("email")]
public InArgument Email { get; set; }

protected override void Execute(CodeActivityContext executionContext)
{
IWorkflowContext context = executionContext.GetExtension();
IOrganizationServiceFactory serviceFactory =executionContext.GetExtension();
IOrganizationService service = serviceFactory.CreateOrganizationService(null);

//Server
ReportGenerator rg = new ReportGenerator("http://servername/Reportserver/ReportExecution2005.asmx", new NetworkCredential("user", "password", "Org"));
Entity entity = RetrieveEntityById(service, "new_testinvoices", context.PrimaryEntityId);
ParameterValue[] parameters = new ParameterValue[1];
parameters[0] = new ParameterValue();
parameters[0].Name = "ReportParameter1";
String strInvoiceName = string.Empty;

if (entity.Attributes.Contains("new_name"))
{
strInvoiceName = entity.Attributes["new_name"].ToString();
parameters[0].Value = strInvoiceName;
}

byte[] reportresult = rg.Render("/OrgCRM_MSCRM/Invoice", FormatType.PDF, parameters);

if (reportresult != null)
{
Entity attachment = new Entity("activitymimeattachment");
attachment["objectid"] = Email.Get(executionContext);
attachment["objecttypecode"] = "email";
attachment["filename"] = "GInvoice.pdf";
attachment["subject"] = "Accounts List";
attachment["body"] = System.Convert.ToBase64String(reportresult);
try
{
service.Create(attachment);
}
catch (Exception ex)
{
throw new Exception(string.Format("Error creating attachment - {0}", ex.Message));
}
try
{
service.Execute(new SendEmailRequest()
{
EmailId = Email.Get(executionContext).Id,
IssueSend = true,
TrackingToken = string.Empty
});
}
catch (Exception ex)
{
throw new Exception(string.Format("Error sending email - {0}", ex.Message));
}
}



}


private Entity RetrieveEntityById(IOrganizationService service, string strEntityLogicalName, Guid guidEntityId)
{

Entity RetrievedEntityById = service.Retrieve(strEntityLogicalName, guidEntityId, new ColumnSet(true));

return RetrievedEntityById;

}




internal class ReportGenerator
{
#region Privates

ReportExecutionService _reportexecutionservice = null;

#endregion Privates

#region CTOR

internal ReportGenerator(string ServiceUrl, ICredentials credentials)
{
if (string.IsNullOrEmpty(ServiceUrl))
throw new Exception("Parameter ServiceUrl has to contain value");

if (credentials == null)
throw new Exception("Parameter Credentials has to contain value");

_reportexecutionservice = new ReportExecutionService()
{
Credentials = credentials,
Url = ServiceUrl
};
}

#endregion CTOR

#region Methods

internal byte[] Render(string Report, FormatType formattype)
{
return this.Render(Report, formattype, new ParameterValue[] { });
}

internal byte[] Render(string Report, FormatType formattype, ParameterValue[] parameters)
{
byte[] result = null;
string format = GetFormatType(formattype);
string historyID = null;
string devInfo = @"False";
string encoding;
string mimeType;
string extension;
Warning[] warnings = null;
string[] streamIDs = null;

try
{

ExecutionInfo execInfo = new ExecutionInfo();
ExecutionHeader execHeader = new ExecutionHeader();
_reportexecutionservice.ExecutionHeaderValue = execHeader;
execInfo = _reportexecutionservice.LoadReport(Report, historyID);
_reportexecutionservice.SetExecutionParameters(parameters, "en-us");
result = _reportexecutionservice.Render(format, devInfo, out extension,out mimeType, out encoding, out warnings, out streamIDs);

}
catch (Exception ex)
{
if (ex is SoapException)
{
SoapException sexc = ex as SoapException;
throw new Exception(string.Format("Error generating report - {0}", sexc.Detail.InnerText));
}
else
{
throw new Exception(string.Format("Error generating report - {0}", ex.Message));
}
}
_reportexecutionservice.Dispose();
return result;
}

private string GetFormatType(FormatType formattype)
{
switch (formattype)
{
case FormatType.XML:
case FormatType.CSV:
case FormatType.IMAGE:
case FormatType.PDF:
case FormatType.MHTML:
case FormatType.EXCEL:
case FormatType.Word:
return formattype.ToString();
case FormatType.HTML40:
return "HTML4.0";
case FormatType.HTML32:
return "HTML3.2";
default:
throw new Exception(string.Format("Rendering type {0} is not available", formattype));
}
}

#endregion Methods
}

internal enum FormatType
{
XML, CSV, IMAGE, PDF, HTML40, HTML32, MHTML, EXCEL, Word
}
8311.wf.JPGrun.JPG

00543.error.JPG

I have the same question (0)

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
11manish Profile Picture

11manish 153

#2
ManoVerse Profile Picture

ManoVerse 147 Super User 2026 Season 1

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 66 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans