If you’re working with an SPListItemCollection, you might have the need to sort the data that stored in the collection. The best way I’ve found to do this is to build an SPQuery object and use that to actually query for the information. Using an object of this type makes it possible to send in whatever sort and/or orderby clause we’d like to use.
<OrderBy><FieldRef Name='EventDate' Ascending='FALSE'></FieldRef></OrderBy>
The full query would look something like this:
SPQuery oQuery = new SPQuery(); oQuery.Query = "<Where><Eq><FieldRef Name='AP_x0020__x002f__x0020_O'/>" + "<Value Type='Text'>" + fruitName + "</Value></Eq></Where>" + "<OrderBy><FieldRef Name='EventDate' Ascending='FALSE'></FieldRef></OrderBy>";
If you receive an error similar to:
One or more field types are not installed properly. Go to the list settings page to delete these fields.
Then you’ve set the FieldRef Name incorrectly. The trick to resolving this is:
- Navigate to your list that the column/field is contained within
- Click the New button as you normally would to create a new item in this list
- Click on View, Source from the toolbar in your browser window.
- Finally, do a find on the phrase fieldinternalname and locate the field you’re trying to query on
- Whatever value is stored in fieldinternalname is what you’ll want to use in your query
Any questions, let me know.
UPDATE: I recently discovered another trick to this. If you want to avoid having to seek out what the internal name of a particular field is, when you first name your column, do not include any spaces or special characters. Once the field (column) has been created, go back and rename the field to include the spaces or special characters as desired. SharePoint will still retain the original field name without spaces and you can use that directly in your query without issue.