Search code examples
c#sharepointsharepoint-2010

What is the best way to reorder SharePoint List items?


I'm currently working on a Web Part that renders specific elements which properties are taken from a SharePoint List.

I'm planning to add a Field in SP List that will allow the SharePoint user to enter a number to specify the Order in which the elements will be rendered.

Question: What is the best way to implement this (keeping in mind that user may enter the same value twice or omit a number in sequence without knowing it)?

Example: we have elements and their Order value: A 1, B 2, C 3, D 4, E 5.

If a user changes the Order value to: A 1, B 4, C 3, D 4, E 5, the elements should be rendered in order: A, C, B, D, E.

And if a user changes the Order value to: A 1, B 7, C 3, D 4, E 5, the elements should be rendered in order: A, C, D, E, B.


Solution

  • I would recommend you to use SPQuery, it is also supposed to be more efficient.

    When you use SPQuery you can specify OrderBy as you wish.

    Here is an example of using it: http://msdn.microsoft.com/en-us/library/ms457534(v=office.14).aspx