Apr/29
2010

Sharepoint Webpart CAML and more than 160 clauses

I got an outsourced project handed over that couldn't be finished in time. It was quite a simple request, one treeview and a listbox where the selected items where showed, the data source should a few joined sharepoint lists. The webpart was finished besides the problem that you only could select 160 items in the treeview, if more you got an "render failed" message from the list beneath.
First i had to figure out what exactly was causing the problem and that without the source. I traced it down to the CAML query where the webservice should get it's data from the filtered lists. Today i got the source and it didn't take long i found exactly that problem

Query completequery = new Query()
{
Where = new Where(GetWhereElement(node, field, BeginsWith)), // custom method
OrderBy = new OrderBy(new FieldRef(field.InternalName)) // custom method
};
SPQuery query = new SPQuery(!string.IsNullOrEmpty(ViewID) ? list.Views[new Guid(ViewID)] : list.DefaultView);
query.Query = completequery.ToString();
return list.RenderAsHtml(query);

Indeed it was clear that the query failed if the statement in it had more then 160 interlaced ORs in it. The SQL-Server says "no thank's" at this point, what is definitly legitimate. So...to work arround that problem i first had to figure out that CAML is really really "special" and not really smart. After a few hours of headaches i switched over to do the stuff manually and build the list by hand.