0

I want to get the VisitorIdentification html string in my C# code. So I do the following:

var viewContext = Sitecore.Mvc.Common.ContextService.Get().GetCurrent<ViewContext>();
var htmlHelper = new HtmlHelper(viewContext, new Sitecore.Mvc.Presentation.ViewDataContainer(viewContext.ViewData));
var sitecoreHelper = htmlHelper.Sitecore();
var result = sitecoreHelper.VisitorIdentification().ToHtmlString();

But the result is just an empty string. Of course, the string should be something like this:

<link href='/layouts/System/VisitorIdentification.aspx' rel='stylesheet' type='text/css' />

How do I get this result?

1 Answer 1

4

You are definitely moving into unsupported waters here. The output of VisitorIdentification is dependent on a lot of different facors; not the least of which is Robots Detection.

As far as I can tell; just asking for a rendered output of the control is not going to work, unless prior steps have happened to activate the Tracker.

I encourage you to share the actual business case you're working to solve here, and open up for a better answer to your question. Until then, this is how you render the above. Use at your own risk.

untested code

using (Sitecore.Web.UI.HtmlControls.Page page = new Sitecore.Web.UI.HtmlControls.Page())
{
      System.Web.UI.Control identificationControl = page.LoadControl("~/layouts/system/VisitorIdentification.ascx");

      if (identificationControl != null)
      {
        HtmlForm htmlForm = new HtmlForm();
        page.Controls.Add((System.Web.UI.Control) htmlForm);
        htmlForm.Controls.Add(identificationControl);
        return new HtmlString(HtmlUtil.RenderControl(identificationControl));
      }
}

But you might as well just do the following:

output.Write("<link href=\"/layouts/System/VisitorIdentification.aspx\" rel=\"stylesheet\" type=\"text/css\" />");

As nothing in the above; is actually guaranteed to reflect any changes in logic from Sitecore's end. Nor are any cookies written - something the normal VisitorIdentification control would do - and this cannot be captured in a HtmlString.

3
  • 1
    Thank you for your answer, @Mark Cassidy! The reason why I want to do this is because I want to pass this string to a viewmodel which will be rendered by the frontend (handlebars). I don't know which other way I could take. I will test your code when I'm back in office, thank you very much!
    – user103
    Commented May 11, 2017 at 14:12
  • 1
    I don't think it's going to truly work. But are you allowing Handlebars to control your entire page output? normally, the <head> section would come from some form of server rendering.
    – Mark Cassidy
    Commented May 11, 2017 at 14:33
  • 1
    You're right, this hack is no good idea. I'll modify the handlebars setup and will do the VisitorIdentification rendering as proposed, with server rendering. Thank you for your input!
    – user103
    Commented May 16, 2017 at 14:12

Your Answer

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