A Marketing list in CRM, can be a collection accounts or contacts or leads. Retrieving the members of a marketing list is not as as simple. We just realized that the list member entity only stores the link to the entity and Retrieve method is not supported by this entity.
Hence to retrieve the list members for a given entity we need to identify the type of entity being supported by the list i.e Leads/Accounts/Contacts and then using the link entity functionality link it with the listmember entity to get the list.
Following is the snippet of getting collection of accounts from a particular marketing list id.
//initialize QueryExpression for adding link entities
QueryExpression qe = new QueryExpression();
qe.EntityName = EntityName.account.ToString();
//Initialize columnset
ColumnSet col = new ColumnSet();
//add columns to columnset for the acc to retrieve each acc from the acc list
col.AddColumns(new string[] { "accountid", "name", "address1_line1", "address1_line2", "address1_city", "address1_stateorprovince", "address1_postalcode"});
qe.ColumnSet = col;
// link from account to listmember
LinkEntity le = new LinkEntity();
le.LinkFromEntityName = EntityName.account.ToString();
le.LinkFromAttributeName = "accountid";
le.LinkToEntityName = EntityName.listmember.ToString();
le.LinkToAttributeName = "entityid";
//link from listmember to list
LinkEntity le2 = new LinkEntity();
le2.LinkFromEntityName = EntityName.listmember.ToString();
le2.LinkFromAttributeName = "listid";
le2.LinkToEntityName = EntityName.list.ToString();
le2.LinkToAttributeName = "listid";
// add condition for listid
ConditionExpression ce = new ConditionExpression();
ce.AttributeName = "listid";
ce.Operator = ConditionOperator.Equal;
ce.Values = new object[] { strList };//here “strList” is the marketing list id provided to this function.
//add condition to linkentity
le2.LinkCriteria = new FilterExpression();
le2.LinkCriteria.Conditions.Add(ce);
//add linkentity2 to linkentity
le.LinkEntities.Add(le2);
//add linkentities to Query Expression which is passed for getting collection of accounts
qe.LinkEntities.Add(le);
//above query expression is passed to retrieve multiple for getting BusinessEntityCollection of accounts.
BusinessEntityCollection bec = service.RetrieveMultiple(qe);
Similarly you could link with Contacts/Leads to get the list of members from a Contact/Lead Marketing list.
Hence to retrieve the list members for a given entity we need to identify the type of entity being supported by the list i.e Leads/Accounts/Contacts and then using the link entity functionality link it with the listmember entity to get the list.
Following is the snippet of getting collection of accounts from a particular marketing list id.
//initialize QueryExpression for adding link entities
QueryExpression qe = new QueryExpression();
qe.EntityName = EntityName.account.ToString();
//Initialize columnset
ColumnSet col = new ColumnSet();
//add columns to columnset for the acc to retrieve each acc from the acc list
col.AddColumns(new string[] { "accountid", "name", "address1_line1", "address1_line2", "address1_city", "address1_stateorprovince", "address1_postalcode"});
qe.ColumnSet = col;
// link from account to listmember
LinkEntity le = new LinkEntity();
le.LinkFromEntityName = EntityName.account.ToString();
le.LinkFromAttributeName = "accountid";
le.LinkToEntityName = EntityName.listmember.ToString();
le.LinkToAttributeName = "entityid";
//link from listmember to list
LinkEntity le2 = new LinkEntity();
le2.LinkFromEntityName = EntityName.listmember.ToString();
le2.LinkFromAttributeName = "listid";
le2.LinkToEntityName = EntityName.list.ToString();
le2.LinkToAttributeName = "listid";
// add condition for listid
ConditionExpression ce = new ConditionExpression();
ce.AttributeName = "listid";
ce.Operator = ConditionOperator.Equal;
ce.Values = new object[] { strList };//here “strList” is the marketing list id provided to this function.
//add condition to linkentity
le2.LinkCriteria = new FilterExpression();
le2.LinkCriteria.Conditions.Add(ce);
//add linkentity2 to linkentity
le.LinkEntities.Add(le2);
//add linkentities to Query Expression which is passed for getting collection of accounts
qe.LinkEntities.Add(le);
//above query expression is passed to retrieve multiple for getting BusinessEntityCollection of accounts.
BusinessEntityCollection bec = service.RetrieveMultiple(qe);
Similarly you could link with Contacts/Leads to get the list of members from a Contact/Lead Marketing list.