A couple of days ago a Dynamics Community Member posted a question titled:
“Automatically add filter on dynamic marketing lists”
To give you a little background, users have Business Unit Level read access on Accounts and Contacts. They create Dynamic Marketing Lists and these lists are being synced with a 3rd party marketing tool. This tool has access across all business units. So when it fetches members, query returns data from all BUs.
However, requirement is to return data from current business unit. To avoid this, currently users add a filter (Owning Business Unit equals Current Business Unit) in the list query. Because of manual intervention, sometimes users miss this filter and in result emails are distributed to cross BU customers as well.
You might argue that it’s a security configuration problem. Although, I am tempted to play with Dynamic Market List queries and see how far we can go!
Problem
Add pre-filters on Dynamic Marketing List. Whenever user creates a Dynamic Marketing List, automatically add following condition:
Owning Business Unit equals current business unit
Solution
Okay, so the Dynamics Marketing list gets members using FetchXML query which is stored in a column named “query”. What if, we can add FetchXML with Business Unit filter using a plugin?
Let’s try!
Here is the plan:
- Create a plugin that builds initial Fetch XML query based on List Target (Account, Contact or Lead).
- Update the “query” column with initial FetchXML.
- Register plugin on pre-create of Marketing List (logical name: list)
Let’s start:
- Here is my plugin code:
public class AddBUFilter : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the tracing service
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
Entity list = (Entity) context.InputParameters["Target"];
// If Target Entity is Marketing List
if (list.LogicalName.Equals("list"))
{
// Get List Type (Static = false, Dynamic = true)
var listType = (Boolean) list.Attributes["type"];
tracingService.Trace("List Type: {0}", listType);
// When List is Dynamic
if (listType == true)
{
// Get Target Entity (Account=1, Contact=2, Lead=4)
var targetedAt = (OptionSetValue)list.Attributes["createdfromcode"];
tracingService.Trace("Targeted At: {0}", targetedAt.Value);
string entityName = string.Empty, idColumnName = string.Empty;
// set entity and column names based on target
switch (targetedAt.Value)
{
case 1:
entityName = "account";
idColumnName = "accountid";
break;
case 2:
entityName = "contact";
idColumnName = "contactid";
break;
case 4:
entityName = "lead";
idColumnName = "leadid";
break;
default:
break;
}
if (!entityName.Equals(string.Empty))
{
// Initial Fetch XML
string query = string.Format(@"
<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='{0}'>
<attribute name='{1}' />
<link-entity name='businessunit' from='businessunitid' to='owningbusinessunit' alias='ab'>
<filter type='and'><condition attribute='businessunitid' operator='eq-businessid' />
</filter>
</link-entity>
</entity>
</fetch>", entityName, idColumnName);
// Set Fetch XML to query column
list.Attributes["query"] = query;
tracingService.Trace("BU Query Added");
}
}
}
}
}
}
- Register assembly and add step with following configuration
- Message: create
- Primary Entity: list
- Event Pipeline Stage of Execution: PreOperation
- Execution Mode: Synchronous

Let’s test the functionality!
Create a Dynamic Marketing List for Account:

Click on Manage Members button to see the filter is added (automatically):

Here are results of this query when executed by a user having access to all Business Units:

To improve this solution , we can use a FetchXML from a view (so we do not need to hardcode entity name and columns). For example, create a view with basic filters, retrieve FetchXML, and assign it to Dynamic Marketing List query.
However, purpose of this post is to explore the capabilities, and to summarize, yes we can add pre-filters in Marketing Dynamics List.

Like
Report
*This post is locked for comments