Question

I want to bulk load a few items from a list. The Caml is something like this:

<View><Query><Where>
   <Gt><FieldRef Name=\'ID\'/><Value Type=\'Number\'>{id}</Value></Gt>
</Where></Query><RowLimit>5000</RowLimit></View>

where {id} is the id from the last element of the previous query

This works fine on my testdata. But can I be sure that the ids from that query are always incremental? In other words: Is the default order always by id?

Or do I have to add an <OrderBy>-statement?

The reason for asking is that an orderby might affect the performance which I do not want.

Was it helpful?

Solution

If you don't specify an order, there is an implied order based on ID since in SharePoint IDs increase as rows are added and there is always an index on ID even if there are no other indexes on the list. However, implied is not guaranteed. The CAML specification doesn't list a default field for sorting if none is specified.

If the order is required by the use case, add ORDERBY. You would have to test to verify but I doubt there would be any performance hit since, as you see by your test data so far, the data is generally already sorted in ID order.

OTHER TIPS

Yes as default a list is sorted by ID, so if that's what you don't have to include <OrderBy>

In your query, you are using view tag. Are you querying based on a specific view? If not, then better remove it, otherwise make sure you specify which view. It is best that you use orderby as the query could change, probably the query is based on the default view. Try to change the orderby in the default view of your list/library and see what happens when you query the data.

Use Caml Query Builder for quick tests.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top