2

My question is regarding retrieving a value from a sharepoint list. Basically I have a list with some data. One of the fields in the list is a text field with numbers.

So what i want to do, i want to retrieve the highest number from that field ... how can i do that most practical and with best approach?

This will be done programmatically.

Of course i couuld do a loop through all the items, but i would like to avoid looping, is there a better way?

Thanks in advance.

2

Programatically use SPQuery and in your caml query use the OrderBy Node

eg:

<Query><OrderBy><FieldRef Name="yourcolumn" Ascending="True" /></OrderBy></Query>

You can also set the RowLimit property of SPQuery to 1 to make it more efficient

Hope this helps

UPDATE: As Nadeem Yousuf mentioned on another answer, this will not work with a text field, so might need to consider changing the type of this column (if possible?)

| improve this answer | |
  • nah, can't change it directly ... so no any other practical approaches for it? – Shkipper Dec 19 '12 at 13:29
  • did you know that if the data are all number you could change the type without losing any data?. Unfortunately I am not aware of another way to do it without looping though all the items. Wonder if you can use this guys camlex.codeplex.com and casting to an int in the lambda query – Renzo Dec 19 '12 at 13:35
0

Had the field in question been a Number field, I would have done Order By desc on that and put the RowLimit of SPQuery to 1. But I am not sure whether Ordery By clause will work here as yours is a text field containing numbers.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.