Sample: Retrieve custom fields by Belongs-To company and entity


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:




This sample demonstrates how to retrieve custom fields in a company that’s using Microsoft Dynamics Marketing.


namespace CustomFieldSample
    using System;
    using System.Globalization;
    using System.Collections.Generic;
    using Microsoft.Dynamics.Marketing.SDK.Common;
    using Microsoft.Dynamics.Marketing.SDK.Messages;
    using Microsoft.Dynamics.Marketing.SDK.Messages.CustomField;
    using Microsoft.Dynamics.Marketing.SDK.Samples.SdkClient;
    using Microsoft.Dynamics.Marketing.SDK.Samples.SdkSample;

    /// <summary>
    /// Demonstrates how to get existing custom fields.
    /// </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 contacts.
    /// </remarks>
    public class CustomFieldSample : SdkSample
        /// <summary>
        /// Starting point for the sample.
        /// </summary>
        /// <param name="args">Arguments needed for authentication.</param>
        public static void Main(string[] args)
                // 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 CustomFieldSample(queueClientProvider);
            catch (Exception ex)
                Console.WriteLine("Press Enter key to exit.");

        /// <summary>
        /// Initializes a new instance of the <see cref="CustomFieldSample"/> class.
        /// </summary>
        /// <param name="queueClientProvider">
        /// The QueueClientProvider that’s set up to connect to the SDK queues configured in Microsoft Dynamics Marketing.
        /// </param>
        public CustomFieldSample(IQueueClientProvider queueClientProvider)
            var responseHandlerMapping = new Dictionary<Type, Client.ResponseHandler>
                { typeof(RetrieveCustomFieldsResponse), this.ProcessRetrieveCustomFieldsResponse },
                { typeof(SdkErrorResponse), this.ProcessSdkErrorResponse }

            this.SetupClient(queueClientProvider, responseHandlerMapping);

            this.SampleMenuActions = new Dictionary<string, Tuple<string, Action>>
                { "1", new Tuple<string, Action>("Retrieve custom fields", this.RetrieveCustomFields) }

        /// <summary>
        /// Demonstrates RetrieveCustomFieldsRequest and RetrieveCustomFieldsResponse.
        /// </summary>
        private void RetrieveCustomFields()
            Console.WriteLine("This request retrieves custom fields.");
            Console.Write("Please type the ID (GUID) for Belongs To Company to retrieve custom fields=> ");
            Guid id;
            if (!Guid.TryParse(Console.ReadLine(), out id))
                Console.WriteLine("Incorrect ID");

            Console.Write("Please type the name of the entity to retrieve custom fields=> ");

            var entityTypeName = Console.ReadLine();

            var request = new RetrieveCustomFieldsRequest { BelongsToCompanyId = id, EntityTypeName = entityTypeName };

        /// <summary>
        /// Handles the RetrieveCustomFieldsResponse that’s received from the response queue and displays the result.
        /// </summary>
        /// <param name="response">The RetrieveCustomFieldsResponse received after sending a RetrieveCustomFieldsRequest.</param>
        private void ProcessRetrieveCustomFieldsResponse(SdkResponse response)
            var retrieveContactResponse = (RetrieveCustomFieldsResponse)response;

            if (retrieveContactResponse.Succeeded)
                    "\nReceiving the list of custom fields requested for BelongsToCompanyId = {0} where the EntityTypeName is: {1}\n\n", 

                Console.WriteLine("List of custom fields\n");
                Console.WriteLine("Field Name\t\t\t\t\tField Type");

                foreach (var udf in retrieveContactResponse.CustomFields)
                    Console.WriteLine(udf.FieldName + "\t\t\t\t\t" + udf.FieldTypeName);
                Console.WriteLine("Failed to retrieve custom fields.");

        /// <summary>
        /// Handles the <see cref="SdkErrorResponse"/> received from the response queue and 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);


  • 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

Developer overview of Microsoft Dynamics Marketing
Provide customized data to your users
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