Banner

Marketing List Management - Microsoft Dynamics CRM

Back to Blog Listing


Return a list of all users of a dynamic marketing list:

To return a list of all contacts that are associated with a specific marketing list, use the following code where the "ListID" is the GUID of the list within CRM that you wish to return the contacts from. 

Dim cols As Query.ColumnSet = New Query.ColumnSet(New String() {"query"})
Dim entity = ser.Retrieve("list", New Guid(ListID), cols)
Dim dynamicQuery = entity.Attributes("query").ToString()
Dim existingMembers As EntityCollection = ser.RetrieveMultiple(New Query.FetchExpression(dynamicQuery))

If wanting to have more details on the contact returned from this query be sure to edit the view named "Contact Associated View" against the "contact" entity to have further columns.

 

Remove a user from a marketing list:

To remove a user to a marketing list you cannot use the ListMember entity, there is no "Delete" ability on these entities. Instead you must ensure you have imported Microsoft.CRM.sdk.Messages and then use the RemoveMemberListRequest object.

 To use the RemoveMemberListRequest object, instantiate it (it requires no parameters) and then set the entityID (can be contact, account or lead GUID) and listID. Once this has been set up, use your IOrganisationService instance to "Execute" the request.

 e.g.

Dim existingLists As EntityCollection = ser.RetrieveMultiple(q)
 
For Each li As ListMember In existingLists.Entities
 
      Dim req As New RemoveMemberListRequest()
 
      req.EntityId = con.ContactId.Value
      req.ListId = li.ListId.Id
 
      ser.Execute(req)
 
Next

 

Add a user to a marketing list:

To add a user to a marketing list you cannot use the ListMember entity, there is no "Create" ability on these entities. Instead you must ensure you have imported Microsoft.CRM.sdk.Messages and then use the AddMemberListRequest object.

 To use the AddMemberListRequest object, instantiate it (it requires no parameters) and then set the entityID (can be contact, account or lead GUID) and listID. Once this has been set up, use your IOrganisationService instance to "Execute" the request.

 e.g.

For Each subscription As String In newSubsriptions
 
      If subscription <> String.Empty Then
 
         Dim req As New AddMemberListRequest()
 
         req.EntityId = con.ContactId.Value
         req.ListId = New Guid(subscription)
 
          ser.Execute(req)
 
      End If
 
Next

 

Return a list of marketing lists that a user is a member of:

As marketing lists and contacts have a many to many relationship (i.e. a contact can have many lists and likewise a list can have many contacts) you cannot search them in the regular way using a QueryExpression. Instead there are 2 options:

 1) Use fetch xml: go to advanced find and use this to build your query and then click the "Download Fetch XML" button and use that in the code, similar to the example below:

Dim f As New Query.FetchExpression("<?xml version=""1.0""?><fetch distinct=""true"" mapping=""logical"" output-format=""xml-platform"" version=""1.0""><entity name=""list""><attribute name=""listname""/><attribute name=""type""/><attribute name=""createdfromcode""/><attribute name=""lastusedon""/><attribute name=""purpose""/><attribute name=""listid""/><order descending=""true"" attribute=""listname""/>-<link-entity name=""listmember"" intersect=""true"" visible=""false"" to=""listid"" from=""listid""><link-entity name=""contact"" to=""entityid"" from=""contactid"" alias=""ad""><filter type=""and""><condition attribute=""contactid"" value=""" & con.ContactId.Value.ToString & """ operator=""eq""/></filter></link-entity></link-entity></entity></fetch>")
 
Dim existingLists As EntityCollection = ser.RetrieveMultiple(f)

 

2) use QueryByAttribute instead: this, although it has less code, is slightly more complicated as you have to have an understanding of all the entities and how they link to one another. To use this please see the example below:

Dim q As New Query.QueryByAttribute("listmember")
 
q.AddAttributeValue("entityid", con.ContactId.Value)
 
q.ColumnSet = New ColumnSet(True)
 
Dim existingLists As EntityCollection = ser.RetrieveMultiple(q)

 

The first query will return a list of MarketingLists(Entity Name = "list") and the second will return a list of ListMembers.

 


About the Author

James CTwo is our blogging alter ego for the Dynamics CRM and Web experts here at C2. We have a passion for anything CRM, Web Design or Social Media. We love writing about it! Find us on YouTube & Twitter.

comments powered by Disqus

Our White Papers

Discover the content that matters to you on business transformation. Explore how you can drive customer engagement, empower your team, optimise your businesses and revolutionise your products with the next digital revolution. For more information visit our white paper library.

Library

C2 Newsletter

Our newsletter is sent on a quarterly basis, offering detailed insights into all aspects of business transformation with Microsoft cloud technologies. We also promote upcoming events and special offers throughout the year. Sign up to get involved today.

Dynamics 365 Office 365 Power BI Silver Partner