Question

I have a list with 3 views.

I want find special items of this. I use SPQuery for it.

<Where>
 <Eq>
   <FieldRef Name='TypeTitle' />
   <Value Type='Lookup'>Test1</Value>
 </Eq>
</Where>

and getitems of list :

string viewname="View1";
list.GetItems(query,viewname).GetDataTable();

But return all items of view allitems with name :Test1. While the there is no item to View1 .

Was it helpful?

Solution

If you have a look at the definition of the SPList.GetItems(SPQuery,string) method: http://msdn.microsoft.com/en-us/library/ms434064.aspx

You can see that the second parameter needs to be the GUID of the view enclosed in curly braces. Eg: {B080E5D2-DEF9-11E1-8A8D-550A6188709B}

Try the following code:

string viewGUID = list.Views["View1"].ID.ToString("B").ToUpper();
list.GetItems(query,viewGUID).GetDataTable();

Also, in the MSDN docs it is stated that:

The properties of the view that is specified by the viewName parameter override the properties that are specified in the query object that is passed through the query parameter. For example, if the query object includes a tag that specifies only items containing a particular column value, while the view specifies to return all items, this method retrieves all of the items

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top