On a pageLoad event I want to go to two different SharePoint Lists and get the first ListItem from one list and an the last ListItem in another. I don’t know any of the ListItem ID’s so I guess this needs to be done through the list index – just not sure how to implement this. Having got a handle on the List can someone advise (using c#) the best way to get the first and last items for the scenario outlined above?
On a pageLoad event I want to go to two different SharePoint Lists and
Share
The condition “first item” and “last item” is not very clear. Assuming you want to retrieve the first and the latest created item. Important to note is that retrieving items via the index of the
SPListItemCollectioncan be slow for large lists since accessingSPList.Itemsreturns all items of the list. That’s why I suggest using a CAML query:For the “last item” you have to reverse the ordering:
If you want to get the last and the first item based on another condition you just have to change the order by field.
Update:
Even better would be to order by theIDfield. With that you could achieve the same result.Not only better, the created field could have been changed through code as pointed out by @Kobi.