0

I've to develop a custom field type with auto complete functionality. If content author start typing in this field then it will show autocomplete data from external database. On focus out of this field, I want to populate some other fields from external database. Can we do this with Custom field types? I looked into this article but didn't find any logic for focus out and autocomplete. Please guide me how can I achieve this. I am using sitecore 8.2 Update 6.

  • The documentation url what you linked is related to WFFM not Content Editor. – Tamás Tárnok Feb 5 '18 at 7:14
2

I think the most flexible solution is to use the iframe field type, because it allows you to easily implement your own frontend and backend implementation. In the past I did something like this, but here is the main idea how you can achieve this.

In Sitecore you need to use the iframe field type: enter image description here To make this path work I need to map this route:

using Sitecore.Pipelines;
using System.Web.Mvc;
using System.Web.Routing;

namespace MyProject.Pipelines
{
    public class RegisterRoutes
    {
        public void Process(PipelineArgs args)
        {
            RouteTable.Routes.MapRoute("SimpleField", "myFields/SimpleField/Render", new { controller = "SimpleField", action = "Render" });
        }
    }
}

You can create your own Controller and View:

using Sitecore.Configuration;
using Sitecore.Data;
using System.Web.Mvc;

namespace MyProject.Controllers
{
    public class SimpleFieldController : Controller
    {
        public ActionResult Render()
        {
            return View("~/Views/SimpleField/Render.cshtml");
        }
    }
}

View:

<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,300italic,400italic,600italic,700italic,300,600,700,800" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="/sitecore/shell/themes/standard/default/Default.css" />
<link rel="stylesheet" type="text/css" href="/sitecore/shell/themes/standard/default/Content Manager.css" />
<style>
    #ll-fields {
        padding: 10px 10px 10px 10px;
    }
</style>
<div id="ll-fields" class="scEditorSections">
    <div class="scEditorFieldMarker">
        <div class="scEditorFieldLabel">Text:</div>
        <div>
            <input type="text" id="ll-text" class="scContentControl scCombobox" />
        </div>
    </div>
</div>

<script type="text/javascript" src="/scripts/field/field-base.js"></script>
<script type="text/javascript" src="/scripts/field/text-field.js"></script>
<script>
    textField.getValue();

    document.getElementById('ll-text').onchange = function () {
        textField.setValue();
    };

    textField.updateIframeDisplaySettings();
</script>

As you can see in the View it contains 2 important JS references, they are doing the dirty job:

field-base.js

function fieldBase () {}

fieldBase.getParentIframe = function () {
    var iframes = window.parent.document.getElementsByTagName('iframe');
    var parent;
    Array.prototype.forEach.call(iframes, function (el, i) {
        if (fieldBase.getIframeDocument(el) === window.document) {
            parent = el;
            return;
        }
    });
    return parent;
};

fieldBase.getIframeDocument = function (iframe) {
    return iframe.contentDocument || iframe.contentWindow.document;
};

text-field.js

function textField() { }

textField.setValue = function () {
    fieldBase.getParentIframe().setAttribute('sc_value', document.getElementById('ll-text').value);
}

textField.getValue = function () {
    document.getElementById('ll-text').value = fieldBase.getParentIframe().getAttribute('sc_value');
}

textField.updateIframeDisplaySettings = function () {
    var parentIframe = fieldBase.getParentIframe();
    parentIframe.style = "height: 87px";
    parentIframe.scrolling = "no";
}

Instead of the text-field.js you can use your own logic for the autocomplete and in the Controller Action you can retrieve the data from your external database.

| improve this answer | |

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