0

Is there any way that I can update the response of a controller action by pipeline process?

In httpRequestEnd or httpRequestBegin for example

P.S: I'm using Sitecore 9 update 1 included EXM. I created 2 facets fields to the contact. And now, in the EXM - Email Campaign detail page, I would like to show these facets fields value into the Recipient activity tab. I see that the data for this tab came from the response of Sitecore.EmailCampaign.Server.Controllers.DataSource.OpensClicksController (/sitecore/api/ssc/EXM/OpensClicks) . So I think if I can catch the response of this request, edit it by adding facets fields value

Many thanks.

2
  • Can you please provide more information on what you want to achieve? Jun 26 '20 at 8:53
  • I'm using Sitecore 9 update 1 included EXM. I created 2 facets fields to the contact. And now, in the EXM - Email Campaign detail page, I would like to show these facets fields value into the Recipient activity tab. I see that the data for this tab came from the response of Sitecore.EmailCampaign.Server.Controllers.DataSource.OpensClicksController (/sitecore/api/ssc/EXM/OpensClicks) . So I think If I can catch the response of this request, edit it by adding facets fields value
    – Tai Vo
    Jun 26 '20 at 9:02
0

For who needs the answer. We can abort the default request and return a custom response by something like code below. My code in this case is abort the default EXM's OpensClicks request and return a custom result that includes some contact facets fields

Create a new pipeline process and patch it after Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel

The process class should be something like that

public class ApiRedirectProcessor : HttpRequestProcessor
    {
        public override void Process(HttpRequestArgs args)
        {
                if (args.RequestUrl.AbsolutePath.Contains("/sitecore/api/ssc/EXM/OpensClicks"))
                {
                    try
                    {
                        args.AbortPipeline();
                        var messageid = args.HttpContext.Request.Params["messageId"];
                        var recepientList = GetRecipientList(messageid);
                        if (recepientList != null && recepientList.Any())
                        {
                            var managerRootId = args.HttpContext.Request.Params["managerRootId"];
                            var pageIndex = int.Parse(args.HttpContext.Request.Params["pageIndex"]);
                            var pageSize = int.Parse(args.HttpContext.Request.Params["pageSize"]);
                            var openClickStatisticsService = ServiceLocator.ServiceProvider.GetService<OpenClickStatisticsService>();
                            MessageDataSourceContext data = new MessageDataSourceContext()
                            {
                                MessageId = messageid,
                                ManagerRootId = managerRootId,
                                PageIndex = pageIndex,
                                PageSize = pageSize
                            };

                            Task<RecipientActivityResponse> task = System.Threading.Tasks.Task.Run<RecipientActivityResponse>(async () => await openClickStatisticsService.GetResponse(data));
                            CustomRecipientActivityResponse customResponse = new CustomRecipientActivityResponse(task.Result);
                            if (customResponse.Items.Any())
                            {
                                foreach (var customRecipientActivity in customResponse.Items)
                                {
                                    var contactInfo = recepientList.FirstOrDefault(x => x.Personal()?.FirstName == customRecipientActivity.FirstName
                                                        && x.Personal()?.LastName == customRecipientActivity.LastName
                                                        && x.Emails()?.PreferredEmail?.SmtpAddress == customRecipientActivity.Email);
                                    if (contactInfo != null)
                                    {
                                        customRecipientActivity.CustomCompany = contactInfo.GetCustom()?.Company;
                                        customRecipientActivity.CustomContactOwner = contactInfo.GetCustom()?.ContactOwner;
                                    }
                                }
                            }

                            args.HttpContext.Response.ContentType = "application/json; charset=utf-8";
                            args.HttpContext.Response.Output.WriteLine(JsonConvert.SerializeObject(customResponse));
                            args.HttpContext.Response.End();
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }                    
                }
       }
}

public class CustomRecipientActivityResponse : RecipientActivityResponse
    {
        [JsonProperty("items")]
        public List<CustomRecipientActivity> Items { get; set; }
        public CustomRecipientActivityResponse(RecipientActivityResponse recipientActivityResponse)
        {
            if (recipientActivityResponse != null)
            {
                this.TotalCount = recipientActivityResponse.TotalCount;
                List<CustomRecipientActivity> customRecipientActivityList = new List<CustomRecipientActivity>();

                if (recipientActivityResponse.Data.Any())
                {
                    foreach (OpenClick item in recipientActivityResponse.Data)
                    {
                        customRecipientActivityList.Add(new CustomRecipientActivity(item));
                    }
                }

                Items = customRecipientActivityList;
            }
        }
    }

    public class CustomRecipientActivity : OpenClick
    {
        public CustomRecipientActivity(OpenClick recipientActivityModel)
        {
            if (recipientActivityModel != null)
            {
                this.FirstName = recipientActivityModel.FirstName;
                this.LastName = recipientActivityModel.LastName;
                this.Recency = recipientActivityModel.Recency;
                this.Email = recipientActivityModel.Email;
                this.Date = recipientActivityModel.Date;
                this.DateFormatted = recipientActivityModel.DateFormatted;
                this.Name = recipientActivityModel.Name;
                this.Url = recipientActivityModel.Url;
            }
        }

        [JsonProperty("custom_company")]
        public string CustomCompany { get; set; }

        [JsonProperty("custom_contactowner")]
        public string CustomContactOwner { get; set; }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.