SPQuery calculated fields filter does not work in SharePoint 2007/2010 - sharepoint

I have a calculated field in my list and I am trying to use filter on this field. For some reason, the following query always returns all items instead of a filtered item collection:
var spQuery = new SPQuery
{
Query = #"<Where><Geq><FieldRef Name='Score' /><Value Type='Calculated'>10000</Value></Geq></Where><OrderBy><FieldRef Name='Modified' Ascending='True' /></OrderBy>",
RowLimit = 200,
ViewFields = #"<FieldRef Name='Username' />"
};
var spList = web.Lists["Users"];
var spListItemCollection = spList.GetItems(spQuery);

try using
spQuery.ViewFieldsOnly = true;

Remove the query element from your SPQuery. See syntax here

Try removing ViewFields section (to get all columns, also those that are needed for calculation) or set SPQUery.IncludeMandatoryColumns

Related

ListViewByQuery doesn't show correct columns when GroupBy specified

I am using Sharepoint 2010, and wrote an application page in C#/VS2010. I am using ListViewByQuery to display a list. All is well, until I add the GroupBy tag to group the list. When this is added, for some reason the columns I've chosen with query.ViewFields are ignored, and instead three other columns are displayed, both when collapsed and when expanded. Code is here:
//this section demonstrates how to display a list in an SP Application Page project:
using (SPWeb web = site.OpenWeb())
{
myListQuery.List = web.Lists["Links"];
SPQuery query = new SPQuery(myListQuery.List.DefaultView);
//note: there seems to be bug somewhere... when the list is grouped by (folders), you
// don't see the fields you request - just three basic fields. It seems to ignore the ViewFields
// that you specified, unless you don't group. Weird.
query.ViewFields = "<FieldRef Name=\"ID\" /><FieldRef Name=\"URLwMenu\" /><FieldRef Name=\"List\" /><FieldRef Name=\"Category\" /><FieldRef Name=\"Author\" />";
query.Query = "<Where><Contains><FieldRef Name=\"List\"/><Value Type=\"Text\">Projects</Value></Contains></Where>";
//if this next line is commented out, all the correct columns are shown
query.Query += "<GroupBy Collapse=\"FALSE\" GroupLimit=\"100\"><FieldRef Name=\"Category\"></GroupBy>";
myListQuery.DisableFilter = false;
myListQuery.DisableSort = false;
myListQuery.Query = query;
}
The columns that are shown, in this example, are Type, Edit, URL, and Notes. If anyone has any clues, it would be much appreciated!
You appear to be missing the / in the closing tag of the FieldRef in the GroupBy clause:
<FieldRef Name=\"Category\">

how to use content iterator in sharepoit 2010

there are 7000 items in my list. i need to filter the list and retrieve result
i am using the following code in my webpart.
string query = "<Where><BeginsWith><FieldRef Name='Project' /><Value Type='Text'>ab</Value></BeginsWith></Where>"
SPQuery spquery = new SPQuery();
spquery.Query = query;
ContentIterator iterator = new ContentIterator();
iterator.ProcessListItems(list, spquery, ProcessListItem, ProcessListItemError)
as i am using ContentIterator still it is giving me the error "The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator"
Update:
string query2 = #"<Where><Eq><FieldRef Name='Project' /><Value Type='Text'>11759</Value></Eq></Where>";
SPQuery spquery = new SPQuery();
spquery.RowLimit = 10;
spquery.Query = query2;
spquery.QueryThrottleMode = SPQueryThrottleOption.Override;
ContentIterator iterator = new ContentIterator();
iterator.ProcessListItems(list, spquery, ProcessListItem, ProcessListItemError);
In every case weather I used SPCollectionItem or Iterator. when ever I am passing the where condition in spquery. same error comes.
To efficiently use ContentIterator and avoid throttle exception, you should explicitly include OrderBy clauses.
Try to use ContentIterator.ItemEnumerationOrderByNVPField which actually enables the index to be used.
For more details,check this out
http://extreme-sharepoint.com/2012/07/17/data-access-via-caml-queries/
Likely because the the Field you are comparing on is not indexed: http://msdn.microsoft.com/en-us/library/ff798465
If you were to build a query that returns the first 100 items sorted
by the ID field, the query would execute without issue because the ID
column is always indexed. However, if you were to build a query that
returns the first 100 items sorted by a non-indexed Title field, the
query would have to scan all 10,000 rows in the content database in
order to determine the sort order by title before returning the first
100 items. Because of this, the query would be throttled, and rightly
so—this is a resource-intensive operation.
You either have to ...
... set a RowLimit below the configured threshold on your query.
... or define a query that returns less items.
... or change the threshold of the list.
have you seen this:
msdn link
hmmz, just tried this and it still gave an error. If I use this however I can iterate over way more than 7k listitems:
private int GetItems(SPList list){
var query = new SPQuery();
query.Query = "";
query.QueryThrottleMode = SPQueryThrottleOption.Override;
var items = list.GetItems(query);
return items.Count;
}
so my advice is to just use the list.getitems
You just need to change your query.
//Keep your query inside <View></View> tag.
string query = "<View><Where><BeginsWith><FieldRef Name='Project' /><Value Type='Text'>ab</Value></BeginsWith></Where><View>"
SPQuery spquery = new SPQuery();
spquery.Query = query;
ContentIterator iterator = new ContentIterator();
iterator.ProcessListItems(list, spquery, ProcessListItem, ProcessListItemError)
Now run the program and it will work.

