Schlagwort-Archive: Marketing

Howto: Retrieve members of a marketing list

Retrieving the members of a marketing list is for one reason a little bit trickier than retrieving other entities: there are no messages for operating with the listmember entity.

Once you have loaded the marketing list, for which you want to retrieve the members, you have to find out of what type the members are. The type of the members is stored in the membertype property of the marketing list.

Following method returns the name of the member entity type

internal string GetMemberType(CrmNumber crmNumber)
{
    switch (crmNumber.Value)
    {
        case ListType.Account:
            return EntityName.account.ToString();
        case ListType.Contact:
            return EntityName.contact.ToString();
        case ListType.Lead:
            return EntityName.lead.ToString();
        default:
            break;
    }
}

With this information we can create the query expression which we use to retrieve the list members.

BusinessEntityCollection RetrieveMembers(list marketinglist)
{
 
 string targetEntity = GetMemberType(marketinglist.membertype);
 
 QueryExpression query = new QueryExpression
 {
   EntityName = targetEntity;
   ColumnSet = new AllColumns();
 };
 
 // link from target entity to listmember
 LinkEntity linkToListMemberEntity = new LinkEntity
 {
   LinkFromEntityName = EntityName.account.ToString();
   LinkFromAttributeName = targetEntity + "id";
   LinkToEntityName = EntityName.listmember.ToString();
   LinkToAttributeName = "entityid";
 };
 
 //link from listmember to list
 LinkEntity linkToMarketingList = new LinkEntity
 {
   LinkFromEntityName = EntityName.listmember.ToString();
   LinkFromAttributeName = "listid";
   LinkToEntityName = EntityName.list.ToString();
   LinkToAttributeName = "listid";
 };
 
 // filter result with the id of the marketing list
 ConditionExpression marketingListIdCondition = new ConditionExpression
 {
   AttributeName = "listid";
   Operator = ConditionOperator.Equal;
   Values = new [] { marketinglist.listid.Value };
 };
 
 linkToMarketingList.LinkCriteria = new FilterExpression();
 linkToMarketingList.LinkCriteria.Conditions.Add(marketingListIdCondition);
 
 linkToListMemberEntity.LinkEntities.Add(linkToMarketingList);
 
 query.LinkEntities.Add(linkToListMemberEntity);
 
 BusinessEntityCollection members = service.RetrieveMultiple(query);
 
 return members;
}