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:
Second, rearrange your code like this:
I know the code blocks looks the same, but neither
SPContextnorDefaultViewuse private fields. For example, here is the implementation ofDefaultView:So with:
The first line sets the
Queryproperty of an instance ofDefaultViewwhile the second line callsUpdateon a new instance of theDefaultView.