1 out of 2 rated this helpful - Rate this topic

Contains Element (Query)

Windows SharePoint Services 3

Searches for a string anywhere within a column that holds Text or Note field type values.


<Contains>
  <FieldRef Name = "Field_Name"/>
  <Value Type = "Field_Type"/>
  <XML />
</Contains>
Attribute Description

None

N/A

Minimum: 0

Maximum: Unbounded

The following example uses the Contains element within a string that is assigned to the Query property to return the titles of items where the Conference column value begins with "Morning" and contains "discussion session".

SPWeb mySite = SPControl.GetContextWeb(Context);

SPList list = mySite.Lists["List_Name"];

SPQuery query = new SPQuery();
query.Query = "<Where><And><BeginsWith><FieldRef Name="Conference"/>" + 
   "<Value Type="Note">Morning</Value></BeginsWith>" + 
      "<Contains><FieldRef Name="Conference" />
   <Value Type="Note">discussion session</Value>" + 
      "</Contains></And></Where>";

SPListItemCollection myItems = list.GetItems(query);

foreach (SPListItem item in myItems)
{
   Label1.Text += item["Title"] + "<BR>";
}
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Can the field value be a subset of the Value element?
I have tried a CAML query like the following:

<Where>
<Contains>
<Value Type="Text">SomeDataHere</Value>
<FieldRef Name="Title" />
</Contains>
</Where>

Unfortunately, it doesn't work. Is there a reason why it shouldn't work this way?

Thanks
How to implement "ignore case" for Contain
I wanna implement a caml query like:
<where>
<Contains>
<FieldRef Name=\"Title\" />
<Value Type=\"Text\">
Key
</Value>
</Contains>
</where>

If i wanna ignore the case, ex. i wanna search all the items whose title contain "key" or "Key" or "KEY" or...
Does CAML support it?

PS: Same condition when i search "e" but i have many é,è,ê in my items.