Thursday 23 July 2015

Sample Code For creating Plugins For sending Email in crm

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk.Messages;
namespace Plugins
{
    public class Folloeplugins:IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            //Extract the tracing service for use in debugging sandboxed plug-ins.
            ITracingService tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = (IPluginExecutionContext)
                serviceProvider.GetService(typeof(IPluginExecutionContext));
            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
            {
                // Obtain the target entity from the input parameters.
                Entity entity = (Entity)context.InputParameters["Target"];

                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                try
                {
                   
                  
                        Entity email = new Entity("email");
                        Entity fromparty=new Entity("activityparty");
                        Entity toparty=new Entity("activityparty");
                        toparty["partyid"] = new EntityReference("systemuser", context.UserId);
                        fromparty["partyid"] = new EntityReference("systemuser", context.UserId);
                        email["from"] = new Entity[] { fromparty };
                        email["to"] = new Entity[] { toparty };
                        email["subject"] = "email subject - " + DateTime.Now.ToString();
                        email["description"] = "email description";


                        email["regardingobjectid"] = new EntityReference("account", entity.Id);
                       
                      
                        Guid emailid=  service.Create(email);
                        SendEmailRequest sendEmailreq = new SendEmailRequest
                        {
                            EmailId = emailid,
                            TrackingToken = "",
                            IssueSend = true
                        };
                        SendEmailResponse sendEmailresp = (SendEmailResponse)service.Execute(sendEmailreq);
                     
                   
                }
                catch (FaultException<OrganizationServiceFault> ex)
                {
                    throw new InvalidPluginExecutionException("An error occurred in the FollupupPlugin plug-in.", ex);
                }
                   
                }
            }

        }
    }
 

No comments:

Post a Comment