Tuesday 24 April 2012

Plug is for sending the sms in dynamic crm 2011.

First need to do the early binding and add it to the classlibrary.
then give the required references:

then the coding is:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xrm;
using System.Diagnostics;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System.Windows.Browser;
using System.Net;
using System.IO;
using System.ServiceModel.Description;

using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Client;





namespace mop.men.SendSMS
{
    public class SendingSms : IPlugin
    {

        public const string AUTHORIZE = "http:\link";

        string ret = null;
        private OrganizationServiceProxy _serviceProxy;

        public void Execute(IServiceProvider serviceProvider)
        {
           

            IPluginExecutionContext context = (IPluginExecutionContext)
            serviceProvider.GetService(typeof(IPluginExecutionContext));

            Entity entity;

            // Check if the input parameters property bag contains a target
            // of the create operation and that target is of type Entity.
            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {

                // Obtain the target business entity from the input parameters.
                entity = (Entity)context.InputParameters["Target"];

                // Verify that the entity represents a contact.
                if (entity.LogicalName != "new_sms")
                {
                    return;
                }
            }
            else
            {
                return;
            }

            try
            {
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                Entity entity1 = (Entity)context.InputParameters["Target"];

                ColumnSet cols = new ColumnSet(
                                     new String[] { "subject", "new_phonenumber" });

                var contact = service.Retrieve("new_sms", entity1.Id, cols);

                string subjects = contact["subject"].ToString();
                string phonenumber = contact["new_phonenumber"].ToString();

                ret = AUTHORIZE + "?no=" + phonenumber + "&msg=" + subjects;

              //  WebRequest request = WebRequest.Create("your url comes here with parameters and stuf like in IE");
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ret);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                SetStateRequest setStatePhoneCall = new SetStateRequest
                {
                    EntityMoniker = new EntityReference(new_sms.EntityLogicalName, entity1.Id),
                    State = new OptionSetValue((int)slk_smsState.Completed),
                    Status = new OptionSetValue(-1)
                };

                service.Execute(setStatePhoneCall);

                //Console.WriteLine("PhoneCall entity instance has been marked as completed.");

                //PhoneCallState.Completed

            }
            catch (FaultException<OrganizationServiceFault> ex)
            {
                throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
            }
        }

    }

}


and register this plug in for the create 

then doing customize in ribbon area for sending button:

<CustomAction Id="ss_SendSMS" Location="Mscrm.Form.new_sms.MainTab.Save.Controls._children" Sequence="1">
            <CommandUIDefinition>
              <Button Id="btn_newSendSMS" Command="Mscrm.RunSomeJS" LabelText="Send SMS" ToolTipTitle="Send SMS" ToolTipDescription="Sends the SMS" TemplateAlias="o1" Image16by16="/_imgs/SFA/SendAsEmail_16.png" Image32by32="/_imgs/SFA/SendAsEmail_32.png" />
            </CommandUIDefinition>
          </CustomAction>


<CommandDefinition Id="Mscrm.RunSomeJS">
            <EnableRules> </EnableRules>
            <DisplayRules />
            <Actions>
              <JavaScriptFunction FunctionName="SendSMS" Library="$Webresource:new_sendingsms"/>
            </Actions>
          </CommandDefinition>


the Java script is : 

function SendSMS()
{
Xrm.Page.data.entity.save("save");
}


No comments:

Post a Comment