Sharepoint Online - CAML query in CSOM Script issue with date range - date-range

I need to run in a javascript webpart (CSOM) a CAML query between 2 dates.
CAML query returns nothing if I define date criteria with correct format ISO8601 (yyyy-MM-ddTHH:mm:ssZ).
var cQuery = new SP.CamlQuery();
var camlXML = "<View><Query><Where><And>"
+"<Eq><FieldRef Name='Author' LookupId='True'/><Value Type='Lookup'><UserID/></Value></Eq>"
+"<Geq><FieldRef Name='EventDate' /><Value IncludeTimeValue='False' Type='DateTime'>2022-05-18T01:00:00Z</Value></Geq>"
+"<Leq><FieldRef Name='EventDate' /><Value IncludeTimeValue='False' Type='DateTime'>2022-08-24T01:00:00Z</Value></Leq>"
+"</And></Where></Query></View>";
/* camlXML = "<View><Query><Where>"
+"<Eq><FieldRef Name='Author' LookupId='True'/><Value Type='Lookup'><UserID/></Value></Eq>"
+"</Where></Query></View>"; */ This query works perfectly...
cQuery.set_viewXml(camlXML);
oListItems = oList.getItems(cQuery);
I'va also tried to define only date without time (ie 2022-05-18) but same issue.
Any suggestion?

Related

Query on Date Fields using CAML

I am trying to run a query against a sharepoint list (using the client object model), finding an item that matches on a date field.
My query is:
2012-07-24T02:50:28
1
I know that there is an item in the list where the EmDateSent field has the value 2012-07-24T02:50:28 and yet when I run this I get 0 rows back.
Having spent hours researching and experimenting I found that the date formatting I have used works - or so I thought. It seems that now it doesn't.
My code is below.
Can anybody suggest something else I can try?
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = String.Format(
#"<View>
<Query>
<Where>
<Eq>
<FieldRef Name='{0}'/>
<Value Type='DateTime' IncludeTimeValue='True'>{1}</Value>
</Eq>
</Where>
</Query>
</View>", EMAIL_FIELD_DATE_SENT, details.DateSent.HasValue ? details.DateSent.Value.ToString("yyyy-MM-ddTHH:mm:ss") : "");
ListItemCollection listItems = upLoadList.GetItems(camlQuery);
_context.Load(listItems);
_context.ExecuteQuery();
if (listItems.Count > 0) // At this point I expect listItems.Count to be 1, but it is 0
{
....
Your example looks correct, and there is not an issue with using the "T" in the time stamp. However, from what I can find, most of the examples like yours use the SPQuery class instead of the CamlQuery class. I believe this is causing your issue.

Logical AND ,OR CAML Query not working in External List

I am using ClientContext Class and CAML query to retrieve items from External List in sharepoint 2010.I have built the query with Logical AND Condition in CAML Query like this
string sDataFilter =<Query><Where><And><Eq><FieldRef Name="Year" /><Value Type="Text">1960</Value></Eq><Contains><FieldRef Name="ChartName" /><Value Type="Text">Chart1</Value></Contains></And></Where></Query>
When I exceute the following code
List oList = clientContext.Web.Lists.GetById(new Guid(list));
CamlQuery camlQuery = new CamlQuery();
string queryContext = "<View><Query>" + sDataFilter + "</Query>" + viewFieldsContext + "</View>";
camlQuery.ViewXml = queryContext;
ListItemCollection collListItem = oList.GetItems(camlQuery);
clientContext.Load(collListItem);
clientContext.ExecuteQuery();
I am getting the empty ListItemCollection.I have cross verified with the U2U CAML Query Builder the data is empty only. So in External List the caml query is working fine for sorting and simple filter query like Where,EqualTo condition I can't use Logical conditions in the query ? If yes how could I achieve this using caml query
Could anyone help me to resolve this issue ?
The problem is below line of code
string queryContext = "<View><Query>" + sDataFilter + "</Query>" + viewFieldsContext + "</View>";
change this line with follwing code
string queryContext = "<View>" + sDataFilter + viewFieldsContext + "</View>";
the problem is in Query tag. It's two time repeat in query.
late me know when it's working
Thax

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

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

Strange behaviour of CAML question - why?

I'm using caml query to retrieve data from sharepoint list.
The issue is that the query returns one record when it shouldn't, and this takes place only when I'm using this query in my own code.
When I'm using the same query, on the same list by U2U Caml Query Builder it works wright.
My query is like this:
<Query><Where><Eq><FieldRef Name="Account_x0020_Verification_x0020" /><Value Type="Text">211edd1d11844d6c9f21d7930683caba</Value></Eq></Where></Query>
and the vb.net code I'm using to fired it up is like this:
Dim oUserAccStatusList As SPList = oElevatedSPWeb.Lists(sListName)
Dim oSPQuery As New SPQuery
oSPQuery.Query = "<Query><Where><Eq><FieldRef Name='Account_x0020_Verification_x0020' /><Value Type='Text'>" + _sUserID + "</Value></Eq></Where></Query>"
Dim oItems As SPListItemCollection = oUserAccStatusList.GetItems(oSPQuery)
If oItems.Count <= 0 Then
Items.Count is 1 and inside it there is a record with userid different than I was asking for. Do anybody knows what's going on, and why does it happened?
remove the query node out of the oSPQuery.Query property.
oSPQuery.Query = "<Where><Eq><FieldRef Name='Account_x0020_Verification_x0020' /><Value Type='Text'>" + _sUserID + "</Value></Eq></Where>"

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