How to filter out first record returned from a Repeater

Kim Tompkins asked on July 15, 2021 17:46

I have a repeater webpart that is using this WHERE condition to pull in all articles for a news page

ArticleCategory = 'News Article' AND ArticleRegion = 0

What I need to do is filter out the very first record as that one will be highlighted elsewhere on the page and I don't want to display it twice.

so basically I'm asking this. if I have 10 articles how can I only pull back the last 9 and omit the first one?

Recent Answers


Brenden Kehren answered on July 15, 2021 18:20

You could try using the below syntax in your Order BY value after you specify your Order By value.

ORDER BY NewsArticleDate DESC
-- enter after DESC from above
OFFSET (1) ROWS FETCH NEXT (9) ROWS ONLY

Technically speaking the SQL should run without issue, but you may have to "Trick" Kentico into running it. Your full query may look like so:

SELECT *
FROM View_CMS_Tree_Joined
    LEFT OUTER JOIN Content_Article on DocumentForeignKeyValue = ArticleID
WHERE ClassName = 'content.article' AND NodeAliasPath LIKE '/Articles/%'
ORDER BY ArticleDate DESC
OFFSET (1) ROWS FETCH NEXT (9) ROWS ONLY
0 votesVote for this answer Mark as a Correct answer

Kim Tompkins answered on July 15, 2021 19:08

I used this in my ORDER BY expression box in the repeater and received this error. Also, there will be an undetermined number of articles. I need to bring back all of them, not just 9 (I used that as an example)

DocumentPublishFrom DESC from above OFFSET (1) ROWS FETCH NEXT (9) ROWS ONLY

nvalid SQL query in property "orderby". Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Invalid SQL query in property "orderby".

Source Error:

Line 209: get Line 210: { Line 211: return DataHelper.GetNotEmpty(GetValue("OrderBy"), repItems.OrderBy); Line 212: } Line 213: set

Source File: C:\Sites\val.vitalant.org\server\CMS\CMSWebParts\Viewers\Documents\cmsrepeater.ascx.cs Line: 211

Stack Trace:

[InvalidOperationException: Invalid SQL query in property "orderby".] CMS.PortalEngine.Web.UI.CMSAbstractWebPart.GetValueInternal(String propertyName, Boolean resolveMacros) +1636 CMSWebParts_Viewers_Documents_cmsrepeater.get_OrderBy() in C:\Sites\val.vitalant.org\server\CMS\CMSWebParts\Viewers\Documents\cmsrepeater.ascx.cs:211 CMSWebParts_Viewers_Documents_cmsrepeater.SetupControl() in C:\Sites\val.vitalant.org\server\CMS\CMSWebParts\Viewers\Documents\cmsrepeater.ascx.cs:909 CMSWebParts_Viewers_Documents_cmsrepeater.OnContentLoaded() in C:\Sites\val.vitalant.org\server\CMS\CMSWebParts\Viewers\Documents\cmsrepeater.ascx.cs:880 CMS.PortalEngine.Web.UI.CMSWebPartZone.OnContentLoaded() +158 CMS.PortalEngine.Web.UI.CMSAbstractLayout.OnContentLoaded() +165 CMS.PortalEngine.Web.UI.CMSAbstractLayout.LoadWebPartsContent(Boolean reloadData) +305 CMS.PortalEngine.Web.UI.CMSAbstractLayout.LoadContent(PageInfo pageInfo, Boolean reloadData, Boolean allowOrphanedZones) +875 CMS.PortalEngine.Web.UI.CMSPagePlaceholder.LoadContent(PageInfo pageInfo, Boolean reloadData) +1936 CMS.PortalEngine.Web.UI.CMSPagePlaceholder.LoadContent(PageInfo pageInfo, Boolean reloadData) +2749 CMS.PortalEngine.Web.UI.CMSPortalManager.LoadPage(Boolean reloadData) +233 CMS.PortalEngine.Web.UI.CMSPortalManager.PageHelper_InitComplete(Object sender, EventArgs e) +29 CMS.Base.Web.UI.PageContext.CurrentPage_InitComplete(Object sender, EventArgs e) +277 System.EventHandler.Invoke(Object sender, EventArgs e) +0 System.Web.UI.Page.OnInitComplete(EventArgs e) +11849993 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +535

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on July 16, 2021 06:42

I guess you are hitting the SQL injection protection. You can also try a bit different WHERE condition, something like
ArticleCategory = 'News Article' AND ArticleRegion = 0 AND ArticleID NOT IN (SELECT TOP 1 ArticleID FROM ...)
So, you will add a sub-SQL query which selects the TOP 1 records. Similar issue was discussed here as well.

0 votesVote for this answer Mark as a Correct answer

Kim Tompkins answered on July 19, 2021 17:48

I was successful using ArticleCategory = 'News Article' AND ArticleRegion = 0 AND ArticleID NOT IN (SELECT TOP 1 ArticleID FROM ...)

However, the problem I see now is from the ORDER BY clause DocumentPublishFrom DESC. so basically I can filter the first record out with the code you provided me but that record isn't really the one I want to filter. I need to filter out the first record after it results have been ordered. I tried to do this in the Where clause but it fails because DocumentPublishFrom is not a field in the table.

0 votesVote for this answer Mark as a Correct answer

Kim Tompkins answered on July 19, 2021 22:58

I also tried adding OFFSET 1 ROWS to the ORDER BY CLAUSE. but I have tested it in a view and it works there.

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on July 20, 2021 06:25

Have you tried adding the DocumentPublishFrom column into the list of columns in the repeater's Columns property? If not listed, the repeater is not pulling out the data...
Have you considered using a custom code in a custom web part and use the API to get the data you want in the way you want? This is getting more and more complicated and I am afraid that the SQL query options will not be enough for you. Or, maybe using a repeater with custom SQL query where you will create the SQL query logic in which you will get all items, order them and then remove the first item (if this logic is possible through SQL...).

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.