Sample: Retrieve landing page submissions

 

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

Requirements

Note

This sample works with your live production data. It might add, update, or delete external entities in your Microsoft Dynamics Marketing environment.

Demonstrates

This sample demonstrates how to retrieve landing page submissions using Dynamics Marketing.

Example

namespace Microsoft.Dynamics.Marketing.SDK.Samples.LandingPageSample
{
    using System;
    using System.Collections.Generic;
    using Microsoft.Dynamics.Marketing.SDK.Common;
    using Microsoft.Dynamics.Marketing.SDK.Messages;
    using Microsoft.Dynamics.Marketing.SDK.Messages.Landingpage;
    using Microsoft.Dynamics.Marketing.SDK.Model;
    using Microsoft.Dynamics.Marketing.SDK.Samples.SdkClient;
    using Microsoft.Dynamics.Marketing.SDK.Samples.SdkSample;

    /// <summary>
    /// Demonstrates how to use functionality in the landing page namespace.
    /// </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 Landingpage
    /// </remarks>
    public class LandingPageSample : SdkSample
    {
        /// <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 LandingPageSample(queueClientProvider);
                sample.Run();
            }
            catch (Exception ex)
            {
                LandingPageSample.WriteException(ex);
                Console.WriteLine("Press Enter key to exit.");
                Console.ReadLine();
            }
        }

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

            this.SetupClient(queueClientProvider, responseHandlerMapping);

            this.SampleMenuActions = new Dictionary<string, Tuple<string, Action>>
            {
                { "1", new Tuple<string, Action>("Retrieve a submission", this.RetrieveSubmission) },
            };
        }

        /// <summary>
        /// Demonstrates RetrieveSubmissionRequest and RetrieveSubmissionResponse.
        /// </summary>
        private void RetrieveSubmission()
        {
            Console.WriteLine("This request retrieves a submission based on its token.");
            Console.Write("Please type the token for the landing page submission => ");

            string token = Console.ReadLine();
            if (string.IsNullOrEmpty(token))
            {
                Console.WriteLine("Invalid token.");
                return;
            }

            var request = new RetrieveSubmissionRequest { Token = token };
            this.Client.ProcessRequest(request);
        }

        /// <summary>
        /// Handles the RetrieveSubmissionResponse that is received from the response queue.
        /// </summary>
        /// <param name="response">The RetrieveSubmissionResponse received after sending a RetrieveSubmissionRequest.</param>
        private void ProcessRetrieveSubmissionResponse(SdkResponse response)
        {
            var retrieveSubmissionResponse = (RetrieveSubmissionResponse)response;
            Console.WriteLine("Processing RetrieveSubmissionResponse.");

            if (!retrieveSubmissionResponse.Success)
            {
                Console.WriteLine("RetrieveSubmissionResponse failed.");
                Console.WriteLine("Message: {0}", retrieveSubmissionResponse.Message);
                return;
            }

            this.PrintContact(retrieveSubmissionResponse.Submission.Contact);
            this.PrintEventRegistrationSetup(retrieveSubmissionResponse.Submission.EventRegistrationSetup);
            this.PrintEventSession(retrieveSubmissionResponse.Submission.EventSession);
            this.PrintLead(retrieveSubmissionResponse.Submission.Lead);
            this.PrintSubmittedFields(retrieveSubmissionResponse.Submission.Fields);
        }

        /// <summary>
        /// Print the content of a contact.
        /// </summary>
        /// <param name="contact">A <see cref="Contact"/> instance.</param>
        private void PrintContact(Contact contact)
        {
            Console.WriteLine("Contact:");
            Console.WriteLine("  Id: {0}", contact.Id);
            Console.WriteLine("  Name: {0} {1}", contact.FirstName, contact.LastName);
        }

        /// <summary>
        /// Prints the content of an event session.
        /// </summary>
        /// <param name="eventSession">An instance of <see cref="EventSession"/></param>
        private void PrintEventSession(EventSession eventSession)
        {
            Console.WriteLine("Event session:");
            Console.WriteLine("  Id: {0}", eventSession == null ? string.Empty : eventSession.Id.ToString());
            Console.WriteLine("  Name: {0}", eventSession == null ? string.Empty : eventSession.Name);
        }

        /// <summary>
        /// Prints the content of an event registration setup.
        /// </summary>
        /// <param name="eventRegistrationSetup">An instance of <see cref="EventRegistrationSetup"/></param>
        private void PrintEventRegistrationSetup(EventRegistrationSetup eventRegistrationSetup)
        {
            Console.WriteLine("Event registration setup:");
            Console.WriteLine("  Id: {0}", eventRegistrationSetup.Id);
            Console.WriteLine("  Name: {0}", eventRegistrationSetup.Name);
        }

        /// <summary>
        /// Print the content of a lead.
        /// </summary>
        /// <param name="lead">The lead</param>
        private void PrintLead(Lead lead)
        {
            Console.WriteLine("Lead:");
            Console.WriteLine("  Id: {0}", lead.Id);
        }

        /// <summary>
        /// Prints the list of submitted fields with their values.
        /// </summary>
        /// <param name="fields">An <see cref="IEnumerable{Tuple{string,string}}"/> containing all submitted fields and their values.</param>
        private void PrintSubmittedFields(IEnumerable<Tuple<string, string>> fields)
        {
            Console.WriteLine("Submitted fields:");
            foreach (var field in fields)
            {
                Console.WriteLine("  {0} = {1}", field.Item1, field.Item2);
            }
        }

        /// <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.Messages.Landingpage
Microsoft.Dynamics.Marketing.SDK.Common
Microsoft.Dynamics.Marketing.SDK.Model
Developer overview of Microsoft Dynamics Marketing
Integrate online payment with event registration
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