I have created a CAML query to get some particular items on a list, that contains OR:
<Or>
<Eq><FieldRef Name='Title'/><Value Type='Text'>tileA</Value></Eq>
<Eq><FieldRef Name='Title'/><Value Type='Text'>titleB</Value></Eq>
</Or>
Now the query works fine if I pass it to list.GetItems() metod, but it doesn't work when I use it like that:
SPContext.Current.List.DefaultView.Query = myStringQuery;
SPContext.Current.List.DefaultView.Update();
I place the code in a webpart (Page_Load()), that is added to the list, the code executes, but the view remains unfiltered. Anyone knows what might be the reason for that?
Two things:
First, make sure your CAML is wrapped in a Where element:
<Where>
<Or>
<Eq><FieldRef Name='Title'/><Value Type='Text'>tileA</Value></Eq>
<Eq><FieldRef Name='Title'/><Value Type='Text'>titleB</Value></Eq>
</Or>
</Where>
Second, rearrange your code like this:
SPView view = SPContext.Current.List.DefaultView;
view.Query = myStringQuery;
view.Update();
I know the code blocks looks the same, but neither SPContext
nor DefaultView
use private fields. For example, here is the implementation of DefaultView
:
internal SPView DefaultView
{
get
{
if (this.m_iDefaultViewIndex == -1)
return (SPView) null;
else
return this[this.m_iDefaultViewIndex];
}
}
public SPView this[int iIndex]
{
get
{
if (iIndex < 0 || iIndex >= this.Count)
throw new ArgumentOutOfRangeException();
else
return new SPView(this, this.m_arrViewSchema, iIndex);
}
}
So with:
SPContext.Current.List.DefaultView.Query = myStringQuery;
SPContext.Current.List.DefaultView.Update();
The first line sets the Query
property of an instance of DefaultView
while the second line calls Update
on a new instance of the DefaultView
.