Steps to Create PlugIn in CRM Dynamics 365

To create a PostOperation Plug-In for Contact (refer to SDK): after a contact generated, the task to say hello also generated:

0: Turn on the Plug-in Tracing Log
在这里插入图片描述
1: Create a ClassLibrary with .Net FrameWork4.6.2
在这里插入图片描述

2: Add two NuGet Packages:
在这里插入图片描述
3: sign the Class: Project Properties – Signing
在这里插入图片描述
4: Replace the .cs with following Codes

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;

namespace MyCRM1 
{
    public class HelloWorld : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            // Extract the tracing service for use in debugging sandboxed plug-ins.   
            // If you are not registering the plug-in in the sandbox, then you do   
            // not have to add any tracing service related code.   
            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            // Obtain the execution context from the service provider.   
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            // Obtain the organization service reference which you will need for   
            // web service calls.   
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            // The InputParameters collection contains all the data passed in the message request.   
            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                // Obtain the target entity from the input parameters.   
                Entity entity = (Entity)context.InputParameters["Target"];

                // start to try the bussiness logic of Plugin
                try
                {
                    Entity followup = new Entity("task");

                    followup["subject"] = "Send e-mail to the new Contact.";
                    followup["description"] ="Follow up with the contact to say hello.";
                    followup["scheduledstart"] = DateTime.Now.AddDays(7);
                    followup["scheduledend"] = DateTime.Now.AddDays(7);
                    followup["category"] = context.PrimaryEntityName;

                    // Refer to the contact in the task activity.
                    if (context.OutputParameters.Contains("id"))
                    {
                        Guid regardingobjectid = new Guid(context.OutputParameters["id"].ToString());
                        string regardingobjectidType = "contact";

                        followup["regardingobjectid"] =
                        new EntityReference(regardingobjectidType, regardingobjectid);
                    }

                    // Create the task in Microsoft Dynamics CRM.
                    tracingService.Trace("MyCRM: Creating the task activity.");
                    service.Create(followup);
                    tracingService.Trace("MyCRM: SayHello."); // just for fun
                }

                catch (FaultException<OrganizationServiceFault> ex)
                {
                     throw new InvalidPluginExecutionException("An error occurred in MyPlug-in.", ex);
                }
                
                catch (Exception ex)
                {
                       tracingService.Trace("MyPlugin: {0}", ex.ToString());
                       throw;
                }
                // end of try the bussiness logic of Plugin
            }
        }
    }
}

5: Save the code; Ctrl + B to generate the .dll (Under Build)
Find the MyCRM1.dll file under “bin/debug/”

6: Register Assembly .dll and step
a: Find “PlugIn Registration Tool” and run it
b: Create new connection: 在这里插入图片描述
c: Under “Register – Register New Assembly”
在这里插入图片描述
d: Register new Step
在这里插入图片描述

7: now can check the plug-in in D365
a: create a contact to see if say hello task generated
b: check the Plug-in Trace Log 在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值