I am trying to query list items using caml (first time doing this) and sort the items based on modified date so I can get access to most recent documents. However when trying to get the SPListItem based on the ID I am failing.
Code:
SPQuery query = new SPQuery();
String camlquery = "<OrderBy><FieldRef Name='Modified' Ascending='False' /></OrderBy>";
query.ViewAttributes = "Scope = 'Recursive'";
query.ViewFields = "<FieldRef Name='Modified' /><FieldRef Name='Title' /><FieldRef Name='Name' />";
query.Query = camlquery;
query.IncludeMandatoryColumns = true;
SPListItemCollection col = list.GetItems(query);
//failing here... I think
SPListItem item = col.GetItemById(0);
Is there a way to get an SPListItem based on its index from within the SPListItemCollection?
I have tried
SPListItem item = col[0];
Also with no luck.
Here is the error I am getting:
Value does not fall within the expected range. 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.ArgumentException: Value does not fall within the expected range.
col[0]
It usually means an empty array. – banana