CRM插件用于发布和发布所有消息 [英] CRM Plugin for Publish and Publish All messages

查看:94
本文介绍了CRM插件用于发布和发布所有消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我们是否可以编写在Dynamics CRM(任何版本)中针对发布和全部发布之类的消息执行的插件。

解决方案

这是一个适用于Publish和PublishAll的插件消息,它将使用我为此目的创建的实体(您可以更改为执行任何操作)来记录事件。



当事件为发布时,插件使用ParameterXml参数(



您可以从我的网站上下载插件项目和CRM解决方案 GitHub


I was wondering if we can write plugins that get executed for messages like "publish" and "publish all" in Dynamics CRM (any version). if so can you share any sample references for the same or code snippets.

解决方案

This is a plugin that works for Publish and PublishAll messages and it will log the event using an entity that I created for this purpose (you can change to do whatever you want).

When the event is Publish, the plugin uses the ParameterXml parameter (MSDN) to log which components are being published. In the case of the PublishAll message, this parameter is not present so there's no detail (which makes sense because you're publishing all).

public class PublishPlugin : IPlugin
{
    public void Execute(IServiceProvider serviceProvider)
    {
        IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
        IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

        if (context.MessageName != "Publish" && context.MessageName != "PublishAll")
            return;

        string parameterXml = string.Empty;
        if (context.MessageName == "Publish")
        {
            if (context.InputParameters.Contains("ParameterXml"))
            {
                parameterXml = (string)context.InputParameters["ParameterXml"];
            }
        }

        CreatePublishAuditRecord(service, context.MessageName, context.InitiatingUserId, parameterXml);
    }

    private void CreatePublishAuditRecord(IOrganizationService service, string messageName, Guid userId, string parameterXml)
    {
        Entity auditRecord = new Entity("fjo_publishaudit");
        auditRecord["fjo_message"] = messageName;
        auditRecord["fjo_publishbyid"] = new EntityReference("systemuser", userId);
        auditRecord["fjo_publishon"] = DateTime.Now;
        auditRecord["fjo_parameterxml"] = parameterXml;

        service.Create(auditRecord);
    }
}

This is how it looks in CRM:

You can download the plugin project and CRM solution from my GitHub.

这篇关于CRM插件用于发布和发布所有消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