Logical AND ,OR CAML Query not working in External List - sharepoint

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

Related

Sharepoint Online - CAML query in CSOM Script issue with 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?

CAML query is returning all the list items

I have the following CAML query
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<Query><Where><Eq><FieldRef Name='RiskRequestID'/><Value Type='Text'>" + r.id + "</Value></Eq></Where></Query>";
ListItemCollection oListItems = context.Site.RootWeb.GetList(UFSsiteUrl + "Lists/RiskManagement/").GetItems(camlQuery);
context.Load(oListItems);
context.ExecuteQuery();
to get the item with the entered RiskRequestID.. currently instead of getting one item i am getting all the items inside the list.. any advice?
and the RiskRequestID is a SharePoint field of type Single Line of text.
Add tag inside camlQuery.viewXml:
camlQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='RiskRequestID'/><Value Type='Text'>" + r.id + "</Value></Eq></Where></Query></View>";

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.

need to convert SPQuery on a single document library to SPSiteDataQuery on all document libraries in the site

I need help converting the following code snippet to use SPSiteDataQuery instead of SPQuery b/c I need to query accross all Document Libraries in the site.
Here's the original code:
using (SPWeb oWeb = new SPSite(properties.SiteId).OpenWeb(properties.RelativeWebUrl))
{
SPList oList = oWeb.Lists["Quality Documents"];
//create query
SPQuery oQuery = new SPQuery();
//configure the query //
oQuery.Query = "<Where><Eq><FieldRef Name='Document_x0020_Number' /><Value Type='Text'>" + docNum + "</Value></Eq></Where>";
//get the collection of items in the list
SPListItemCollection oItems = oList.GetItems(oQuery);
if (oItems.Count > 0)
{
newRnd = 0;
}
else
{
newRnd = 1;
}
}
And here's what I have for the SPSiteDataQuery but I don't know if it's correct and/or how to make sure the code does the same thing as the above code.
SPSiteDataQuery q = new SPSiteDataQuery();
q.Lists = "<Lists BaseType='1'/>";
q.Query = "<Where><Eq><FieldRef Name='Document_x0020_Number' /><Value Type='Text'>" + docNum + "</Value></Eq></Where>";
q.Webs = "<Webs Scope='SiteCollection' />";
q.ViewFields = "<FieldRef Name='Document_x0020_Number' />"';
q.RowLimit = 1;
The main difference between SPQuery and SPSiteDataQuery is in specifying the list to query. Unlike with SPQuery, in SPSiteDataQuery you cannot specify a list title to narrow down your query in each site, you only have the Lists element which is a lot less flexible. Vincent Rothwell describes all of the possibilities here.
This inflexibility means that the use of BaseType=1 in your Lists element code will return documents from all document libraries. The only way you can avoid this is if you deployed a custom list template for Quality Documents with its own ID number. You would then be able to use <Lists ServerTemplate='XYZ' /> (where XYZ is the ID number to narrow results down to that type of list). Otherwise you will need to filter the results you get back so they only contain entries from the Quality Documents library.
Apart from this your code looks equivalent. You may also like to look at CrossListQueryInfo and CrossListQueryCache if you are running MOSS. These allow you to cache SPSiteDataQuery-style queries. See this post by Jeff Dalton for good information on this.
Update from comments about object disposal:
Your code will leak the SPSite object - this needs a using clause as well. Replace the using line you currently have with these lines:
using (SPSite oSite = new SPSite(properties.SiteId))
using (SPWeb oWeb = oSite.OpenWeb(properties.RelativeWebUrl))
{
// Your code
}
Also, it is easy to use SPDisposeCheck. To integrate with Visual Studio add the following line to your project's post build event:
"%ProgramFiles%\Microsoft\SharePoint
Dispose Check\SPDisposeCheck.exe"
$(TargetPath)
The build will now fail if you have memory leaks. Check the Output window for details on where they are. Note that SPDisposeCheck sometimes reports false positives and it is possible to ignore these (read the documentation to tell you how).

Resources