Sorting files in sharepoint folder (2007)

I am looking for a way to sort files by date (id seems to do the same).
SPFolder imageFolder = web.GetFolder(...);
...
I know I have to do this with caml but how?
Thanks
Yes, this should be done with a CAML query. Use the SPQuery class to execute such a query. Use the OrderBy element in order to sort the result set:
<OrderBy>
<FieldRef Name="yourdatefield" />
</OrderBy>
Example:
SPList list = ... // the list where you images are stored.
SPQuery query = new SPQuery();
query.Folder = imageFolder;
query.Query = "<OrderBy><FieldRef Name=\"Created\" /></OrderBy>";
SPListItemCollection items = list.GetItems(query);
The variable items now contains the contents of the imageFolder sorted by the field "Created".
In order to access the image files use the member File on SPListItem:
foreach (SPListItem item in items)
{
Console.WriteLine("Filename: " + item.File.Name);
}

Sharepoint SPQuery problem

I'm trying to use GetItems() method on a SPList and I pass SPQuery to it. The problem is, it return all the items from my SPList instead of just the filtered ones. My query looks like this:
<WHERE><Eq><FieldRef Name='Type' /><Value Type='Text'>Analysis</Value></Eq></WHERE>
Thye typye of the'Type' column is Single line of text, which i believe translates to Text in CAML. Then I just do the standard stuff:
SPQuery q = new SPQuery();
q.Query = CAMLQuery.ToString();
var filtered = _NoticeList.GetItems(q);
filtered.Count is 4 instead of 2... perhaps someone cann se whats wrong with this code
I think CAML is Case sensitive so it'd have to be:
<Where><Eq><FieldRef Name='Type' /><Value Type='Text'>Analysis</Value></Eq></Where>
Otherwise you could try renaming the 'Type' field, because it might be interpreted as an internal field.

SharePoint list CAML query using CONTAINS

I'm trying to query a SharePoint list using the following CAML query in a webpart. I have tested the query in U2U CAML Query Builder and Stramit CAML Viewer and it works fine, only returning the matching records, but when I use it in my webpart it return all list items. It is driving me crazyyyyy. Here is the code:
string camlQuery = string.Format(#"<Query><Where><Contains><FieldRef Name='Title' /><Value Type='Text'>2</Value></Contains></Where></Query>");
SPQuery query = new SPQuery();
query.Query = camlQuery;
SPListItemCollection items = Articles.GetItems(query);
grid.DataSource = items.GetDataTable();
grid.DataBind();
Leave out the surrounding Query tag, just use:
<Where><Contains><FieldRef Name='Title' /><Value Type='Text'>2</Value></Contains></Where>
SPQuery adds the Query tag itself.

Resources