This illustration shows how to send an email synchronously in Microsoft Dynamics CRM 2011 with SendEmailRequest. This example will be given in Jscript (SOAP) and in C# (.NET).
NOTE: Yesterday's post covered creating the email activity in CRM in code (a prerequisite to today's tutorial). Check it out if you are having trouble creating the entity: http://mileyja.blogspot.com/2012/02/create-email-activity-in-microsoft.html
If you need help instantiating a service object in .NET within a plugin check out this post:
http://mileyja.blogspot.com/2011/04/instantiating-service-object-within.html
NOTE: Yesterday's post covered creating the email activity in CRM in code (a prerequisite to today's tutorial). Check it out if you are having trouble creating the entity: http://mileyja.blogspot.com/2012/02/create-email-activity-in-microsoft.html
Ok, here is what the code looks like!
First in C#:
SendEmailRequest ser = new SendEmailRequest();
//Issue send specifies whether to really send the email, or just mark it sent.
ser.IssueSend = true;
//Guid of email activity to send
ser.EmailId = new Guid("dc946d36-3e41-e111-9d05-0050569838b7");
ser.TrackingToken = "";
service.Execute(ser);
If you need help instantiating a service object in .NET within a plugin check out this post:
http://mileyja.blogspot.com/2011/04/instantiating-service-object-within.html
Now here is the Jscript nicely formatted by the CRM 2011 SOAP formatter. Available at: http://crm2011soap.codeplex.com/
Now in Jscript
Now in Jscript
if (typeof (SDK) == "undefined")
{ SDK = { __namespace: true }; }
//This will establish a more unique namespace for functions in this library. This will reduce the
// potential for functions to be overwritten due to a duplicate name when the library is loaded.
SDK.SAMPLES = {
_getServerUrl: function () {
///<summary>
/// Returns the URL for the SOAP endpoint using the context information available in the form
/// or HTML Web resource.
///</summary>
var ServicePath = "/XRMServices/2011/Organization.svc/web";
var serverUrl = "";
if (typeof GetGlobalContext == "function") {
var context = GetGlobalContext();
serverUrl = context.getServerUrl();
}
else {
if (typeof Xrm.Page.context == "object") {
serverUrl = Xrm.Page.context.getServerUrl();
}
else
{ throw new Error("Unable to access the server URL"); }
}
if (serverUrl.match(/\/$/)) {
serverUrl = serverUrl.substring(0, serverUrl.length - 1);
}
return serverUrl + ServicePath;
},
SendEmailRequest: function () {
var requestMain = ""
requestMain += "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
requestMain += " <s:Body>";
requestMain += " <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
requestMain += " <request i:type=\"b:SendEmailRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
requestMain += " <a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
requestMain += " <a:KeyValuePairOfstringanyType>";
requestMain += " <c:key>EmailId</c:key>";
requestMain += " <c:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">dc946d36-3e41-e111-9d05-0050569838b7</c:value>";
requestMain += " </a:KeyValuePairOfstringanyType>";
requestMain += " <a:KeyValuePairOfstringanyType>";
requestMain += " <c:key>IssueSend</c:key>";
requestMain += " <c:value i:type=\"d:boolean\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">true</c:value>";
requestMain += " </a:KeyValuePairOfstringanyType>";
requestMain += " <a:KeyValuePairOfstringanyType>";
requestMain += " <c:key>TrackingToken</c:key>";
requestMain += " <c:value i:type=\"d:string\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\" />";
requestMain += " </a:KeyValuePairOfstringanyType>";
requestMain += " </a:Parameters>";
requestMain += " <a:RequestId i:nil=\"true\" />";
requestMain += " <a:RequestName>SendEmail</a:RequestName>";
requestMain += " </request>";
requestMain += " </Execute>";
requestMain += " </s:Body>";
requestMain += "</s:Envelope>";
var req = new XMLHttpRequest();
req.open("POST", SDK.SAMPLES._getServerUrl(), false)
req.setRequestHeader("Accept", "application/xml, text/xml, */*");
req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
req.send(requestMain);
//work with the response here
//var strResponse = req.responseXML.xml;
//alert(strResponse.toString());
},
__namespace: true
};
To understand how to parse the response please review my post on using the DOM parser.
Now you can call the SDK.SAMPLES.SendEmailRequest function from your form jscript handler.
Thats all there is to it!
-
-
This comment has been removed by the author.
ReplyDeleteHi. On crm 2015 online update 1 all works. But in line "alert(strResponse.toString())" error. req.responseXML.xml is "undefined".
ReplyDeleteHey MileyJa! Blessings! very very very thanks!
ReplyDelete