-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetPDF.cs
More file actions
141 lines (130 loc) · 6.1 KB
/
getPDF.cs
File metadata and controls
141 lines (130 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using System;
using System.Activities;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using MOCD.CS.Report.Generate.ReportService;
using Microsoft.Crm.Sdk.Messages;
using Newtonsoft.Json;
namespace MOCD.CS.Report.Generate
{
public class getPDF : CodeActivity
{
[Input("E-Mail")]
[ReferenceTarget("email")]
public InArgument<EntityReference> Email { get; set; }
[Input("Report Name")]
public InArgument<string> RptName { get; set; }
[Input("ApplicationNum")]
public InArgument<string> AppNum { get; set; }
protected override void Execute(CodeActivityContext executionContext)
{
var tracingService = executionContext.GetExtension<ITracingService>();
var context = executionContext.GetExtension<IWorkflowContext>();
var serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
var service = serviceFactory.CreateOrganizationService(context.UserId);
try
{
Guid RecordId = context.PrimaryEntityId;
string _rpName = RptName.Get<string>(executionContext);
string _appNum = AppNum.Get<string>(executionContext);
byte[] result = null;
ReportExecutionService rs = new ReportExecutionService();
rs.Credentials = new NetworkCredential("<Username>", "<Password>", "<Domain>");
rs.Url = "<Org_URL>/ReportExecution2005.asmx";
tracingService.Trace(context.OrganizationName);
string reportPath = "/"+context.OrganizationName+"_MSCRM/"+_rpName;
string format = "PDF";
string historyID = null;
string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";
string encoding;
string mimeType;
string extension;
Warning[] warnings = null;
string[] streamIDs = null;
ParameterValue[] parameters = new ParameterValue[1];
parameters[0] = new ParameterValue();
parameters[0].Name = "id";
parameters[0].Value = RecordId.ToString();
tracingService.Trace("Id passed: " + RecordId.ToString());
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;
result = rs.Render(format, devInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);
Response.Rootobject response = this.GetStamp(Convert.ToBase64String(result), Convert.ToInt32(result.Length.ToString()), _appNum, "<Token>", "<BlockChain_URL>");
string StamppedFile = response.UploadZipHash;
//Create email activity
Entity attachment = new Entity("activitymimeattachment");
attachment["objectid"] = Email.Get<EntityReference>(executionContext);
attachment["objecttypecode"] = "email";
attachment["filename"] = "الشهادة.pdf";
attachment["subject"] = "لمن يهمه الأمر";
attachment["body"] = System.Convert.ToBase64String(response.StampContent);
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<EntityReference>(executionContext).Id,
IssueSend = true,
TrackingToken = string.Empty
});
}
catch (Exception ex)
{
throw new Exception(string.Format("Error sending email - {0}", ex.Message));
}
}
catch (Exception err)
{
throw new Exception("Exception" + err);
}
}
private Response.Rootobject GetStamp(string filebody,int filesize, string filetag, string token, string APoURL)
{
string str = JsonConvert.SerializeObject(new Request()
{
AccountKey = token,
Operation = "8",
Culture = "ar-AE",
FileName = (filetag + ".pdf"),
FileType = "application/pdf",
FileSize = filesize.ToString(),
Tags = ("FL" + filetag),
Base64Content = filebody
});
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(APoURL);
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/json";
httpWebRequest.ContentLength = (long)str.Length;
using (Stream requestStream = httpWebRequest.GetRequestStream())
{
using (StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.ASCII))
streamWriter.Write(str);
}
using (Stream stream = httpWebRequest.GetResponse().GetResponseStream() ?? Stream.Null)
{
using (StreamReader streamReader = new StreamReader(stream))
{
Response response = new Response();
return JsonConvert.DeserializeObject<Response.Rootobject>(streamReader.ReadToEnd());
}
}
}
}
}