public object GetData(int id, int page, string sortField, bool sortDescending, string searchQuery = null) { // NOTE: this is fine for now, but eventually make it should probably be configurable const int PerPage = 10; var document = ContentHelper.GetById(id); if (document == null) { return(null); } var model = ContentHelper.GetFormModel(document); if (model == null) { return(null); } var preValues = ContentHelper.GetPreValues(document, FormModel.PropertyEditorAlias); var allFields = GetAllFieldsForDisplay(model, document, preValues); var statisticsEnabled = ContentHelper.StatisticsEnabled(preValues); var approvalEnabled = ContentHelper.ApprovalEnabled(preValues); var index = IndexHelper.GetIndex(id); var fullTextIndex = index as IFullTextIndex; var result = (fullTextIndex != null && string.IsNullOrWhiteSpace(searchQuery) == false ? fullTextIndex.Search(searchQuery, allFields.Select(f => f.FormSafeName).ToArray(), sortField, sortDescending, PerPage, (page - 1) * PerPage) : index.Get(searchQuery, null, sortField, sortDescending, PerPage, (page - 1) * PerPage) ) ?? Result.Empty(sortField, sortDescending); var totalPages = (int)Math.Ceiling((double)result.TotalRows / PerPage); // out of bounds request - e.g. right after removing some rows? if (page > totalPages && totalPages > 0) { // repeat the query but get the last page page = totalPages; result = index.Get(searchQuery, null, sortField, sortDescending, PerPage, (page - 1) * PerPage); } var rows = model.ExtractSubmittedValues(result, allFields, (field, value, row) => field.FormatValueForDataView(value, document, row.Id)); return(new { fields = allFields.Select(f => new { name = f.Name, sortName = f.FormSafeName }).ToArray(), rows = rows.Select(r => new { _id = r.Id, _createdDate = r.CreatedDate, _approval = r.ApprovalState.ToString().ToLowerInvariant(), values = r.Fields.Select(f => f.Value) }).ToArray(), currentPage = page, totalPages = totalPages, sortField = result.SortField, sortDescending = result.SortDescending, supportsSearch = fullTextIndex != null, supportsStatistics = statisticsEnabled && index is IStatisticsIndex && allFields.StatisticsFields().Any(), supportsApproval = approvalEnabled && index is IApprovalIndex }); }