转自(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
}
}
}