Sample: Work with marketing lists

 

Applies To: Dynamics Marketing

This sample code is for Microsoft Dynamics Marketing. The sample code can be found in the following location after you download and install the Microsoft Dynamics Marketing SDK:

Samples\CS\MarketingListSample

Requirements

  1. Download and install the Microsoft Dynamics Marketing SDK package. The sample code is contained in the SDK.msi part of the download package.

  2. Enable the SDK gateway and assign permissions to queues. More information: Authorize queues

  3. Follow the instructions provided in the Quick start with sample code to install, build, and run the sample.

Note

This sample works with your live production data. It may create contact objects in your Microsoft Dynamics Marketing and Microsoft Dynamics CRM environments. In addition, it will send emails to the designated recipients.

Tip

  • You should allocate some email addresses that actually exist as a test audience, and then write scenario tests that run against the test audience. Create a marketing list specifically for testing mails and create test contacts with email addresses you control as an audience for testing your email marketing messages and SDK integration code.

  • Use email marketing message testing functions to determine if the email messages are formatted correctly before sending the email messages.

Demonstrates

This sample demonstrates how to work with marketing lists via the SDK as part of the list segmentation functionality in Microsoft Dynamics Marketing.

Example

namespace Microsoft.Dynamics.Marketing.SDK.Samples.MarketingListSample
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using Microsoft.Dynamics.Marketing.SDK.Common;
    using Microsoft.Dynamics.Marketing.SDK.Messages;
    using Microsoft.Dynamics.Marketing.SDK.Messages.MarketingList;
    using Microsoft.Dynamics.Marketing.SDK.Model;
    using Microsoft.Dynamics.Marketing.SDK.Samples.SdkClient;
    using Microsoft.Dynamics.Marketing.SDK.Samples.SdkSample;

    /// <summary>
    /// Demonstrates how to manipulate marketing lists via the SDK.
    /// </summary>
    /// <remarks>
    /// Depending on which part of the sample you run, some prior set up is required.
    /// For all of the options in this sample you must have the SDK configured with your Azure Service Bus Queues on the Integration Options page in Microsoft Dynamics Marketing. You must also have permissions enabled for lists.
    /// </remarks>
    public class MarketingListSample : SdkSample
    {
        /// <summary>
        /// Starting point for sample.
        /// </summary>
        /// <param name="args">Arguments needed for authentication.</param>
        public static void Main(string[] args)
        {
            try
            {
                // This creates the QueueClientProvider that connects to the queues, tests them, and then provides them to the client when needed.
                var queueClientProvider = CreateQueueClientProviderFromArgs(args);

                // This runs the sample that shows a menu of possible requests.
                var sample = new MarketingListSample(queueClientProvider);
                sample.Run();
            }
            catch (Exception ex)
            {
                MarketingListSample.WriteException(ex);
                Console.WriteLine("Press enter to exit.");
                Console.ReadLine();
                return;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MarketingListSample"/> class.
        /// </summary>
        /// <param name="queueClientProvider">The QueueClientProvider that is set up to connect to the SDK queues configured in Microsoft Dynamics Marketing.</param>
        public MarketingListSample(IQueueClientProvider queueClientProvider)
        {
            var responseHandlerMapping = new Dictionary<Type, Client.ResponseHandler>
            {
                { typeof(AddContactToMarketingListByEmailResponse), this.ProcessAddContactToMarketingListByEmailResponse },
                { typeof(AddContactsToMarketingListByEmailsResponse), this.ProcessAddContactsToMarketingListByEmailsResponse },
                { typeof(AddContactToMarketingListByIdResponse), this.ProcessAddContactToMarketingListByIdResponse },
                { typeof(AddContactsToMarketingListByIdsResponse), this.ProcessAddContactsToMarketingListByIdsResponse },
                { typeof(CopyContactsFromMarketingListResponse), this.ProcessCopyContactsFromMarketingListResponse },
                { typeof(CopyQueryToMarketingListResponse), this.ProcessCopyQueryToMarketingListResponse },
                { typeof(CreateMarketingListResponse), this.ProcessCreateMarketingListResponse },
                { typeof(DeleteMarketingListResponse), this.ProcessDeleteMarketingListResponse },
                { typeof(RemoveAllContactsFromMarketingListResponse), this.ProcessRemoveAllContactsFromMarketingListResponse },
                { typeof(RemoveContactFromMarketingListByEmailResponse), this.ProcessRemoveContactFromMarketingListByEmailResponse },
                { typeof(RemoveContactFromMarketingListByIdResponse), this.ProcessRemoveContactFromMarketingListByIdResponse },
                { typeof(RetrieveAllListsResponse), this.ProcessRetrieveAllListsResponse },
                { typeof(RetrieveMarketingListResponse), this.ProcessRetrieveMarketingListResponse },
                { typeof(RetrieveMarketingListsResponse), this.ProcessRetrieveMarketingListsResponse },
                { typeof(UpdateMarketingListResponse), this.ProcessUpdateMarketingListResponse },
                { typeof(SdkErrorResponse), this.ProcessSdkErrorResponse }
            };

            this.SetupClient(queueClientProvider, responseHandlerMapping);

            this.SampleMenuActions = new Dictionary<string, Tuple<string, Action>>
            {
                { "1", new Tuple<string, Action>("Add contact to marketing list by email", this.AddContactToMarketingListByEmail) },
                { "2", new Tuple<string, Action>("Add contacts to marketing list by emails", this.AddContactsToMarketingListByEmails) },
                { "3", new Tuple<string, Action>("Add contact to marketing list by ID", this.AddContactToMarketingListById) },
                { "4", new Tuple<string, Action>("Add contacts to marketing list by IDs", this.AddContactsToMarketingListByIds) },
                { "5", new Tuple<string, Action>("Copy contacts from marketing list", this.CopyContactsFromMarketingList) },
                { "6", new Tuple<string, Action>("Copy query to marketing list", this.CopyQueryToMarketingList) },
                { "7", new Tuple<string, Action>("Create marketing list", this.CreateMarketingList) },
                { "8", new Tuple<string, Action>("Delete marketing list", this.DeleteMarketingList) },
                { "9", new Tuple<string, Action>("Remove all contacts from marketing list", this.RemoveAllContactsFromMarketingList) },
                { "10", new Tuple<string, Action>("Remove contact from marketing list by email", this.RemoveContactFromMarketingListByEmail) },
                { "11", new Tuple<string, Action>("Remove contact from marketing list by ID", this.RemoveContactFromMarketingListById) },
                { "12", new Tuple<string, Action>("Retrieve all lists", this.RetrieveAllLists) },
                { "13", new Tuple<string, Action>("Retrieve marketing list", this.RetrieveMarketingList) },
                { "14", new Tuple<string, Action>("Retrieve marketing lists", this.RetrieveMarketingLists) },
                { "15", new Tuple<string, Action>("Update marketing list", this.UpdateMarketingList) }
            };
        }

        /// <summary>
        /// Demonstrates AddContactToMarketingListByEmailRequest and AddContactsToMarketingListByEmailsResponse.
        /// </summary>
        private void AddContactToMarketingListByEmail()
        {
            Console.WriteLine("This request adds a marketing contact to a list, given the contact's email address.");
            Console.Write("Contact email: ");
            var contactEmail = Console.ReadLine();
            Console.Write("Marketing list ID: ");
            var marketingListId = Console.ReadLine();

            var request = new AddContactToMarketingListByEmailRequest()
                {
                    ContactEmail = contactEmail,
                    MarketingListId = new Guid(marketingListId)
                };

            this.Client.ProcessRequest(request);
        }

        /// <summary>
        /// Handles the AddContactToMarketingListByEmailResponse received from the response queue.
        /// </summary>
        /// <param name="response">The AddContactToMarketingListByEmailResponse received after sending a AddContactToMarketingListByEmailRequest.</param>
        private void ProcessAddContactToMarketingListByEmailResponse(SdkResponse response)
        {
            var addContactToMarketingListByEmailResponse = (AddContactToMarketingListByEmailResponse)response;
            Console.WriteLine("Processing AddContactToMarketingListByEmailResponse.");
            Console.WriteLine("Successfully added: {0}", addContactToMarketingListByEmailResponse.SuccessfullyAdded.ToString(CultureInfo.InvariantCulture));
        }

        /// <summary>
        /// Demonstrates AddContactsToMarketingListByEmailsRequest and AddContactsToMarketingListByEmailsResponse.
        /// </summary>
        private void AddContactsToMarketingListByEmails()
        {
            Console.WriteLine("This request adds multiple marketing contacts to a list, given the contacts' email addresses.");
            Console.Write("Contact emails separated by comma: ");
            var contactEmails = Console.ReadLine();
            Console.Write("Marketing list ID: ");
            var marketingListId = Console.ReadLine();

            var request = new AddContactsToMarketingListByEmailsRequest()
            {
                ContactEmails = contactEmails.Split(',').ToList(),
                MarketingListId = new Guid(marketingListId)
            };

            this.Client.ProcessRequest(request);
        }

        /// <summary>
        /// Handles the AddContactsToMarketingListByEmailsResponse received from the response queue.
        /// </summary>
        /// <param name="response">The AddContactsToMarketingListByEmailsResponse received after sending a AddContactsToMarketingListByEmailsRequest.</param>
        private void ProcessAddContactsToMarketingListByEmailsResponse(SdkResponse response)
        {
            var addContactsToMarketingListByEmailsResponse = (AddContactsToMarketingListByEmailsResponse)response;
            Console.WriteLine("Processing AddContactsToMarketingListByEmailsResponse.");

            if (addContactsToMarketingListByEmailsResponse.SkippedEmails.Count == 0)
            {
                Console.WriteLine("All contacts were added successfully.");
            }
            else
            {
                Console.WriteLine("The following emails were skipped:");

                foreach (var skippedEmail in addContactsToMarketingListByEmailsResponse.SkippedEmails)
                {
                    Console.WriteLine(skippedEmail);
                }
            }
        }

        /// <summary>
        /// Demonstrates AddContactToMarketingListByIdRequest and AddContactToMarketingListByIdResponse.
        /// </summary>
        private void AddContactToMarketingListById()
        {
            Console.WriteLine("This request adds a marketing contact to a list, given the contact's external ID.");
            Console.Write("Contact ID: ");
            var contactId = Console.ReadLine();
            Console.Write("Marketing list ID: ");
            var marketingListId = Console.ReadLine();

            var request = new AddContactToMarketingListByIdRequest()
            {
                ContactId = new Guid(contactId),
                MarketingListId = new Guid(marketingListId)
            };

            this.Client.ProcessRequest(request);
        }

        /// <summary>
        /// Handles the AddContactToMarketingListByIdResponse received from the response queue.
        /// </summary>
        /// <param name="response">The AddContactToMarketingListByIdResponse received after sending an AddContactToMarketingListByIdRequest.</param>
        private void ProcessAddContactToMarketingListByIdResponse(SdkResponse response)
        {
            var addContactToMarketingListByIdResponse = (AddContactToMarketingListByIdResponse)response;
            Console.WriteLine("Processing AddContactToMarketingListByIdResponse.");
            Console.WriteLine("Successfully added: {0}", addContactToMarketingListByIdResponse.SuccessfullyAdded.ToString(CultureInfo.InvariantCulture));
        }

        /// <summary>
        /// Demonstrates AddContactsToMarketingListByIdsRequest and AddContactsToMarketingListByIdsResponse.
        /// </summary>
        private void AddContactsToMarketingListByIds()
        {
            Console.WriteLine("This request adds multiple marketing contacts to a list, given the contacts' external IDs.");
            Console.Write("Contact IDs separated by comma: ");
            var contactIds = Console.ReadLine();
            Console.Write("Marketing list ID: ");
            var marketingListId = Console.ReadLine();

            var request = new AddContactsToMarketingListByIdsRequest()
            {
                ContactIds = contactIds.Split(',').Select(id => new Guid(id)).ToList(),
                MarketingListId = new Guid(marketingListId)
            };

            this.Client.ProcessRequest(request);
        }

        /// <summary>
        /// Handles the AddContactsToMarketingListByIdsResponse received from the response queue.
        /// </summary>
        /// <param name="response">The AddContactsToMarketingListByIdsResponse received after sending an AddContactsToMarketingListByIdsRequest.</param>
        private void ProcessAddContactsToMarketingListByIdsResponse(SdkResponse response)
        {
            var addContactsToMarketingListByIdsResponse = (AddContactsToMarketingListByIdsResponse)response;
            Console.WriteLine("Processing AddContactsToMarketingListByIdsResponse.");

            if (addContactsToMarketingListByIdsResponse.SkippedIds.Count == 0)
            {
                Console.WriteLine("All contacts were added successfully.");
            }
            else
            {
                Console.WriteLine("The following IDs were skipped:");

                foreach (var skippedId in addContactsToMarketingListByIdsResponse.SkippedIds)
                {
                    Console.WriteLine(skippedId.ToString());
                }
            }
        }

        /// <summary>
        /// Demonstrates CopyContactsFromMarketingListRequest and CopyContactsFromMarketingListResponse.
        /// </summary>
        private void CopyContactsFromMarketingList()
        {
            Console.WriteLine("This request copies contacts from one marketing list to another.");
            Console.Write("Source marketing list ID: ");
            var sourceMarketingListId = Console.ReadLine();
            Console.Write("Target marketing list ID: ");
            var targetMarketingListId = Console.ReadLine();

            var request = new CopyContactsFromMarketingListRequest()
            {
                SourceMarketingListId = new Guid(sourceMarketingListId),
                TargetMarketingListId = new Guid(targetMarketingListId)
            };

            this.Client.ProcessRequest(request);
        }

        /// <summary>
        /// Handles the CopyContactsFromMarketingListResponse received from the response queue.
        /// </summary>
        /// <param name="response">The CopyContactsFromMarketingListResponse received after sending a CopyContactsFromMarketingListRequest.</param>
        private void ProcessCopyContactsFromMarketingListResponse(SdkResponse response)
        {
            var copyContactsFromMarketingListResponse = (CopyContactsFromMarketingListResponse)response;
            Console.WriteLine("Processing CopyContactsFromMarketingListResponse.");

            Console.WriteLine("The response was received successfully.");
        }

        /// <summary>
        /// Demonstrates CopyQueryToMarketingListRequest and CopyQueryToMarketingListResponse.
        /// </summary>
        private void CopyQueryToMarketingList()
        {
            Console.WriteLine("This request copies contacts from a query to a new marketing list.");
            Console.Write("Query ID: ");
            var queryId = Console.ReadLine();

            var request = new CopyQueryToMarketingListRequest()
            {
                QueryId = new Guid(queryId)
            };

            this.Client.ProcessRequest(request);
        }

        /// <summary>
        /// Handles the CopyQueryToMarketingListResponse received from the response queue.
        /// </summary>
        /// <param name="response">The CopyQueryToMarketingListResponse received after sending a CopyQueryToMarketingListRequest.</param>
        private void ProcessCopyQueryToMarketingListResponse(SdkResponse response)
        {
            var copyQueryToMarketingListResponse = (CopyQueryToMarketingListResponse)response;
            Console.WriteLine("Processing CopyQueryToMarketingListResponse.");

            Console.WriteLine("Marketing list ID: {0}", copyQueryToMarketingListResponse.MarketingListId.ToString());
        }

        /// <summary>
        /// Demonstrates CreateMarketingListRequest and CreateMarketingListResponse.
        /// </summary>
        private void CreateMarketingList()
        {
            Console.WriteLine("This request creates a new marketing list.");
            Console.Write("Marketing list name: ");
            var name = Console.ReadLine();
            Console.Write("Marketing list description: ");
            var description = Console.ReadLine();

            var request = new CreateMarketingListRequest()
            {
                MarketingListId = Guid.NewGuid(),
                MarketingListName = name,
                MarketingListDescription = description
            };

            this.Client.ProcessRequest(request);
        }

        /// <summary>
        /// Handles the CreateMarketingListResponse received from the response queue.
        /// </summary>
        /// <param name="response">The CreateMarketingListResponse received after sending a CreateMarketingListRequest.</param>
        private void ProcessCreateMarketingListResponse(SdkResponse response)
        {
            var createMarketingListResponse = (CreateMarketingListResponse)response;
            Console.WriteLine("Processing CreateMarketingListResponse.");

            Console.WriteLine("Marketing list ID: {0}", createMarketingListResponse.MarketingListId.ToString());
        }

        /// <summary>
        /// Demonstrates DeleteMarketingListRequest and DeleteMarketingListResponse.
        /// </summary>
        private void DeleteMarketingList()
        {
            Console.WriteLine("This request deletes a marketing list.");
            Console.Write("Marketing list ID: ");
            var marketingListId = Console.ReadLine();

            var request = new DeleteMarketingListRequest()
            {
                MarketingListId = new Guid(marketingListId)
            };

            this.Client.ProcessRequest(request);
        }

        /// <summary>
        /// Handles the DeleteMarketingListResponse received from the response queue.
        /// </summary>
        /// <param name="response">The DeleteMarketingListResponse received after sending a DeleteMarketingListRequest.</param>
        private void ProcessDeleteMarketingListResponse(SdkResponse response)
        {
            var deleteMarketingListResponse = (DeleteMarketingListResponse)response;
            Console.WriteLine("Processing DeleteMarketingListResponse.");

            Console.WriteLine("The response was received successfully.");
        }

        /// <summary>
        /// Demonstrates RemoveAllContactsFromMarketingListRequest and RemoveAllContactsFromMarketingListResponse.
        /// </summary>
        private void RemoveAllContactsFromMarketingList()
        {
            Console.WriteLine("This request removes all contacts from a marketing list.");
            Console.Write("Marketing list ID: ");
            var marketingListId = Console.ReadLine();

            var request = new RemoveAllContactsFromMarketingListRequest()
            {
                MarketingListId = new Guid(marketingListId)
            };

            this.Client.ProcessRequest(request);
        }

        /// <summary>
        /// Handles the RemoveAllContactsFromMarketingListResponse received from the response queue.
        /// </summary>
        /// <param name="response">The RemoveAllContactsFromMarketingListResponse received after sending a RemoveAllContactsFromMarketingListRequest.</param>
        private void ProcessRemoveAllContactsFromMarketingListResponse(SdkResponse response)
        {
            var removeAllContactsFromMarketingListResponse = (RemoveAllContactsFromMarketingListResponse)response;
            Console.WriteLine("Processing RemoveAllContactsFromMarketingListResponse.");

            Console.WriteLine("Successfully removed: {0}", removeAllContactsFromMarketingListResponse.SuccessfullyRemoved.ToString(CultureInfo.InvariantCulture));
        }

        /// <summary>
        /// Demonstrates RemoveContactFromMarketingListByEmailRequest and RemoveContactFromMarketingListByEmailResponse.
        /// </summary>
        private void RemoveContactFromMarketingListByEmail()
        {
            Console.WriteLine("This request removes a contact from a marketing list, given the contact's email.");
            Console.Write("Marketing list ID: ");
            var marketingListId = Console.ReadLine();
            Console.Write("Contact email: ");
            var contactEmail = Console.ReadLine();

            var request = new RemoveContactFromMarketingListByEmailRequest()
            {
                ContactEmail = contactEmail,
                MarketingListId = new Guid(marketingListId)
            };

            this.Client.ProcessRequest(request);
        }

        /// <summary>
        /// Handles the RemoveContactFromMarketingListByEmailResponse received from the response queue.
        /// </summary>
        /// <param name="response">The RemoveContactFromMarketingListByEmailResponse received after sending a RemoveContactFromMarketingListByEmailRequest.</param>
        private void ProcessRemoveContactFromMarketingListByEmailResponse(SdkResponse response)
        {
            var removeContactFromMarketingListByEmailResponse = (RemoveContactFromMarketingListByEmailResponse)response;
            Console.WriteLine("Processing RemoveContactFromMarketingListByEmailResponse.");

            Console.WriteLine("Successfully removed: {0}", removeContactFromMarketingListByEmailResponse.SuccessfullyRemoved.ToString(CultureInfo.InvariantCulture));
        }

        /// <summary>
        /// Demonstrates RemoveContactFromMarketingListByIdRequest and RemoveContactFromMarketingListByIdResponse.
        /// </summary>
        private void RemoveContactFromMarketingListById()
        {
            Console.WriteLine("This request removes a contact from a marketing list, given the contact's external ID.");
            Console.Write("Marketing list ID: ");
            var marketingListId = Console.ReadLine();
            Console.Write("Contact ID: ");
            var contactId = Console.ReadLine();

            var request = new RemoveContactFromMarketingListByIdRequest()
            {
                ContactId = new Guid(contactId),
                MarketingListId = new Guid(marketingListId)
            };

            this.Client.ProcessRequest(request);
        }

        /// <summary>
        /// Handles the RemoveContactFromMarketingListByIdResponse received from the response queue.
        /// </summary>
        /// <param name="response">The RemoveContactFromMarketingListByIdResponse received after sending a RemoveContactFromMarketingListByIdRequest.</param>
        private void ProcessRemoveContactFromMarketingListByIdResponse(SdkResponse response)
        {
            var removeContactFromMarketingListByIdResponse = (RemoveContactFromMarketingListByIdResponse)response;
            Console.WriteLine("Processing RemoveContactFromMarketingListByIdResponse.");

            Console.WriteLine("Successfully removed: {0}", removeContactFromMarketingListByIdResponse.SuccessfullyRemoved.ToString(CultureInfo.InvariantCulture));
        }

        /// <summary>
        /// Demonstrates RetrieveAllListsRequest and RetrieveAllListsResponse.
        /// </summary>
        private void RetrieveAllLists()
        {
            Console.WriteLine("This request retrieves multiple marketing lists from the system.");

            Console.Write("Enter offset - or empty for the first set of records =>");
            var offset = Console.ReadLine();
            var skipNumberOfRecords = 0;
            int.TryParse(offset, out skipNumberOfRecords);

            var request = new RetrieveAllListsRequest { SkipNumberOfRecords = skipNumberOfRecords, MaxNumberOfRecordsToGet = 100};

            this.Client.ProcessRequest(request);
        }

        /// <summary>
        /// Handles the RetrieveAllListsResponse received from the response queue.
        /// </summary>
        /// <param name="response">The RetrieveAllListsResponse received after sending a RetrieveAllListsRequest.</param>
        private void ProcessRetrieveAllListsResponse(SdkResponse response)
        {
            var retrieveAllListsResponse = (RetrieveAllListsResponse)response;
            Console.WriteLine("Processing RetrieveAllListsResponse.");
            Console.WriteLine("Marketing List Count: {0}", retrieveAllListsResponse.MarketingLists.Count().ToString(CultureInfo.InvariantCulture));

            foreach (var marketingList in retrieveAllListsResponse.MarketingLists)
            {
                this.DisplayMarketingList(marketingList);
            }
        }

        /// <summary>
        /// Displays the data of the given marketing list.
        /// </summary>
        /// <param name="marketingList">The marketing list to display.</param>
        private void DisplayMarketingList(MarketingList marketingList)
        {
            Console.WriteLine(
                @"
                ID: {0}
                CRM ID: {1}
                Count: {2}
                Name: {3}
                Description: {4}
                Query: {5}
                QueryTypeCode: {6}
                Created By: {7}
                Created On: {8}
                Modified By: {9}
                Modified On: {10}",
                marketingList.Id,
                marketingList.CrmId,
                marketingList.Count,
                marketingList.Name,
                marketingList.Description,
                marketingList.Query,
                marketingList.QueryTypeCode,
                marketingList.CreatedBy,
                marketingList.CreatedOn.ToString(),
                marketingList.ModifiedBy,
                marketingList.ModifiedOn.ToString(CultureInfo.CurrentCulture));
        }

        /// <summary>
        /// Demonstrates RetrieveMarketingListRequest and RetrieveMarketingListResponse.
        /// </summary>
        private void RetrieveMarketingList()
        {
            Console.WriteLine("This request retrieves a marketing list, given its external ID.");
            Console.Write("Marketing list ID: ");
            var marketingListId = Console.ReadLine();

            var request = new RetrieveMarketingListRequest()
            {
                MarketingListId = new Guid(marketingListId)
            };

            this.Client.ProcessRequest(request);
        }

        /// <summary>
        /// Handles the RetrieveMarketingListResponse received from the response queue.
        /// </summary>
        /// <param name="response">The RetrieveMarketingListResponse received after sending a RetrieveMarketingListRequest.</param>
        private void ProcessRetrieveMarketingListResponse(SdkResponse response)
        {
            var retrieveMarketingListResponse = (RetrieveMarketingListResponse)response;
            Console.WriteLine("Processing RetrieveMarketingListResponse.");

            this.DisplayMarketingList(retrieveMarketingListResponse.MarketingList);
        }

        /// <summary>
        /// Demonstrates RetrieveMarketingListsRequest and RetrieveMarketingListsResponse.
        /// </summary>
        private void RetrieveMarketingLists()
        {
            Console.WriteLine("This request retrieves multiple marketing list, given their external IDs.");
            Console.Write("Marketing list IDs separated by comma: ");
            var marketingListIds = Console.ReadLine();

            Console.Write("Enter offset - or empty for the first set of records =>");
            var offset = Console.ReadLine();
            var skipNumberOfRecords = 0;
            int.TryParse(offset, out skipNumberOfRecords);

            var request = new RetrieveMarketingListsRequest
               {
                   MarketingListIds = marketingListIds.Split(',').Select(id => new Guid(id)).ToList(),
                   SkipNumberOfRecords = skipNumberOfRecords,
                   MaxNumberOfRecordsToGet = 100
               };

            this.Client.ProcessRequest(request);
        }

        /// <summary>
        /// Handles the RetrieveMarketingListsResponse received from the response queue.
        /// </summary>
        /// <param name="response">The RetrieveMarketingListsResponse received after sending a RetrieveMarketingListsRequest.</param>
        private void ProcessRetrieveMarketingListsResponse(SdkResponse response)
        {
            var retrieveMarketingListsResponse = (RetrieveMarketingListsResponse)response;
            Console.WriteLine("Processing RetrieveMarketingListsResponse.");

            Console.WriteLine("Marketing List Count: {0}", retrieveMarketingListsResponse.MarketingLists.Count().ToString(CultureInfo.InvariantCulture));

            foreach (var marketingList in retrieveMarketingListsResponse.MarketingLists)
            {
                this.DisplayMarketingList(marketingList);
            }
        }

        /// <summary>
        /// Demonstrates UpdateMarketingListRequest and UpdateMarketingListResponse.
        /// </summary>
        private void UpdateMarketingList()
        {
            Console.WriteLine("This request updates the name and description of a marketing list.");
            Console.Write("Marketing list ID: ");
            var marketingListId = Console.ReadLine();
            Console.Write("Marketing list Name: ");
            var name = Console.ReadLine();
            Console.Write("Marketing list Description: ");
            var description = Console.ReadLine();

            var request = new UpdateMarketingListRequest()
            {
                MarketingListId = new Guid(marketingListId),
                MarketingListName = name,
                MarketingListDescription = description
            };

            this.Client.ProcessRequest(request);
        }

        /// <summary>
        /// Handles the UpdateMarketingListResponse received from the response queue.
        /// </summary>
        /// <param name="response">The UpdateMarketingListResponse received after sending an UpdateMarketingListRequest.</param>
        private void ProcessUpdateMarketingListResponse(SdkResponse response)
        {
            var updateMarketingListResponse = (UpdateMarketingListResponse)response;
            Console.WriteLine("Processing UpdateMarketingListResponse.");

            Console.Write("Response received successfully.");
        }

        /// <summary>
        /// Handles the <see cref="SdkErrorResponse"/> received from the response queue. Displays the error message.
        /// </summary>
        /// <param name="response">The <see cref="SdkErrorResponse"/> received after sending an <see cref="SdkRequest"/>.</param>
        private void ProcessSdkErrorResponse(SdkResponse response)
        {
            var sdkErrorResponse = (SdkErrorResponse)response;
            Console.WriteLine("An SdkErrorResponse was received.");
            Console.WriteLine("Error message: {0}", sdkErrorResponse.Message);
        }
    }
}

Troubleshooting

  • If the response is not received in time, you’ll get the following error message:

    “The request has been sent but the response was not received. You may want to wait a few more seconds and try to receive the response again or try increasing the ResponseMessageTimeout in client.cs.”

    Some of the possible reasons why the response is not received:

    • The server took longer than expected to process this response. Try selecting Option A, to see the response. If this issue occurs frequently, consider changing the value of the ResponseMessageTimeout constant in your client.cs file.

    • Either your client is looking at the wrong response queue, or Microsoft Dynamics Marketing is unable to write to the response queue due to a configuration error. To know how to setup your queues correctly, see Getting started with the SDK 

  • If you don’t receive a response immediately, try using the Get all responses for this session option. Sometimes a response may take longer than the default time-out period.

  • If you never receive the response, make sure your request and response queue names and namespace matches the details you provided on the Home > Settings > Administrator > Integration Options. More information: Site configuration and integration settings page.

  • Unlike email messages, if you enter an email address that doesn’t match a contact in Microsoft Dynamics Marketing, a new contact won’t be created. Instead, the email address will be skipped or the request will fail.

  • Sign in to Microsoft Dynamics Marketing and go to Home > Marketing Execution > Marketing Lists > Marketing Lists. If you’re not able to view the marketing list that you have created with Option 7, make sure you click the Show All/Active only icon. The marketing lists created though the SDK will be listed as Created By SDK SYNCHRONIZATION AGENT in the user interface.

  • If you add the contacts to the marketing list using the command option Add contact to Marketing list, you get the following error message:

    “Contact <email address> does not exists”

    There is no contact in Dynamics Marketing with the specified email address that you’re using. You must first create the contact in Dynamics Marketing, and then get the external ID for the email message either from the Retrieve action or on the Home > Marketing Execution > Marketing Database > Marketing Contacts by clicking on the Customize column icon and selecting External ID from the list.

See Also

Microsoft.Dynamics.Marketing.SDK.Common
Microsoft.Dynamics.Marketing.SDK.Messages
Microsoft.Dynamics.Marketing.SDK.Messages.MarketingList
Microsoft.Dynamics.Marketing.SDK.Model
Developer overview of Microsoft Dynamics Marketing
Introduction to marketing entities
Manage lists and send transactional or commercial email
Getting started with the SDK
Quick start with sample code
Sample code
Assembly included in the Microsoft Dynamics Marketing SDK
Programming reference for Microsoft Dynamics Marketing