Tuesday, April 24, 2018

S1E13: The Easy Way to code with Nasty SharePoint Search restAPI JSON

S1E13: Easy Way to code with Nasty SharePoint Search restAPI JSON



The JSON returned by the SharePoint search restAPI is just down right nasty. Because it is nasty, it will make your code nasty and unscalable.



BUT!!! With the right "data method" to encapsulate this nastiness, our existing pattern will still hold true, and we will get the power of SharePoint search...which is very powerful and widely used/needed in SharePoint Online.



Lucky for you, I've endured all of the pain for you and these 12 lines of code will save your life (i.e. heartache, head banging, cliff jumping)



   function _getSPRelevantResults(endpoint) {

            return $http.get(endpoint, _getConfig).then(function (d) {               

                var results = d.data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;

                var col = [];

                results.forEach(function (rItem) {

                    var item = {};

                    rItem.Cells.results.forEach(function (cell) {

                        item[cell.Key] = cell.Value;

                    });               

                    col.push(item);

                });           

                return col;

            });

        }





#spEasyDev #sp2013 #sp2016 #ngsharepoint #custommasterpage #sharepoint2013 #sharepoint2016 #sharepoint #sharepointandangularjs #odata #sharepointlargelists





No comments:

Post a Comment