0

I am using below listed JSLink code to perform cascading and its a working version except one issue. The Drop-down shows duplicate values because of how things are populated in the reference list, can someone please help me with the JSLink code to remove duplicates and show only unique values that I can integrate with the below code..

<script type="text/javascript">


    var FilterLookupHandler = (function () {
        var handler = function (fieldInternalName, queryField) {
            this.internalName = fieldInternalName;
            this.showField = queryField;
            this.suggestions = [];
            this.listId = "";
            this.currentValue;
        }
        handler.prototype.render = function (rCtx, field) {
            var self = this;
            self.renderContext = rCtx;
            if (rCtx != null && rCtx.CurrentItem != null) {
                var stringValue = rCtx.CurrentItem[rCtx.CurrentFieldSchema.Name];
                //self.currentValue = new SP.FieldLookupValue();
                //if (stringValue) {
                //    self.currentValue.set_lookupId(stringValue.split(";#")[0])
                //    stringValue = stringValue.split(";#")[1]
                //}
                self.listId = rCtx.CurrentFieldSchema.LookupListId;
            }
            if (rCtx == null || rCtx.CurrentFieldValue == null)
                return '';
            var formCtx = SPClientTemplates.Utility.GetFormContextForCurrentField(rCtx);
            if (formCtx == null || formCtx.fieldSchema == null)
                return '';
            self.formCtx = formCtx;
            self.registerCallbacks();
            var htmlString = "";
            htmlString += "<select id='" + self.internalName + "_select'></select>";

            SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
                execCallFoItems(self);
            });
            return htmlString;
        }

        function execCallFoItems(handler) {
            handler.callForItems("Brick Church Plaza", "Eq");
        }
        handler.prototype.callForItems = function (filterValue, tag) {
            var self = this;
            var cctx = new SP.ClientContext("https://**.sharepoint.com");
            var list = cctx.get_web().get_lists().getById(new SP.Guid(self.listId));
            var query = new SP.CamlQuery();
            query.set_viewXml('<View><Query><Where><' + tag + '><FieldRef Name=\'' + self.showField + '\'/>' +
                '<Value Type=\'Text\'>' + filterValue + '</Value></Geq></' + tag + '></Query><RowLimit>100</RowLimit></View>');
            self.suggestedItems = list.getItems(query);
            cctx.load(self.suggestedItems)
            cctx.executeQueryAsync(Function.createDelegate(self, self.suggestionsLoaded, Function.createDelegate(self, self.callFailed)))
        }
        handler.prototype.suggestionsLoaded = function (sender, args) {
            var self = this;
            var itemsEnumerator = self.suggestedItems.getEnumerator();
            self.suggestions = [];
            while (itemsEnumerator.moveNext()) {
                var item = itemsEnumerator.get_current();
                self.suggestions.push({
                    id: item.get_item("ID"),
                    value: item.get_item(self.internalName)
                })
            }
            self.updateSuggestions();
        }
        handler.prototype.updateSuggestions = function () {
            var self = this;
            var selectElement = document.getElementById(self.internalName + "_select");
            var optionsHtml = "";
            var liHtml = "";
            for (var i = 0; i < self.suggestions.length; i++) {
                var suggestion = self.suggestions[i];
                optionsHtml += "<option value='" + suggestion.id + "'>" + suggestion.value + "</option>";
                liHtml += "<li>" + suggestion.value + "</li>"
            }

            selectElement.innerHTML = optionsHtml;
            selectElement.onchange();
        }
        handler.prototype.callFailed = function (sender, args) {
            alert(args.get_message())
        }
        handler.prototype.registerCallbacks = function () {
            var self = this;
            self.formCtx.registerGetValueCallback(self.formCtx.fieldName, function () {
                return self.getCurrentValue();
            });
        }
        handler.prototype.getCurrentValue = function () {
            var selectedValue = document.getElementById(this.internalName + "_select").value;
            //this.currentValue.set_lookupId(selectedValue);
            //return this.currentValue;
            return selectedValue;
        }
        return handler;
    })();  


    (function () {
        if (typeof SPClientTemplates === 'undefined')
            return;
        var officeFieldHandler = new FilterLookupHandler("Company","Title");
        var OfficeFieldContext = {};
        OfficeFieldContext.Templates = {};
        OfficeFieldContext.Templates.Fields = {
            'Company': {
                'EditForm': Function.createDelegate(officeFieldHandler, officeFieldHandler.render),
                'NewForm': Function.createDelegate(officeFieldHandler, officeFieldHandler.render)
            }
        };
        SPClientTemplates.TemplateManager.RegisterTemplateOverrides(OfficeFieldContext);
    })();  
</script>

Thanks in advance.

  • 1
    A better solution is to require your lookup list to require unique values via the list settings. Enforce data integrity there instead of further downstream. – Eric Alexander Jul 30 '18 at 19:15
  • Unique values have already been enforced and it did not help at all. – mdevm Jul 30 '18 at 19:20
  • If it is turned on after the fact, you have to delete the duplicates. It isn't retroactive. Go back to your source list, pick an item, and try to add another item with the same value. It should fail. If it does not, then there is a bug and should be reported through the Office 365 support channels. – Eric Alexander Jul 30 '18 at 19:25
  • That is a business requirement, I can not remove duplicates because with those there are some unique content associated/populated in other column fields and which is exactly why i am looking for a coding solution, thanks. – mdevm Jul 30 '18 at 19:28

Your Answer

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

Browse other questions tagged or ask your own question.