Sample: Create, read, update, and delete contact (including custom fields)

 

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\ContactSample

Requirements

Note

This sample works with your live production data. It might create real contacts in your Microsoft Dynamics Marketing environment.

Demonstrates

This sample demonstrates how to create, retrieve, update, and delete a contact using Dynamics Marketing.

Example

namespace Microsoft.Dynamics.Marketing.SDK.Samples.ContactSample
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using Microsoft.Dynamics.Marketing.SDK.Common;
    using Microsoft.Dynamics.Marketing.SDK.Messages;
    using Microsoft.Dynamics.Marketing.SDK.Messages.Contact;
    using Microsoft.Dynamics.Marketing.SDK.Model;
    using Microsoft.Dynamics.Marketing.SDK.Samples.SdkClient;
    using Microsoft.Dynamics.Marketing.SDK.Samples.SdkSample;

    /// <summary>
    /// Demonstrates how to perform create, read, update, and delete operations on the Contact entity.
    /// </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 Microsoft Azure Service Bus queues on the Integration Options page in Microsoft Dynamics Marketing.
    /// You must also have permissions enabled for Contact
    /// </remarks>
    public class ContactSample : SdkSample
    {
        private bool updateRequestSent;

        /// <summary>
        /// Starting point for the 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 ContactSample(queueClientProvider);
                sample.Run();
            }
            catch (Exception ex)
            {
                ContactSample.WriteException(ex);
                Console.WriteLine("Press Enter key to exit.");
                Console.ReadLine();
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ContactSample"/> class.
        /// </summary>
        /// <param name="queueClientProvider">
        /// The QueueClientProvider that’s set up to connect to the SDK queues configured in Microsoft Dynamics Marketing.
        /// </param>
        public ContactSample(IQueueClientProvider queueClientProvider)
        {
            this.updateRequestSent = false;
            var responseHandlerMapping = new Dictionary<Type, Client.ResponseHandler>
            {
                { typeof(RetrieveContactResponse), this.ProcessRetrieveContactResponse },
                { typeof(CreateOrUpdateContactResponse), this.ProcessCreateOrUpdateContactResponse },
                { typeof(DeleteContactResponse), this.ProcessDeleteContactResponse },
                { typeof(ActivateContactResponse), this.ProcessActivateContactResponse },
                { typeof(RetrieveSalutationsResponse), this.ProcessRetrieveSalutationsResponse },
                { typeof(RetrieveCurrenciesResponse), this.ProcessRetrieveCurrenciesResponse },
                { typeof(RetrieveLanguagesResponse), this.ProcessRetrieveLanguagesResponse },
                { typeof(RetrieveContactsResponse), this.ProcessRetrieveContactsResponse },
                { typeof(CreateOrUpdateContactsWithResultsResponse), this.ProcessCreateOrUpdateContactsWithResultsResponse },
                { typeof(PartialContactUpdateResponse), this.ProcessPartialContactUpdateResponse },
                { typeof(PartialContactsUpdateResponse), this.ProcessPartialContactsUpdateResponse },
                { typeof(SdkErrorResponse), this.ProcessSdkErrorResponse }
            };

            this.SetupClient(queueClientProvider, responseHandlerMapping);

            this.SampleMenuActions = new Dictionary<string, Tuple<string, Action>>
            {
                { "1", new Tuple<string, Action>("Retrieve a Contact", this.RetrieveContact) },
                { "2", new Tuple<string, Action>("Create a Contact", this.CreateContact) },
                { "3", new Tuple<string, Action>("Update a Contact", this.UpdateContact) },
                { "4", new Tuple<string, Action>("Delete a Contact", this.DeleteContact) },
                { "5", new Tuple<string, Action>("Activate a Contact", this.ActivateContact) },
                { "6", new Tuple<string, Action>("Retrieve a list of salutations", this.RetrieveSalutations) },
                { "7", new Tuple<string, Action>("Retrieve a list of currencies", this.RetrieveCurrencies) },
                { "8", new Tuple<string, Action>("Retrieve a list of languages", this.RetrieveLanguages) },
                { "9", new Tuple<string, Action>("Retrieve multiple Contacts", this.RetrieveContacts) },
                { "10", new Tuple<string, Action>("Create multiple Contacts", this.CreateContacts) },
                { "11", new Tuple<string, Action>("Partial update of contact.", this.PartialUpdate) },
                { "12", new Tuple<string, Action>("Partial update of contacts.", this.PartialUpdates) },
            };
        }

        /// <summary>
        /// Demonstrates RetrieveContactRequest and RetrieveContactResponse.
        /// </summary>
        private void RetrieveContact()
        {
            Console.WriteLine("This request retrieves a contact.");
            Console.Write("Please type the ID (GUID) for the contact to retrieve => ");
            Guid id;
            if (Guid.TryParse(Console.ReadLine(), out id))
            {
                var request = new RetrieveContactRequest { ContactId = id };
                this.Client.ProcessRequest(request);
            }
            else
            {
                Console.WriteLine("Incorrect ID");
            }
        }

        /// <summary>
        /// Handles the RetrieveContactResponse that is received from the response queue. Displays the contact.
        /// </summary>
        /// <param name="response">The RetrieveContactResponse received after sending a RetrieveContactRequest.</param>
        private void ProcessRetrieveContactResponse(SdkResponse response)
        {
            var retrieveContactResponse = (RetrieveContactResponse)response;
            Console.WriteLine("Processing RetrieveContactResponse.");

            if (this.updateRequestSent)
            {
                Console.WriteLine("Old last name for Contact: " + retrieveContactResponse.Contact.LastName);
                Console.Write("Please type the new last name for the Contact => ");
                var name = Console.ReadLine();
                if (string.IsNullOrEmpty(name))
                {
                    Console.WriteLine("Incorrect Name");
                    return;
                }

                var contact = retrieveContactResponse.Contact;
                contact.LastName = name;
                var request = new CreateOrUpdateContactRequest { Contact = contact };
                this.Client.ProcessRequest(request);
            }
            else
            {
                this.PrintContact(retrieveContactResponse.Contact);
            }
        }

        /// <summary>
        /// Print the content of a contact.
        /// </summary>
        /// <param name="contact">The contact</param>
        private void PrintContact(Contact contact)
        {
            foreach (var propertyInfo in contact.GetType().GetProperties().ToList())
            {
                var value = propertyInfo.GetValue(contact);
                var company = value as Company;
                if (company != null)
                {
                    Console.WriteLine(propertyInfo.Name + ":" + company.Id + " (" + company.Name + ")");
                    continue;
                }

                if (string.CompareOrdinal(propertyInfo.Name, "OriginOfChanges") == 0)
                {
                    Console.WriteLine(propertyInfo.Name + ":" + string.Join(";", ((ICollection<string>)value).Distinct()));
                    continue;
                }

                if (propertyInfo.Name == "CustomFields")
                {
                    var customFields = (Dictionary<string, object>)value;
                    foreach (var customField in customFields)
                    {
                        var category = customField.Value as Category;
                        if (category != null)
                        {
                            Console.WriteLine(customField.Key + "Id:" + category.Id);
                            Console.WriteLine(customField.Key + "Name:" + category.Name);
                        }
                        else
                        {
                            Console.WriteLine(customField.Key + ":" + customField.Value);
                        }
                    }
                }
                else
                {
                    if (propertyInfo.PropertyType == typeof(Currency) && value != null)
                    {
                        Console.WriteLine(propertyInfo.Name + ":" + ((Currency)value).Name);
                    }
                    else
                    {
                        Console.WriteLine(propertyInfo.Name + ":" + value);
                    }
                }
            }
        }

        /// <summary>
        /// Demonstrates how to create a contact
        /// </summary>
        private void CreateContact()
        {
            Console.WriteLine("This request creates a Contact.");
            var contact = CreateContactModel(string.Empty);

            if (contact == null)
            {
                return;
            }

            var request = new CreateOrUpdateContactRequest { Contact = contact };
            this.Client.ProcessRequest(request);
        }

        /// <summary>
        /// Demonstrates how to create multiple contacts.
        /// </summary>
        private void CreateContacts()
        {
            Console.WriteLine("This request will create multiple Contacts.");
            var contact1 = CreateContactModel("1");
            var contact2 = CreateContactModel("2");

            if (contact1 == null || contact2 == null)
            {
                return;
            }

            var request = new CreateOrUpdateContactsWithResultsRequest() { Contacts = new[] { contact1, contact2 } };
            this.Client.ProcessRequest(request);
        }

        private static Contact CreateContactModel(string contactId)
        {
            Console.Write("Please type the ID (GUID) for contact {0} => ", contactId);
            Guid id;
            if (!Guid.TryParse(Console.ReadLine(), out id))
            {
                Console.WriteLine("Incorrect ID");
                return null;
            }

            Console.Write("Please type the last name for contact {0} => ", contactId);
            var name = Console.ReadLine();
            if (string.IsNullOrEmpty(name))
            {
                Console.WriteLine("Incorrect Name");
                return null;
            }

            Console.Write("Please type the ID (GUID) for the company that contact {0} belongs to => ", contactId);
            Guid belongsToCompanyId;
            if (!Guid.TryParse(Console.ReadLine(), out belongsToCompanyId))
            {
                Console.WriteLine("Incorrect belongs to ID");
                return null;
            }

            return new Contact
            {
                Id = id,
                LastName = name,
                BelongsToCompany = new Company { Id = belongsToCompanyId },
                IsMarketing = true
            };
        }

        /// <summary>
        /// Demonstrates how to update a contact.
        /// </summary>
        private void UpdateContact()
        {
            Console.WriteLine("This request updates a contact.");
            Console.Write("Please type the ID (GUID) for the Contact => ");
            Guid id;
            if (!Guid.TryParse(Console.ReadLine(), out id))
            {
                Console.WriteLine("Incorrect ID");
                return;
            }

            this.updateRequestSent = true;
            var request = new RetrieveContactRequest { ContactId = id };
            this.Client.ProcessRequest(request);
        }

        /// <summary>
        /// Demonstrates how to do partial updates of a contact.
        /// </summary>
        private void PartialUpdate()
        {
            Console.WriteLine("This request updates a contact.");
            Console.Write("Please type the ID (GUID) for the Contact => ");
            Guid id;
            if (!Guid.TryParse(Console.ReadLine(), out id))
            {
                Console.WriteLine("Incorrect ID");
                return;
            }

            var request = new PartialContactUpdateRequest { };
            request.PartialContactUpdateItem = this.BuildPartialContactUpdateItem(id);
            this.Client.ProcessRequest(request);
        }

        /// <summary>
        /// Demonstrates how to do partial updates of multiple contacts.
        /// </summary>
        private void PartialUpdates()
        {
            Console.WriteLine("This request updates multiple contacts.");

            var partialContactUpdateItems = new List<PartialContactUpdateItem>();
            while (true)
            {
                Console.Write("Please type the ID (GUID) for the Contact => ");
                var input = Console.ReadLine();
                if (string.IsNullOrEmpty(input))
                {
                    break;
                }

                Guid id;
                if (!Guid.TryParse(input, out id))
                {
                    Console.WriteLine("Invalid Identifier");
                    continue;
                }

                var item = this.BuildPartialContactUpdateItem(id);
                partialContactUpdateItems.Add(item);
            }

            if (!partialContactUpdateItems.Any())
            {
                Console.WriteLine("No items added. Aborting.");
                return;
            }

            var request = new PartialContactsUpdateRequest
            {
                EnableCreationOrUpdateOfCompany = false, // do not create or update companies.
                PartialContactUpdateItems = partialContactUpdateItems,
            };

            this.Client.ProcessRequest(request);
        }

        private PartialContactUpdateItem BuildPartialContactUpdateItem(Guid id)
        {
            var partialContactUpdateItem = new PartialContactUpdateItem();
            partialContactUpdateItem.Id = id;
            partialContactUpdateItem.ContactFields = new Dictionary<ContactFields, object>
            {
                { ContactFields.FirstName, "TestFirstname" },
                { ContactFields.LastName, "LastName" },
                { ContactFields.OriginOfChanges, new Collection<string> { "SDK" } },

                /* some fields require instances of objects, such as:
                { ContactFields.Company, new Company { Id = Guid.Parse("7EB18FE6-AC3A-481C-8DF0-7F4F02367F09") }},
                { ContactFields.TimeZone, TimeZoneInfo.Utc },
                { ContactFields.Language, new Language { Id = Guid.Parse("7EB18FE6-AC3A-481C-8DF0-7F4F02367F0A") } },
                { ContactFields.Currency, new Currency { Id = Guid.Parse("7EB18FE6-AC3A-481C-8DF0-7F4F02367F0B") } },
                { ContactFields.Territory, new Territory { Id = Guid.Parse("7EB18FE6-AC3A-481C-8DF0-7F4F02367F0C") } },
                { ContactFields.Salutation, new Salutation { Id = Guid.Parse("7EB18FE6-AC3A-481C-8DF0-7F4F02367F0D") } },
                { ContactFields.BelongsToCompany, new Company { Id = Guid.Parse("7EB18FE6-AC3A-481C-8DF0-7F4F02367F0E") } }, */
            };

            /* manipulate PartialContactUpdateItem.CustomFields to add custom field changes:
             * partialContactUpdateItem.CustomFields = new Dictionary<string, object>();
             * partialContactUpdateItem.CustomFields.Add("NameOfCustomField", "New value of the custom field.");
            */

            return partialContactUpdateItem;
        }

        /// <summary>
        /// Get user input to update regular contact properties of <paramref name="item"/>.
        /// </summary>
        /// <param name="item">An instance of <see cref="PartialContactUpdateItem"/>.</param>
        private void PopulateContactFieldUpdates(PartialContactUpdateItem item)
        {
            var i = 0;

            var fields = Enum.GetValues(typeof(ContactFields)).Cast<ContactFields>().Skip(1).ToDictionary(p => i++);
            foreach (var field in fields)
            {
                Console.WriteLine("{0}\t{1}", field.Key, field.Value.ToString());
            }

            while (true)
            {
                Console.WriteLine("Select value to change:");
                var selection = Console.ReadLine();
                if (selection == string.Empty)
                {
                    return;
                }

                var intSelection = 0;
                if (int.TryParse(selection, out intSelection))
                {
                    if (fields.ContainsKey(intSelection))
                    {
                        var selectedField = fields[intSelection];
                        Console.WriteLine("Input new value for {0}:", selectedField.ToString());

                        var value = Console.ReadLine();
                        item.ContactFields[selectedField] = value;
                    }
                }
            }
        }

        /// <summary>
        /// Handles the <see cref="CreateOrUpdateContactResponse"/> that is received from the response queue. Displays the contact.
        /// </summary>
        /// <param name="response">The <see cref="CreateOrUpdateContactResponse"/> received after sending a <see cref="CreateOrUpdateContactRequest"/>.</param>
        private void ProcessCreateOrUpdateContactResponse(SdkResponse response)
        {
            var createOrUpdateContactResponse = (CreateOrUpdateContactResponse)response;
            Console.WriteLine("Processing CreateOrUpdateContactResponse.");

            if (!createOrUpdateContactResponse.Succeeded)
            {
                Console.WriteLine("Failed to create/update Contact");
                return;
            }

            this.PrintContact(createOrUpdateContactResponse.Contact);

            this.updateRequestSent = false;
        }

        /// <summary>
        /// Demonstrates how to delete a Contact
        /// </summary>
        private void DeleteContact()
        {
            Console.WriteLine("This request deletes a contact.");
            Console.Write("Please type the ID (GUID) for the Contact => ");
            Guid id;
            if (!Guid.TryParse(Console.ReadLine(), out id))
            {
                Console.WriteLine("Incorrect ID");
                return;
            }

            var request = new DeleteContactRequest { ContactId = id };
            this.Client.ProcessRequest(request);
        }

        /// <summary>
        /// Demonstrates how to activate a Contact
        /// </summary>
        private void ActivateContact()
        {
            Console.WriteLine("This request activates a contact.");
            Console.Write("Please type the ID (GUID) for the Contact => ");
            Guid id;
            if (!Guid.TryParse(Console.ReadLine(), out id))
            {
                Console.WriteLine("Incorrect ID");
                return;
            }

            var request = new ActivateContactRequest { ContactId = id };
            this.Client.ProcessRequest(request);
        }

        /// <summary>
        /// Handles the <see cref="CreateOrUpdateContactsResponse"/> that is received from the response queue. Displays the contact.
        /// </summary>
        /// <param name="response">The <see cref="CreateOrUpdateContactResponse"/> received after sending a <see cref="CreateOrUpdateContactRequest"/>.</param>
        private void ProcessCreateOrUpdateContactsWithResultsResponse(SdkResponse response)
        {
            var createOrUpdateContactsResponse = (CreateOrUpdateContactsWithResultsResponse)response;
            Console.WriteLine("Processing CreateOrUpdateContactsWithResultsResponse.");

            // Get the result details
            var resultDetails = createOrUpdateContactsResponse.ResultDetails;

            // Iterate over the result details to check if there is any result failed
            foreach (var result in resultDetails)
            {
                // In case any result has been marked as not succeeded, print the error message to see why it has failed
                if (!result.Succeeded)
                {
                    Console.WriteLine("Failed to create/update Contact " + result.Message);
                }
            }

            this.updateRequestSent = false;
        }

        /// <summary>
        /// Handles the <see cref="ActivateContactResponse"/> that is received from the response queue.
        /// </summary>
        /// <param name="response">The <see cref="ActivateContactResponse"/> received after sending a <see cref="ActivateContactRequest"/>.</param>
        private void ProcessActivateContactResponse(SdkResponse response)
        {
            var activateContactResponse = (ActivateContactResponse)response;
            Console.WriteLine("Processing ActivateContactResponse.");

            Console.WriteLine(activateContactResponse.Succeeded ? "Contact activated" : "Failed to activate Contact");
        }

        /// <summary>
        /// Handles the <see cref="DeleteContactResponse"/> that is received from the response queue.
        /// </summary>
        /// <param name="response">The <see cref="DeleteContactResponse"/> received after sending a <see cref="DeleteContactRequest"/>.</param>
        private void ProcessDeleteContactResponse(SdkResponse response)
        {
            var deleteContactResponse = (DeleteContactResponse)response;
            Console.WriteLine("Processing DeleteContactResponse.");

            Console.WriteLine(deleteContactResponse.Succeeded ? "Contact deleted" : "Failed to delete Contact");
        }

        /// <summary>
        /// Demonstrates how to retrieve salutations.
        /// </summary>
        private void RetrieveSalutations()
        {
            Console.WriteLine("This request retrieves a list of salutations.");
            var request = new RetrieveSalutationsRequest();
            this.Client.ProcessRequest(request);
        }

        /// <summary>
        /// Handles the <see cref="RetrieveSalutationsResponse"/> that is received from the response queue.
        /// </summary>
        /// <param name="response">The <see cref="RetrieveSalutationsResponse"/> received after sending a <see cref="RetrieveSalutationsRequest"/>.</param>
        private void ProcessRetrieveSalutationsResponse(SdkResponse response)
        {
            var retrieveSalutationsResponse = (RetrieveSalutationsResponse)response;
            Console.WriteLine("Processing RetrieveSalutationsResponse.");

            if (retrieveSalutationsResponse.Succeeded)
            {
                Console.WriteLine("List of salutations");
                Console.WriteLine("ID\t\t\t\t\tName");
                foreach (var priority in retrieveSalutationsResponse.Salutations)
                {
                    Console.WriteLine(priority.Id + "\t" + priority.Name);
                }
            }
            else
            {
                Console.WriteLine("Failed to retrieve salutations");
            }
        }

        /// <summary>
        /// Demonstrates how to retrieve currencies.
        /// </summary>
        private void RetrieveCurrencies()
        {
            Console.WriteLine("This request retrieves a list of currencies.");
            var request = new RetrieveCurrenciesRequest();
            this.Client.ProcessRequest(request);
        }

        /// <summary>
        /// Handles the <see cref="RetrieveCurrenciesResponse"/> that is received from the response queue.
        /// </summary>
        /// <param name="response">The <see cref="RetrieveCurrenciesResponse"/> received after sending a <see cref="RetrieveCurrenciesRequest"/>.</param>
        private void ProcessRetrieveCurrenciesResponse(SdkResponse response)
        {
            var retrieveCurrenciesResponse = (RetrieveCurrenciesResponse)response;
            Console.WriteLine("Processing RetrieveCurrenciesResponse.");

            if (retrieveCurrenciesResponse.Succeeded)
            {
                Console.WriteLine("List of currencies");
                Console.WriteLine("ID\t\t\t\t\tName");
                foreach (var currency in retrieveCurrenciesResponse.Currencies)
                {
                    Console.WriteLine(currency.Id + "\t" + currency.Name);
                }
            }
            else
            {
                Console.WriteLine("Failed to retrieve currencies");
            }
        }

        /// <summary>
        /// Demonstrates how to retrieve languages.
        /// </summary>
        private void RetrieveLanguages()
        {
            Console.WriteLine("This request retrieves a list of languages.");
            var request = new RetrieveLanguagesRequest();
            this.Client.ProcessRequest(request);
        }

        /// <summary>
        /// Handles the <see cref="RetrieveLanguagesResponse"/> that is received from the response queue.
        /// </summary>
        /// <param name="response">The <see cref="RetrieveLanguagesResponse"/> received after sending a <see cref="RetrieveLanguagesRequest"/>.</param>
        private void ProcessRetrieveLanguagesResponse(SdkResponse response)
        {
            var retrieveLanguagesResponse = (RetrieveLanguagesResponse)response;
            Console.WriteLine("Processing RetrieveLanguagesResponse.");

            if (retrieveLanguagesResponse.Succeeded)
            {
                Console.WriteLine("List of languages");
                Console.WriteLine("ID\t\t\t\t\tName");
                foreach (var language in retrieveLanguagesResponse.Languages)
                {
                    Console.WriteLine(language.Id + "\t" + language.Name);
                }
            }
            else
            {
                Console.WriteLine("Failed to retrieve languages");
            }
        }

        /// <summary>
        /// Demonstrates how to retrieve multiple contacts.
        /// </summary>
        private void RetrieveContacts()
        {
            Console.WriteLine("This request will retrieve multiple Contacts.");
            Console.Write("Filter on Origin of changes =>");
            var originOfChange = Console.ReadLine();

            Console.Write("Enter offset - or empty for the first set of records =>");
            var offset = Console.ReadLine();
            var skipNumberOfRecords = string.IsNullOrEmpty(offset) ? 0 : int.Parse(offset);

            var request = new RetrieveContactsRequest { SkipNumberOfRecords = skipNumberOfRecords, MaxNumberOfRecordsToGet = 100, OriginOfChange = originOfChange, IncludeDeletedContacts = true };
            this.Client.ProcessRequest(request);
        }

        /// <summary>
        /// Handles the RetrieveContactsResponse that is received from the response queue. Displays the contacts.
        /// </summary>
        /// <param name="response">The RetrieveContactsResponse received after sending a RetrieveContactsRequest.</param>
        private void ProcessRetrieveContactsResponse(SdkResponse response)
        {
            var retrieveContactsResponse = (RetrieveContactsResponse)response;
            Console.WriteLine("Processing RetrieveContactsResponse.");

            var counter = 1;
            foreach (var contact in retrieveContactsResponse.Contacts)
            {
                Console.WriteLine("\nContact #{0}", counter++);
                this.PrintContact(contact);
            }
        }

        /// <summary>
        /// Handles the PartialContactUpdateResponse received from the PartialContactUpdateRequest.
        /// </summary>
        /// <param name="response">An instance of PartialContactUpdateResponse.</param>
        private void ProcessPartialContactUpdateResponse(SdkResponse response)
        {
            var partialContactUpdateResponse = (PartialContactUpdateResponse)response;
            Console.WriteLine("Processing PartialContactUpdateResponse");
            Console.WriteLine(
                "ID: {0}, Success: {1}, Message: {2}",
                partialContactUpdateResponse.Id,
                partialContactUpdateResponse.Succeeded,
                partialContactUpdateResponse.Message);
        }

        /// <summary>
        /// Handles the PartialContactsUpdateResponse received from the PartialContactsUpdateRequest.
        /// </summary>
        /// <param name="response">An instance of PartialContactsUpdateResponse.</param>
        private void ProcessPartialContactsUpdateResponse(SdkResponse response)
        {
            var partialContactsUpdateResponse = (PartialContactsUpdateResponse)response;
            Console.WriteLine("Processing PartialContactUpdateResponse");
            Console.WriteLine(partialContactsUpdateResponse.Message);

            foreach (var reply in partialContactsUpdateResponse.Responses)
            {
                Console.WriteLine(
                    "\tID: {0}, Success: {1}, Message: {2}",
                    reply.Id,
                    reply.Succeeded,
                    reply.Message);
            }
        }

        /// <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 isn’t 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 wasn’t 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 learn how to set up 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 match the details you provided on Home > Settings > Administrator > Integration Options. More information: Site configuration and integration settings.

See Also

Microsoft.Dynamics.Marketing.SDK.Common
Microsoft.Dynamics.Marketing.SDK.Messages
Microsoft.Dynamics.Marketing.SDK.Messages.Contact
Microsoft.Dynamics.Marketing.SDK.Model
Developer overview of Microsoft Dynamics Marketing
Manage leads, contacts, and company
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