0

C# Get contact membership of groups in Dynamics CRM 2016

Share this content:
LinkedIn
Facebook
RSS
For more info on Managing Identities in Dynamics CRM, I will be giving a presentation on the MIM Usergroup on April 11th.
To join the usergroup and attend the meeting please visit the above link.

 

Groups in Dynamics CRM are known as marketing lists (ML). There are two kinds of ML, there is the static and there is the dynamic. When you go to CRM, there is an field known as a List type which you have to select, it either dynamic or static

So lets say I want to do a query to get which static groups a contact is a member of, one would think check if List type = “static” or “dynamic”? But that is not the case, List type is a Boolean field so you check if it is false then it is static, if it is true then it is dynamic. In the sample code below look at

if ((bool)list.Entities[0].Attributes[“type”] == false)

Code – C#

public static EntityCollection GetContactList()
{
    EntityCollection contacts = null;
    try
    {
        // The using statement assures that the service proxy is properly disposed.
        QueryExpression query = new QueryExpression
        {
            EntityName = Contact.EntityLogicalName,
            ColumnSet = new ColumnSet(true),
        };
        // Get the market list.
        contacts = _service.RetrieveMultiple(query);
        for (int i = 0; i < contacts.Entities.Count; i++)
        {
            Contact contact = contacts[i].ToEntity();
            QueryExpression queryListMember = new QueryExpression
            {
                EntityName = ListMember.EntityLogicalName,
                ColumnSet = new ColumnSet(true),
                Criteria = { Conditions = { new ConditionExpression("entityid", ConditionOperator.Equal, contact.ContactId), new ConditionExpression("entitytype", ConditionOperator.Equal, "contact") } }
            };
            var memberList = _service.RetrieveMultiple(queryListMember);
            if (memberList.Entities.Count>0)
            {
                EntityReference id = (EntityReference)memberList.Entities[0].ToEntity().ListId;
                QueryExpression queryList = new QueryExpression
                {
                    EntityName = List.EntityLogicalName,
                    ColumnSet = new ColumnSet(true),
                    Criteria = { Conditions = { new ConditionExpression("listid", ConditionOperator.Equal, id.Id) } }
                };
                var list = _service.RetrieveMultiple(queryList);
                if ((bool)list.Entities[0].Attributes["type"] == false)
                {
                    Console.WriteLine("Contact Id = {0}, Name = {1}", contact.Id, contact.FirstName + " " + contact.LastName);
                    Console.WriteLine("Marketing List = " + list.Entities[0].ToEntity().ListName);
                }
            }
        }
    }
    // Catch any service fault exceptions that Microsoft Dynamics CRM throws.
    catch (FaultException ex)
    {
        // You can handle an exception here or pass it back to the calling method.
        Console.WriteLine(ex.Message);
    }
    return contacts;
}




For more info on Managing Identities in Dynamics CRM, I will be giving a presentation on the MIM Usergroup on April 11th.
To join the usergroup and attend the meeting please visit the above link.


			

Ike Ugochuku

Leave a Reply

Your email address will not be published. Required fields are marked *