What is the best way to retrieve distinct / unique values using SPQuery?

前端 未结 6 939
孤独总比滥情好
孤独总比滥情好 2020-12-30 04:03

I have a list that looks like:

Movie          Year
-----          ----
Fight Club     1999
The Matrix     1999
Pulp Fiction   1994

Using CA

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-30 04:54

    After coming across post after post about how this was impossible, I've finally found a way. This has been tested in SharePoint Online. Here's a function that will get you all unique values for a column. It just requires you to pass in the list Id, View Id, internal list name, and a callback function.

    function getUniqueColumnValues(listid, viewid, column,  _callback){
    var uniqueVals = [];
    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_layouts/15/filter.aspx?ListId={" + listid + "}&FieldInternalName=" + column + "&ViewId={" + viewid + "}&FilterOnly=1&Filter=1",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" }
        }).then(function(response) {
            $(response).find('OPTION').each(function(a,b){
                if ($(b)[0].value) {
                    uniqueVals.push($(b)[0].value);
                }
            });
            _callback(true,uniqueVals);
    },function(){
        _callback(false,"Error retrieving unique column values");
    });
    

    }

提交回复
热议问题