Get the total members count of a Dynamic Marketing List in CRM 2011.


For a Static Marketing List, we can get the total member count from the attribute “Members Count”.

However this attribute doesn’t’ work for Dynamic Marketing List as this value can only be derived at run time, when the query associated with the list is executed.

One way we can get the total count for the list is by following the below steps.

  • We can get the fetch xml query associated with the Dynamic Marketing List.
  • Remove the attribute and order tag from it.
  • Modify it  to include aggregate function.

Sample Code in C#

 private void btnGetMembersDynamicML_Click(object sender, EventArgs e)
 IOrganizationService orgService = GetOrganizationService();

ColumnSet cols= new ColumnSet(new string[] { "query" });

// GUID of the Dynamic Marketing List
 var entity = orgService.Retrieve("list", new Guid("03C4E473-F7DA-E111-B988-00155D886334"), cols);
 var dynamicQuery = entity.Attributes["query"].ToString();

var countQuery = ModifyFetchXML(dynamicQuery);

var memberCountResult = orgService.RetrieveMultiple(new FetchExpression(countQuery));

DataCollection<Entity> dataCollection = memberCountResult.Entities;

int totalMembers = default(int);
 if(dataCollection != null && dataCollection.Count > 0)
 foreach(Entity entityVal in dataCollection)
 AliasedValue value = (entityVal.Attributes["member_count"] as AliasedValue);
 totalMembers = (int)value.Value;

MessageBox.Show("Total Members : " + totalMembers.ToString());


private static string ModifyFetchXML(string dynamicQuery)
 var doc = new XmlDocument();

var entitynode = doc.GetElementsByTagName("entity")[0];

int childCount = entitynode.ChildNodes.Count;

// Remove all the attribute and order tag
 for (int i = 0; i <= childCount; i++)
 var attributenode = entitynode.SelectSingleNode("//attribute");
 if (attributenode != null) entitynode.RemoveChild(attributenode);
 var ordernode = entitynode.SelectSingleNode("//order");
 if (ordernode != null) entitynode.RemoveChild(ordernode);

// add a new attribute tag
 // <attribute name="fullname" alias="member_count" aggregate="count" />
 XmlElement xmlNodeCustomSettings = doc.CreateElement("attribute");

xmlNodeCustomSettings.SetAttribute("name", "contactid");
 xmlNodeCustomSettings.SetAttribute("alias", "member_count");
 xmlNodeCustomSettings.SetAttribute("aggregate", "count");


// Add aggregate= true attribute
 // <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false" aggregate="true">
 var root = doc.DocumentElement;
 if (root != null) root.SetAttribute("aggregate", "true");

return doc.InnerXml;

public IOrganizationService GetOrganizationService()
 Uri organizationUri = new Uri("http://servername/orgname/XRMServices/2011/Organization.svc");
 Uri homeRealmUri = null;
 ClientCredentials credentials = new ClientCredentials();
 credentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
 OrganizationServiceProxy orgProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);
 IOrganizationService _service = (IOrganizationService)orgProxy;
 return _service;

Hope it helps.


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

One thought on “Get the total members count of a Dynamic Marketing List in CRM 2011.”

Please share your thoughts

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.