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.
Related
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?
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
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);
}
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
I have a webpart which is 2-3 subsites down the top level site. I need to query the list which is in top site collection and one at the same level,I guess its possible through SPSiteDataquery...I have some confusion related to it can i write single query which can query both these list....
The scope of this query is sitecollection so that means it wud going to look into all list in sitecollection..and if my CAML query is same for both these lists ...it should work?
let me explain through my code:
SPSite mySite = SPControl.GetContextSite(Context);
SPWeb myWeb = SPControl.GetContextWeb(Context);
SPSiteDataQuery qry = new SPSiteDataQuery();
qry.Lists = "<Lists BaseType='0' />";
qry.Query = "<Where><Contains><FieldRef Name='Country'/><Value Type='Text'>" + strcount + "</Value></Contains></Where>";
qry.ViewFields = "<FieldRef Name='Capital' Nullable='TRUE'/><FieldRef Name='Currency' Nullable='TRUE'/>";
qry.Webs = "<Webs Scope='SiteCollection' />";
DataTable dt = myWeb.GetSiteData(qry);
Now i need currency from list which is in top level site and Capital from the list which is at same level. Is this possible? or I misunderstood SPSiteDataQuery...?
You are on the right track and it is possible to retrieve results for lists in different webs. The following example shows how to retrieve items from the default 'Tasks' or 'Workflow Tasks' lists and works with task lists created at the root level and within a sub site.
SPSiteDataQuery q = new SPSiteDataQuery();
q.ViewFields = "<FieldRef Name='Title'/><FieldRef Name='Priority'/><FieldRef Name='Status'/>";
q.Webs = "<Webs Scope='SiteCollection' />";
q.Lists = "<Lists BaseType='0' />";
q.Query = "<Where><Gt><FieldRef Name='ID' /><Value Type='Number'>0</Value></Gt></Where>";
DataTable results = new DataTable();
using (SPSite site = new SPSite("http://sharepoint"))
{
using (SPWeb web = site.OpenWeb("subsite"))
{
results = web.GetSiteData(q);
}
}
I've written it using a hardcoded URL so you can run it inside a console application for testing but you can replace the using statements with something like SPWeb web = SPContext.Current.Web; when you put this inside a web part.
A few other things worth considering:
The lists you are querying must contain all the fields in the ViewFields element
Multi-lookup fields do not work well with the SPSiteDataQuery (single value lookup fields are ok)
The u2u CAML builder tool is also useful for testing CAML queries. See http://www.u2u.be/Res/Tools/CamlQueryBuilder.aspx