Pages

Tuesday 19 November 2013

Send SMS to contact in Dynamics CRM


I have developed a plugin for send a text message to contact when a contact is created in crm.

Iam using site2sms api. We need to have a site2sms account and pass those credentials in web request. Using this, we can send unlimited text messages.

But we cannot send messages to contact who are registered their mobile number with DND activation.


Plugin code:


publicclass SendSms:IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
string emailobj = string.Empty;
Guid Conid = Guid.Empty;
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = (IOrganizationService)serviceFactory.CreateOrganizationService(context.UserId);
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity Cont = context.InputParameters["Target"] as Entity;
string to = Cont.Attributes["mobilephone"].ToString();
string customer = Cont.Attributes["fullname"].ToString();
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(@"https://site2sms.p.mashape.com/index.php?uid=xxxxxxxxxx&pwd=xxxxx&phone=" + to + "&msg=type message here");
request.Headers.Add(
"X-Mashape-Authorization", "xxxxxxxxxxxxxx");
request.ContentType =
"application/json";
request.MaximumAutomaticRedirections = 4;
request.MaximumResponseHeadersLength = 4;
request.AllowAutoRedirect =
true;
request.KeepAlive =
true;
request.Method =
"GET";
request.Timeout = 50000;
request.Credentials =
CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
}
}
}