Create Template

Creating a template is an important function of MMP Enterprise.

Please check Template > CreateTemplate API documentation.

Here is a simple function to call CreateTemplate API.

 public static async Task<string> CreateTemplate()
{
    try
    {
        // Create an email template
        using (HttpClient client = new HttpClient())
        {
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        // Better you create a class for request and serialize it here... 
        var request = "{\"accountId\": 17,\"name\": \"name_of_the_template\",\"subject\": \"Subject of the email\",\"body\":   
        \"html> <head> </head> <body> Dear {{name}} {{surname}}, <a href=\"https:\/\/www.domain.com\/\">Please click the link.</a> <body> </html>\",\"poolId\": 8,\"mode\": \"HTML\",\"publicId\": \"JanNewsletter20200319",\"preHeader\":\"preheader_of_email\",\"fromid\": 15,\"replyId\": 12}";

        // Target Url of at api...
        var uri = new Uri("[[BaseUrlOfApi]]/rest/template/email/create");

        // Perform api call...
        var msg = new HttpRequestMessage(HttpMethod.Post, uri);
        msg.Content = new StringContent(request, Encoding.UTF8, "application/json");
        HttpResponseMessage response = await client.SendAsync(msg, HttpCompletionOption.ResponseHeadersRead);

        //Reading result & returning...
        var result = string.Empty;
        HttpContent content = response.Content;
        using (Task<string> completedTask = content.ReadAsStringAsync())
        {
          result = completedTask.Result;
          completedTask.Dispose();
        }

        // If success, again you better create a class for api response and deserialize it here...
        return result;
    }
    catch (Exception ex)
    {
        System.Console.WriteLine(ex);
        return null;
    }
}

accountId: This parameter must be given to you before you start developing.

publicId: Unique template id. If you don't give an id for the template, the system creates its own public id and puts this id in response. This parameter is used in SendBatch to define the template.

You can get the following ids by calling Configuration > GetSendParams function.

replyId: The id for reply address. This parameter must be created beforehand.

fromId: The id for from address. from nam/from address pair must be created beforehand.

poolId: The id for from pool. This parameter must be created beforehand.

The followings are optional parameters.

jsonData and csvData: This is the data to test the template through interface. You can ignore these parameters.

 public static async Task<string> CreateTemplate()
{
    try
    {
        using (HttpClient client = new HttpClient())
        {
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            // 1 - Get Token...
            var tokenUrl = "[[BASE_API_URL]]/rest/login";
            var tokenMsg = new HttpRequestMessage(HttpMethod.Post, tokenUrl);
            var userName = "[[USERNAME]]";
            var password = "[[PASSWORD]]";

            var tokenRequest =
                "{" +
                    $"\"userName\": \"{userName}\"," +
                    $"\"password\": \"{password}\"" +
                "}";

            tokenMsg.Content = new StringContent(tokenRequest, Encoding.UTF8, "application/json");
            HttpResponseMessage response = await client.SendAsync(tokenMsg, HttpCompletionOption.ResponseHeadersRead);

            var result = string.Empty;
            HttpContent content = response.Content;
            using (Task<string> completedTask = content.ReadAsStringAsync())
            {
                result = completedTask.Result;
                completedTask.Dispose();
            }

            dynamic deserializedResultObject = JsonConvert.DeserializeObject(result);
            var token = deserializedResultObject.token.Value;

            // Add token to request...
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

            // Better you create a class for request and serialize it here... 
            var request = "{\"accountId\": 1,\"name\": \"your template name\",\"body\": \"Dear Our Customer {Name}. This is a test message for you.\",\"publicId\": \"6666\",\"poolId\": 1}";

            // Target Url of at api...
            var uri = new Uri("[[BaseUrlOfApi]]/rest/template/email/create");

            // Perform api call...
            var msg = new HttpRequestMessage(HttpMethod.Post, uri);
            msg.Content = new StringContent(request, Encoding.UTF8, "application/json");
            response = await client.SendAsync(msg, HttpCompletionOption.ResponseHeadersRead);

            //Reading result & returning...
            result = string.Empty;
            content = response.Content;
            using (Task<string> completedTask = content.ReadAsStringAsync())
            {
                result = completedTask.Result;
                completedTask.Dispose();
            }

            // If success, again you better create a class for api response and deserialize it here...
            return result;
        }
    }
    catch (Exception ex)
    {
        System.Console.WriteLine(ex);
        return null;
    }
}

accountId: This parameter must be given to you before you develop.

publicId: Unique template id. If you don't give it, the system creates its own public id. This parameter is used in
SendBatch to define the template.

You can get the following ids by calling Configuration > GetSendParams function.

fromId: The id for from address. This from nam/from address pair must be created beforehand.

poolId: The id for from pool. This parameter must be created beforehand.