Monday, May 31, 2010

MS CRM4: Creating View and setting filter criteria programmatically

We wanted to create a view with criteria on the “Due Date” field to show all records that have due date on or before today

Checking the view creation wizard, we can select on or before and we must specify specific date, we can’t specify  it as “Today”


I have came up with a workaround for this, I have created the view and set the criteria to specific date, 01/01/1999 for example, any date. Then I created a plugin to work on execute message, on pre-event, then in the plugin I was parsing the FetchXML of the execution, if the primary entity is same as the view entity, and there is a condition on “Due Date” and the value is “01/01/1999” then to change this value to today’s date in the FetchXML.

main code snippets

//get FetchXML from the context
fetchXml = (string)context.InputParameters.Properties["FetchXml"];

//get the entity name
string entity = xmlDoc.SelectSingleNode("fetch/entity").Attributes["name"].Value.ToString();

if (entity == "incident") // here I am using incident, replace with your entity name

if (xmlDoc.SelectSingleNode("fetch/entity/filter") != null)

// Check if there are conditions exists
if (xmlDoc.SelectSingleNode("fetch/entity/filter/condition") != null)
// Loop through the conditions to look for the followup by condition
nodes = xmlDoc.SelectNodes("fetch/entity/filter/condition");
foreach (XmlNode node in nodes)
if (node.Attributes["attribute"] != null && node.Attributes["operator"] != null && node.Attributes["value"] != null)
string slaOnOrBefore = "01/01/1999";
if (node.Attributes["attribute"].Value.ToString().ToLower() == "followupby".ToLower() && node.Attributes["operator"].Value.ToString().ToLower() == "on-or-before".ToLower() && node.Attributes["value"].Value.ToString().ToLower() == slaOnOrBefore.ToLower())
"value"].Value = DateTime.Today.AddDays(-1).ToString("yyyy-MM-ddT00:00:00");

//Update the fetch xml
fetchXml = xmlDoc.InnerXml.ToString();
"FetchXml"] = fetchXml;

No comments:

Post a Comment