In one of my webparts, I have a ListViewByQuery displaying some elements (or all) of a specified list. The SPQuery is built dynamically depending on the search fields.
Here is how I build the SPQuery :
var list = SPContext.Current.Web.Lists["gpProjects"];
SPQuery query = new SPQuery(list.DefaultView);
query.ViewFields = "<FieldRef Name=\"Title\" /><FieldRef Name=\"departments\" />";
query.Query = "<OrderBy><FieldRef Name='Title' Ascending='true' /></OrderBy>";
listView.List = list;
listView.Query = query;
It works well, but now I need to use this query to get the items from the SPList:
var elements = list.GetItems(listView.Query).GetDataTable();
But it doesn't work. I got the following error : "Cannot complete this action. Please try again. "
Very helpful.
This doesn't only arises when there are no conditions on the query.
Does someone have an idea on how to make that working ?
I read somewhere that I have to impersonate myself in the webpart, but it didn't worked.