Thursday, May 7, 2009

Retrieve Marketing List Members

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.

2 comments:

  1. Hi

    Recently I've been looking for some way to do Retrieve Multiple with JavaScript and I found something like this:

    http://technet.microsoft.com/en-us/library/cc677077.aspx

    My problem is thata ALL that I have found in the web shows how to do RetriveMultiple request but I don't know how to specifie more than one conditions, look:

    "<-q1:Conditions>"+
    "<-q1:Condition>"+
    "<-q1:AttributeName>address1_city<-/q1:AttributeName>"+
    "<-q1:Operator>Like<-/q1:Operator>"+
    "<-q1:Values>"+
    "<-q1:Value xsi:type='xsd:string'>"+searchCity+"<-/q1:Value>"+
    "<-/q1:Values>"+
    "<-/q1:Condition>"+
    "<-/q1:Conditions>"+

    In that example, they say SELECT field FROM contact WHERE address1_city LIKE "%@searchCity%"

    Waht I do need is something more like this:

    SELECT field FROM contact WHERE (address1_city IN("value1","value2")) AND contactid = "xxxx-xxxx-xxxx-xxx"

    Do you think you could help me? any idea of how to build this query using jscript?

    By the way, I used "-" just to can post the comment, it really does not go there.

    Thanks in advanced

    ReplyDelete
  2. The requirement of yours can be done by adding following bold part to your script:

    "<-q1:EntityName>account<-/q1:EntityName>"+
    "<-q1:ColumnSet xsi:type='q1:ColumnSet'>"+
    "<-q1:Attributes>"+
    "<-q1:Attribute>fullname<-/q1:Attribute>"+
    "<-q1:Attribute>contactid<-/q1:Attribute>"+
    "<-/q1:Attributes>"+
    "<-/q1:ColumnSet>"+
    "<-q1:Distinct>false<-/q1:Distinct>"+
    "<-q1:Criteria>"+
    "<-q1:FilterOperator>And<-/q1:FilterOperator>"+
    "<-q1:Conditions>"+
    "<-q1:Condition>"+
    "<-q1:AttributeName>address1_city<-/q1:AttributeName>"+
    "<-q1:Operator>In<-/q1:Operator>"+
    "<-q1:Values>"+
    "<-q1:Value xsi:type='xsd:string'>"+searchCity1+"<-/q1:Value>"+
    "<-q1:Value xsi:type='xsd:string'>"+searchCity2+"<-/q1:Value>"+
    "<-q1:Value xsi:type='xsd:string'>"+searchCity3+"<-/q1:Value>"+
    "<-/q1:Values>"+
    " <-/q1:Condition>" +
    " <-q1:Condition>" +
    "<-q1:AttributeName> contactid <-/q1:AttributeName>" +
    "<-q1:Operator>Equal<-/q1:Operator>" +
    "<-q1:Values>" +
    "<-q1:Value xsi:type=\"xsd:string\">" + contid +
    "<-/q1:Value>" +
    "<-/q1:Values>" +
    "<-/q1:Condition>" +
    "<-/q1:Conditions>"+
    "<-/q1:Criteria>"+
    "<-/query>"+
    "<-/RetrieveMultiple>"+
    "<-/soap:Body>"+
    "<-/soap:Envelope>";

    ReplyDelete