Hi All,
The following example demonstrate how to get list items by using he "IN" Operator in CAML Query.
Syntax:
<Where>
<In>
<FieldRef Name='ColumnName' />
<Values>
<Value Type='Text'>1</Value>
<Value Type='Text'>2</Value>
<Value Type='Text'>3</Value>
.
.
.
<Value Type='Text'>3</Value>
</Values>
</In>
</Where>
below example will explain , generating values dynamically and used to get items from List
using (SPSite objSite = new SPSite("SiteURL"))
{
using (SPWeb objWeb = objSite.OpenWeb())
{
string strQuery = string.Empty;
if (dtRequests != null && dtRequests.Rows.Count > 0)
{
foreach (DataRow drRequests in dtRequests.Rows)
{
if (string.IsNullOrEmpty(strQuery))
strQuery = "<Value Type='Text'>" + Convert.ToString(drRequests["Col1"]) + "/Value>";
else
strQuery = strQuery + "<Value Type='Text'>" + Convert.ToString(drRequests["Col1"]) + "</Value>";
}
SPList oList = objWeb.Lists.TryGetList("ListName");
SPQuery oQuery = new SPQuery();
oQuery.Query = "<Where><In><FieldRef Name='Col1' /><Values>" + strQuery + "</Values></In></Where>";
oQuery.ViewFields = @"<FieldRef Name='Column2' />";
SPListItemCollection=oList.GetItems(oQuery);
// do
}
}
}
The following example demonstrate how to get list items by using he "IN" Operator in CAML Query.
Syntax:
<Where>
<In>
<FieldRef Name='ColumnName' />
<Values>
<Value Type='Text'>1</Value>
<Value Type='Text'>2</Value>
<Value Type='Text'>3</Value>
.
.
.
<Value Type='Text'>3</Value>
</Values>
</In>
</Where>
below example will explain , generating values dynamically and used to get items from List
using (SPSite objSite = new SPSite("SiteURL"))
{
using (SPWeb objWeb = objSite.OpenWeb())
{
string strQuery = string.Empty;
if (dtRequests != null && dtRequests.Rows.Count > 0)
{
foreach (DataRow drRequests in dtRequests.Rows)
{
if (string.IsNullOrEmpty(strQuery))
strQuery = "<Value Type='Text'>" + Convert.ToString(drRequests["Col1"]) + "/Value>";
else
strQuery = strQuery + "<Value Type='Text'>" + Convert.ToString(drRequests["Col1"]) + "</Value>";
}
SPList oList = objWeb.Lists.TryGetList("ListName");
SPQuery oQuery = new SPQuery();
oQuery.Query = "<Where><In><FieldRef Name='Col1' /><Values>" + strQuery + "</Values></In></Where>";
oQuery.ViewFields = @"<FieldRef Name='Column2' />";
SPListItemCollection=oList.GetItems(oQuery);
// do
}
}
}
0 comments:
Post a Comment