在项目中,我们可能要处理一些数据,不让其通过高级查找或者视图列表中展示出来,这时候我们可以通过注册RetrieveMultiple消息的插件来处理。
我们可以通过注册RetrieveMultiple的Post Operation插件,然后处理它的输出结果BusinessEntityCollection来改变查询结果。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
namespace Carl.OverrideRMUCI
{
public class GetRetailSales : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Here we will simulate connecting to a 3rd party environment
#region simulate3rdparty
// Create a list of Retail Sales that would exist in a database in another system
var retailSales = new List<RetailSale>();
var retailSales1 = new RetailSale
{
Product = "Product1",
Amount = 1
};
var retailSales2 = new RetailSale
{
Product = "Product2",
Amount = 5
};
var retailSales3 = new RetailSale
{
Product = "Product3",
Amount = 10
};
retailSales.Add(retailSales1);
retailSales.Add(retailSales2);
retailSales.Add(retailSales3);
#endregion
// Plugin Code
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
EntityCollection entityCollection = new EntityCollection();
entityCollection.EntityName = "crc07_retailsale";
foreach (var retailSale in retailSales)
{
Guid guid = new Guid();
guid = Guid.NewGuid();
Entity crmRetailSale = new Entity("crc07_retailsale");
crmRetailSale.Attributes.Add("crc07_retailsaleid", guid);
crmRetailSale.Attributes.Add("crc07_product", retailSale.Product);
crmRetailSale.Attributes.Add("crc07_amount", retailSale.Amount);
entityCollection.Entities.Add(crmRetailSale);
}
context.OutputParameters["BusinessEntityCollection"] = entityCollection;
}
}
public class RetailSale
{
public string Product { get; set; }
public Int32 Amount { get; set; }
}
}
可以通过改写BusinessEntityCollection来达到改变查询结果的目的。
注意只能注册post插件,pre插件是没有BusinessEntityCollection输出的。