1

I'm trying to write a custom filter display template. My template requires some external calls, and thus requires some async work.

How can I output my filters after the initial render? I have to read the data from list and put those data as the filter value on the filter display template. I'm trying to create a custom refiner, that receive filters from the query results. I am adding the below code in the filter display template.

var context = new SP.ClientContext();
                        var list = context.get_web().get_lists().getByTitle('ListName');
                        var camlQuery = new SP.CamlQuery();
                        camlQuery.set_viewXml('<View><RowLimit>100</RowLimit></View>');
                        returnedItems = list.getItems(camlQuery);

                        context.load(returnedItems,'Include(Title, SiteName)');
                        context.executeQueryAsync(onSucceededCallback, onFailedCallback);


                            function onSucceededCallback(sender, args) {
                            var listItemInfo = '';

                    var listItemEnumerator = returnedItems.getEnumerator();

                    while (listItemEnumerator.moveNext()) {

                        var oListItem = listItemEnumerator.get_current();
                        var urlTitle =  oListItem.get_item('Title') ;
                            var siteName = oListItem.get_item('SiteName');

                            map[siteName] = {
                        "RefinerName": "SPSiteURL",
                        "RefinementValue": urlTitle
                    };

                            }
                    var listData = ctx.ListData;
        var hasControl = true;
        var shortListSize = 5;
                    if ($isNull(ctx.RefinementControl) || $isNull(ctx.ClientControl)) hasControl = false;
                    if (ctx.RefinementControl.propertyName == "SPSiteURL") {
                            var listData = ctx.ListData;
                            shortListSize = 6;
                             for (var i in listData) {
                            listData[i].RefinementTokens = [listData[i].RefinementToken];
                                listData[i].RefinementName = mapSiteUrl(listData[i].RefinementName);
                                listData[i].RefinementTokenWrappedValues = [Srch.RefinementUtil.stringValueToEqualsToken(listData[i].RefinementValue)];

                            }
                    }
                            }
                            function onFailedCallback(sender, args) {
                            alert("error");
                            }
                            function mapSiteUrl(refinementName) {



                    var mappedRefinementName = null;

                    for (var key in map) {
                    if(map[key].RefinerName == "SPSiteURL"){
                        if (refinementName.toLowerCase() === map[key].RefinementValue.toLowerCase()) {
                            mappedRefinementName = key;
                            break;
                        }
                    }
                    }
                    if ($isNull(mappedRefinementName)) {
                        mappedRefinementName = refinementName;
                    }
                    return mappedRefinementName;
                }

I've tried duplicating the outputFilter method from the default template. Then I call it in a PostRender callback. But this does not work.

for (var i = 0; i < listData.length; i++){
  var filter = listData[i];
  if(!$isNull(filter)){
    filter.RefinementTokens = [filter.RefinementToken];
    filter.RefinementTokenWrappedValues = [Srch.RefinementUtil.stringValueToEqualsToken(filter.RefinementValue)];

    EnsureScriptFunc("sp.js", false, function(){


            var context = SP.ClientContext.get_current();         

            // load some data

            context.executeQueryAsync(Function.createDelegate(this, function(){}
                outputFilter(filterData.Name, filter.RefinementCount, refiners, addMethod, "ms-displayBlock", true);

            ),Function.createDelegate(this, function(){}

            )); 



            });
        });
    });

  }
}
  • You may want to post your code so other can help you determine the problem. – Matthew McDermott Sep 17 '18 at 22:59
  • Updated my question with the code. – Utkarsh Sep 18 '18 at 6:46

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.