Thursday, August 19, 2010

Dynamic CRM 4.0 Custom Plugin For Views

Dynamics CRM 4.0 provides the users the facility to create views which provides users with customized view of their records. Out of box CRM views could be used to specify some criteria to query records, same like the advanced find. But it has some limitations when linking multiple entities to fetch records and on some criteria. For Example, fetching a record from an entity which has a many to many relationship will not be possible in with some criteria.

To override the limitation with views and criteria, custom plugins could be used which could be used to define the criteria required to satisfy the requirement. Each time the views are selected, a Fetch XML statement will be executed in the background which will return the result set. Even though this approach will return the required record, their will be some performance impact of querying the records and building the Custom Fetch XML statement which needs to be handled by managing the records fetched and paging.

public void Execute(IPluginExecutionContext context)
{
ICrmService vService = context.CreateCrmService(true);

if (context.InputParameters != null && context.InputParameters.Properties.Contains("FetchXml"))
{
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(context.InputParameters.Properties["FetchXml"].ToString());

if ((xDoc.FirstChild.FirstChild.Attributes[0].Value.ToString() == "lead") && (xDoc.GetElementsByTagName("condition")[0].Attributes[0].Value == "Custom Attribute"))
{
context.InputParameters.Properties["FetchXml"] = "you Custom FETCH XML STATEMENT";
}
}

}



The plugin step should be configured on the "Execute" message and the primary and secondary entity has to be defined as "none", and the criteria of which entity it is related to should be handled within the code.

Thanks for reading this document. I hope this could help you in building the custom plugins for views.