1.step: In Visual Studio creating new WORKFLOW ACTIVITY LIBRARY
2.step: Add references Microsoft.Crm.Sdk, Microsoft.Crm.SdkTypeProxy;
3.step: Add web reference
http://<reportservername>/ReportServer/ReportExecution2005.asmx
4.step: Debug this code
5.step: Run Plugin Registration Tool and register new assembly (that will be dll created from this source in path where this project will be created in bin folder)
6.step: Create new Workflow in crm with this custom workflow activity as new step
And here goes full code:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Crm.Workflow;
using System.Workflow.Activities;
using System.Workflow.ComponentModel;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.SdkTypeProxy.Metadata;
using Microsoft.Crm.Sdk.Metadata;
using SendingReport;
using System.IO;
using System.Web.Services.Protocols;
using Microsoft.Crm.Sdk.Query;
using CustomEmail.ReportService;
namespace SendingReport
{
[CrmWorkflowActivity("Send PDF Report")] // Name of custom workflow activity
public class SendReport : SequenceActivity
{
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));
IWorkflowContext context = contextService.Context;
ICrmService service = context.CreateCrmService();
Guid incidentId = context.PrimaryEntityId; // Get Id of current entity on which workflow will run
byte[] result = null;
// API Reporting Service
CustomEmail.ReportService.ReportExecutionService rs = new ReportExecutionService();
//rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
rs.Credentials = new System.Net.NetworkCredential("username", "password", "domain");
rs.Url = "http://crmservername/ReportServer/ReportExecution2005.asmx";
// Setting Report Parameters
// 1. Report path from report server
// 2. Output format
// 3. Device Info
string reportPath = "/path/reportname"; //example (/myreports/jobreport)
string format = "PDF";
string historyID = null;
string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";
// Passing parameters to report
ParameterValue[] parameters = new ParameterValue[1]; // Define parameters
parameters[0] = new ParameterValue();
parameters[0].Name = "incidentid"; // incident GUID
parameters[0].Value = incidentId.ToString();
// Security settings
DataSourceCredentials[] credentials = null;
string showHideToggle = null;
string encoding;
string mimeType;
string extension;
Warning[] warnings = null;
ParameterValue[] reportHistoryParameters = 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;
try
{
// Generating report
result = rs.Render(format, devInfo, out extension, out encoding, out mimeType, out warnings, out
streamIDs);
}
catch (SoapException err)
{
throw new Exception(err.Detail.OuterXml);
}
//Creating email
email mail = new email();
activityparty mailfrom = new activityparty();
mailfrom.partyid = new Lookup();
mailfrom.partyid.type = EntityName.systemuser.ToString();
mailfrom.partyid.Value = Sender.Value;
mail.from = new activityparty[] { mailfrom };
activityparty mailto = new activityparty();
mailto.partyid = new Lookup();
mailto.partyid.type = EntityName.contact.ToString();
mailto.partyid.Value = MailRecipient.Value;
mail.to = new activityparty[] { mailto };
activityparty mailcc = new activityparty();
mailcc.partyid = new Lookup();
mailcc.partyid.type = EntityName.contact.ToString();
mailcc.partyid.Value = MailRecipientCC.Value;
mail.cc = new activityparty[] { mailcc };
//Get attributes from current entity
incident ret = (incident)service.Retrieve(EntityName.incident.ToString(), incidentId, new AllColumns());
mail.subject = "Job report";
mail.sender = "example@someone.com";
mail.description = "Here goes mail body description for: " + ret.ticketnumber.ToString();
mail.ownerid = new Owner();
mail.ownerid.type = EntityName.systemuser.ToString();
mail.ownerid.Value = context.UserId;
Guid createdEmailGuid = service.Create(mail);
// Attaching Pdf report
activitymimeattachment setupEmailAttachment = new activitymimeattachment();
setupEmailAttachment.subject = "Attachment subject";
setupEmailAttachment.filename = "Reportname.pdf";
setupEmailAttachment.body = Convert.ToBase64String(result);
setupEmailAttachment.filesize = new CrmNumber(Convert.ToInt32(result.Length.ToString()));
setupEmailAttachment.mimetype = "text/plain";
setupEmailAttachment.attachmentnumber = new CrmNumber();
setupEmailAttachment.attachmentnumber.Value = 1;
setupEmailAttachment.activityid = new Lookup();
setupEmailAttachment.activityid.type = EntityName.email.ToString();
setupEmailAttachment.activityid.Value = createdEmailGuid;
service.Create(setupEmailAttachment);
// Sending email with attachment
SendEmailRequest sendrequest = new SendEmailRequest();
sendrequest.EmailId = createdEmailGuid;
sendrequest.TrackingToken = "";
sendrequest.IssueSend = true;
service.Execute(sendrequest);
return ActivityExecutionStatus.Closed;
}
// Define lookup fields [from, to, cc], these you will use when setting parameters in CRM for this workflow
public static DependencyProperty SenderProperty = DependencyProperty.Register("Sender", typeof
(Lookup), typeof(SendReport));
[CrmInput("Sender")]
[CrmReferenceTarget("systemuser")]
public Lookup Sender
{
get
{
return (Lookup)base.GetValue(SenderProperty);
}
set
{
base.SetValue(SenderProperty, value);
}
}
public static DependencyProperty MailRecipientProperty = DependencyProperty.Register("MailRecipient", typeof
(Lookup), typeof(SendReport));
[CrmInput("MailRecipient")]
[CrmReferenceTarget("contact")]
public Lookup MailRecipient
{
get
{
return (Lookup)base.GetValue(MailRecipientProperty);
}
set
{
base.SetValue(MailRecipientProperty, value);
}
}
public static DependencyProperty MailRecipientCCProperty = DependencyProperty.Register("MailRecipientCC", typeof
(Lookup), typeof(SendReport));
[CrmInput("MailRecipientCC")]
[CrmReferenceTarget("contact")]
public Lookup MailRecipientCC
{
get
{
return (Lookup)base.GetValue(MailRecipientCCProperty);
}
set
{
base.SetValue(MailRecipientCCProperty, value);
}
}
}
}
Thanks for the code, it is very helpful to me.
ReplyDelete