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:
        case ListType.Lead:
            return EntityName.lead.ToString();

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();
 BusinessEntityCollection members = service.RetrieveMultiple(query);
 return members;

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.