How to automatically sort SPListItems based on a field
I have a requirement that the SPList be sorted by the Priority field (numeric field, no limit) when adding or updating the ListItem.
The variety should work like this:
Original Inserted item Modified
1 1
2 2 2
3 3 (old 2)
4 (old 3)
Edited: This behavior is essential to improve the user experience when editing data in a data sheet view. This is the kind of bad AJAX update I'm trying to get.
The only solution I see now is to use an event handler and update the ListItems that should be out of memory, however this can be quite an expensive operation on a list with hundreds of items (if there are no spaces in the sequence).
Am I missing the obvious solution, or did I scale much better?
TIA
a source to share
Wow, tough. I don't like the idea of ββgoing back and doing updates with existing items from the EventReceiver. You are asking for trouble.
Here's my recommendation.
Use 2 boxes for your view.
1 your priorty field as you have today (so someone says 1, 2, 3, 4) ... 2 create a date field (this is the built-in datetime field).
so that your results look something like this.
1 1/1/2009
2 16.05.2009
2 5/11/2009
3 5/12/2009
a source to share
If you want the items to be sorted in the view: Set the sort order in the view.
If you need items sorted using the SharePoint API: Use SPQuery with the OrderBy element. http://sharepointmagazine.net/technical/development/writing-caml-queries-for-retrieving-list-items-from-a-sharepoint-list
a source to share