zoukankan      html  css  js  c++  java
  • CRM 4.0 Creating Interactive Plugins

    转自(http://mscrm4ever.blogspot.com/2009/01/crm-40-creating-interactive-plug-ins.html)

    The Plug-ins model is a truly robust mechanism which makes it very easy to enforce business rules and control the way your organization data flows.

    However, this model does have a few limitations. One in specific which I find very annoying is the fact you can’t return interactive html to the user and must be satisfied with static textual information. I truly believe ms should consider changing this behavior in their next version.

    In order to overcome this limitation I made a small yet unsupported modification to the error dialog page that pops up when you throw an
    Invalid plug-in exception. The dialog resides in inside /CRMWeb/_common/error/dlg_error.aspx

    In order to make the dialog support HTML messages I added the following function that transforms the ErrorMessage Text to HTML and also added an onload event in the html body tag.


    function unsupported()
    {
    var ErrorMessage = document.getElementById("ErrorMessage");
    ErrorMessage.innerHTML = ErrorMessage.innerText;
    }



    <body onload="unsupported()">


    To test this “theory” I created a simple duplicate detection check on the contact entity first and last name.
    If a duplicate is found, I construct an HTML error message with a link to the duplicate record and
    pass it as an InvalidPluginExecutionException parameter.

    To test this out simply register the plug-in on the contact pre create / update event.



    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Crm.Sdk;
    using Microsoft.Crm.SdkTypeProxy;
    using Microsoft.Crm.Sdk.Query;

    namespace TestPlugin
    {
    public class DuplicateHandler : IPlugin
    {
    #region IPlugin Members

    public void Execute(IPluginExecutionContext context)
    {
    if (context.InputParameters.Properties.Contains(ParameterName.Target))
    {
    #region Check Duplicate Contact [FirstName LastName]

    DynamicEntity contact = context.InputParameters.Properties[ParameterName.Target] as DynamicEntity;
    String firstName = String.Empty;
    String lastName = String.Empty;

    if (contact.Properties.Contains("firstname"))
    {
    firstName = contact.Properties["firstname"].ToString();
    }

    if (contact.Properties.Contains("lastname"))
    {
    lastName = contact.Properties["lastname"].ToString();
    }


    QueryExpression query = new QueryExpression(context.PrimaryEntityName);
    query.ColumnSet.AddColumn(context.PrimaryEntityName + "id");

    ConditionExpression firstCondition = new ConditionExpression();
    firstCondition.AttributeName = "firstname";
    firstCondition.Operator = ConditionOperator.Equal;
    firstCondition.Values = new object[] { firstName };

    ConditionExpression lastCondition = new ConditionExpression();
    lastCondition.AttributeName = "lastname";
    lastCondition.Operator = ConditionOperator.Equal;
    lastCondition.Values = new object[] { lastName };

    FilterExpression whereFilter = new FilterExpression();
    whereFilter.Conditions.Add(firstCondition);
    whereFilter.Conditions.Add(lastCondition);
    whereFilter.FilterOperator = LogicalOperator.And;

    query.Criteria = whereFilter;

    ICrmService Service = context.CreateCrmService(true);
    BusinessEntityCollection resultCollection =
    (BusinessEntityCollection)Service.RetrieveMultiple(query);

    #endregion

    #region Retrieve Duplicate HTML

    if (resultCollection.BusinessEntities.Count > 0)
    {
    contact dupContact = resultCollection.BusinessEntities[0] as contact;

    String linkStyle = "text-decoration:underline;color:blue;";

    String contactUrl = String.Format(
    "{3} {4}" ,
    linkStyle,
    context.OrganizationName,
    dupContact.contactid.Value.ToString(),
    firstName,
    lastName
    );

    String errorMessage = String.Format("Duplicate Contact: {0}",contactUrl);

    throw new InvalidPluginExecutionException(errorMessage);

    }

    #endregion
    }

    #endregion
    }
    }
    }
  • 相关阅读:
    六:观察者模式
    聊一聊如何接入支付宝
    每天学点SpringCloud(八):使用Apollo做配置中心
    每天学点SpringCloud(七):路由器和过滤器-Zuul
    每天学点SpringCloud(六):Hystrix使用
    每天学点SpringCloud(五):如何使用高可用的Eureka
    每天学点SpringCloud(四):Feign的使用及自定义配置
    每天学点SpringCloud(三):自定义Eureka集群负载均衡策略
    每天学点SpringCloud(二):服务注册与发现Eureka
    每天学点SpringCloud(一):使用SpringBoot2.0.3整合SpringCloud
  • 原文地址:https://www.cnblogs.com/janmson/p/1602155.html
Copyright © 2011-2022 走看看