0

Here is my scenario - on the page, there will be a button, on the button click there will be a popup and the user will enter some detail. On submit button, I need to create a contact and save custom facets on xDB and after that, there will be a confirmation message on the same popup.

Because I do not want to post back the page, I am doing an ajax call, and Tracker will not be available on ServicesApiController. Is there any solution to this?

Code:


public class SubscriptionApiController : ServicesApiController
    {
        private readonly IContactRepository _contactRepository;

        public SubscriptionApiController()
        {
            _contactRepository = ServiceLocator.ServiceProvider.GetService<IContactRepository>(); 
        }

        [HttpPost]
        public HttpResponseMessage Post(SubscriptonModel model)
        {
            try
            {
               
            }
            catch (Exception ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, Constants.ErrorMessage.InternalServerError);
            }
        }
    }

Route

 public class WebApiServices
    {
        public void Process(PipelineArgs args)
        {
            GlobalConfiguration.Configure(Configure);
        }
        protected void Configure(HttpConfiguration configuration)
        {
            var routes = configuration.Routes;
            routes.MapHttpRoute("subscriptionApi", "api/{controller}/{action}", new { });
        }
    }

1 Answer 1

1

You can register a custom route to a regular sitecorecontroller using:

RouteTable.Routes.MapRoute( "yourcustomroute", "custom/path", new { controller = "yourcontroller", action = "youraction" } );

Then call that via an ajax call.

By doing it this way, it is like navigating to that page in the normal session and sitecore tracker is available.

This article covers the main points involved in setting this up:

https://www.mydatahack.com/how-to-configure-custom-goals-and-trigger-them-by-ajax-call-sitecore/

3
  • I have added my code, which is not working for me Commented Oct 29, 2022 at 2:26
  • I see you must have got it working. The key is to use normal controller, not apicontroller. Commented Oct 29, 2022 at 6:57
  • Yes after using SitecoreController, it is working Commented Oct 29, 2022 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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