Pages

Tuesday, April 15, 2014

Dynamics CRM 2011 Activities Code & Configuration

Dynamics CRM 2011 Activities Code & Configuration

1.Activities Code

//Added Service Reference, reference name is 'ServiceEmailAudit'
//Code to Create Activities, this will get input field and call webservice
using System;
using System.Activities;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using System.Linq;
using DNS.CRM.Entities;

namespace CRM.EmailAudit.Activities
{
    public class GenerateDAEmail : CodeActivity
    {
        [RequiredArgument]
        [Input("Report Name")]
        public InArgument<string> iReportName { get; set; }

        [RequiredArgument]
        [Input("Report Format")]
        public InArgument<string> iReportFormat { get; set; }

        /*[RequiredArgument]
        [Input("Parameter Name")]
        public InArgument<string> iParameterName { get; set; }*/

        [Input("Parameter Value")]
        [ReferenceTarget(dns_match.EntityLogicalName)]
        public InArgument<EntityReference> iMatch { get; set; }
       
        [Input("CC To User(List of Emails Seperated by ';')")]
        public InArgument<string> iCCEmail { get; set; }

        [Input("Attachment?")]
        public InArgument<Boolean> iAttachment { get; set; }
       
        [Input("Attachment Name")]
        public InArgument<string> iAttachmentName { get; set; }

        [Input("Attachment Format")]
        public InArgument<string> iAttachmentFormat { get; set; }

        [Input("WCF Service URL")]
        public InArgument<string> iWCFServiceURL { get; set; }

        protected override void Execute(CodeActivityContext executionContext)
        {
            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
            IOrganizationServiceFactory sericeFacotry = executionContext.GetExtension<IOrganizationServiceFactory>();
            IOrganizationService service = sericeFacotry.CreateOrganizationService(context.UserId);
            //XrmServiceContext xrmContext = new XrmServiceContext(service);

            string rptName = string.Empty;
            string rptFormat = string.Empty;
            string ccEmail = string.Empty;
            string attName = string.Empty;
            string attFormat = string.Empty;
            string wcfServiceURL = string.Empty;
            string matchid = string.Empty;

            try
            {
                //var qry = xrmContext.dns_globalsettingSet.Where(gs => gs.dns_name.Contains("Email Audit Notification"))
                //    .Select(gs => new {gs.dns_Value }).Single();
        //wcfServiceURL = qry.dns_Value;
       
                rptName = iReportName.Get(executionContext);
                rptFormat = iReportFormat.Get(executionContext);
                ccEmail = iCCEmail.Get(executionContext);
                bool attachment = iAttachment.Get(executionContext);
                attName = iAttachmentName.Get(executionContext);
                attFormat = iAttachmentFormat.Get(executionContext);
                wcfServiceURL = iWCFServiceURL.Get(executionContext);
                //string paramName = iParameterName.Get(executionContext); ;
                if (iMatch != null)
                {
                    EntityReference MatchRef = iMatch.Get(executionContext);
                    if (MatchRef != null || MatchRef.Id != Guid.Empty)
                    {
                        matchid = MatchRef.Id.ToString();
                        WCFService(wcfServiceURL, rptName, rptFormat, "matchid", matchid, ccEmail, attachment, attName, attFormat);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException("GenerateDAEmail.Execute", ex);
            }
        }

        private void WCFService(string WCFServiceURL, string rptName, string rptFormat, string paramName, string paramValue,
            string ccEmail, bool attachment, string attName, string attFormat)
        {
            //Another way to call webservice
            /*ServiceEmailAudit.ICRMDNSOperations csoClient = null;
            try
            {
                var binding = new BasicHttpBinding();
                var endPoint = new EndpointAddress(WCFServiceURL);
                var channelFactory = new ChannelFactory<ServiceEmailAudit.ICRMDNSOperations>(binding, endPoint);

                binding.Security.Mode = BasicHttpSecurityMode.None;
                binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
                binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
                binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;

                csoClient = channelFactory.CreateChannel();
                csoClient.GenerateDAEmail(rptName, rptFormat, paramName, paramValue, ccEmail, attachment, attName, attFormat);
                ((ICommunicationObject)csoClient).Close();
            }
            catch (Exception ex)
            {
                if (csoClient != null)
                {
                    ((ICommunicationObject)csoClient).Abort();
                }
                throw new Exception(string.Format("Error occured while sending case request to other application: {0}", ex.Message), ex);
            }*/

            try
            {
                //Call the webservice
                BasicHttpBinding bhBinding = new BasicHttpBinding();
                bhBinding.Name = "BasicCRMDNSOperations";
                bhBinding.Security.Mode = BasicHttpSecurityMode.None;
                bhBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
                bhBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
                bhBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
                EndpointAddress eAddress = new EndpointAddress(WCFServiceURL);
               
                ServiceEmailAudit.CRMDNSOperationsClient csoClient = new ServiceEmailAudit.CRMDNSOperationsClient(bhBinding, eAddress);
                csoClient.GenerateDAEmail(rptName, rptFormat, paramName, paramValue, ccEmail, attachment, attName, attFormat);
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException("GenerateDAEmail.WCFService => ", ex);
            }
        }
    }
}


2.Build and Register (CRM.EmailAudit.Activities.dll) Activities Code using Plugin Registration tool


3.Goto CRM web->Settings->Processes,
create new workflow process

add activities step

set properties.

Finally activate the process.

No comments:

Post a Comment