CRM插件调用字段更新调用WCF

时间:2013-12-20 12:16:07

标签: c# wcf dynamics-crm-2011

我希望在更新特定字段时触发插件。

这是我正在调用的代码

public class CaseStageUpdate : IPlugin
{
    public void Execute(IServiceProvider serviceProvider)
    {
        try
        {
            // Extract the tracing service for use in debugging sandboxed plug-ins.
            ITracingService tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            if (serviceProvider == null)
            {
                throw new ArgumentNullException("serviceProvider");
            }

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

            Entity entity;
            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                string strParams = "None";
                entity = (Entity)context.InputParameters["Target"];
                if (entity.LogicalName == "entityname")
                {
                    // Create Matter Process
                    try
                    {

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

                        try
                        {

                            if (entity.Attributes.ContainsKey("field")){// call web service}

                        }
                        catch (Exception ex)
                        {
                            tracingService.Trace("Plugin Exception : \n {0}", ex.ToString());
                            throw;
                        }

                    }
                    catch (Exception ex)
                    {
                        tracingService.Trace("Plugin Exception : \n {0}", ex.ToString());
                        throw;
                    }
                }
            }
            else
            {
                throw new InvalidPluginExecutionException("Plugin is not valid for this enity.");
            }
        }
        catch (Exception ex)
        {

        }
    }
}

在WCF中我正在更新实体,但插件再次调用,因为插件中再次找到更新的字段。但我没有更新该领域。

假设我想在AAA字段更新时更新ABC字段。我触发设置何时找到AAA字段我将更新ABC字段。

WCF代码

Entity entity = service.Retrieve("entityname", Guid.Parse(Id), new ColumnSet(true));
entity.Attributes["ABC"] = "TEST";
service.Update(entity);  

1 个答案:

答案 0 :(得分:1)

should be using the Target更新现有实体的现有字段,而不是检索实体以更新字段。

// 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.
    var entity = (Entity)context.InputParameters["Target"];
    entity.Attributes["ABC"] = "TEST";
    // No Need to call Update on the target.  It'll get updated as a part of the Plugin Process
}

此外,删除所有试用版以外的所有试用版:

public void Execute(IServiceProvider serviceProvider)
{
    try
    {
        // Extract the tracing service for use in debugging sandboxed plug-ins.
        ITracingService tracingService =
            (ITracingService)serviceProvider.GetService(typeof(ITracingService));

        if (serviceProvider == null)
        {
            throw new ArgumentNullException("serviceProvider");
        }

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

        Entity entity;
        if (!context.InputParameters.Contains("Target") || !(context.InputParameters["Target"] is Entity))
        {
            throw new InvalidPluginExecutionException("Plugin is not valid for this enity.");
        }

        entity = (Entity)context.InputParameters["Target"];
        if (entity.LogicalName == "entityname")
        {
            // Create Matter Process
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            if (entity.Attributes.ContainsKey("field")){// call web service}
        }
    }
    catch (Exception ex)
    {
        tracingService.Trace("Plugin Exception : \n {0}", ex.ToString());
        throw;
    }
}