Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I use SP Client and want to return ListItemCollection which will be filled with items from specific view which I choose. I get ViewQuery string by sending view index in viewcollection. Problem is in part where I send camlQuery to GetItems method "ListItemCollection listItems = list.GetItems(camlQuery);" because whatever camlquery is sent to GetItems I allways get ALL items from list instead of FILTERED ones, despite the fact that camlquery string are good and different every time. Any suggestion?

        clientContext.ExecuteQuery();
        List list = site.Lists.GetByTitle(SPList);
        clientContext.Load(list);
        clientContext.ExecuteQuery();

        ViewCollection viewItems = list.Views;
        clientContext.Load(viewItems);
        clientContext.ExecuteQuery();


        CamlQuery camlQuery = new CamlQuery();
        camlQuery.ViewXml = list.Views[index].ViewQuery;

        ListItemCollection listItems = list.GetItems(camlQuery);
        clientContext.Load(listItems);
        clientContext.ExecuteQuery();
share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

I've had this recently.

The Client OM version of CamlQuery behaves differently to how you think.

Inspect the results of list.Views[0].ViewQuery.

If it has a tag around it, remove it before you assign to it CamlQuery.ViewXml. (possibly a bit tricky)

If it doesn't have a tag around it, add one. (easy)

Then assign it to CamlQuery.ViewXml. Almost certain this will work, as the symptoms you describe are identical to what I had, except I was using the Silverlight object model. Same objects, however.

EDIT:

Also, streamline your code, you've got a lot of "blocking" operations there that you probably don't need...

clientContext.ExecuteQuery();
List list = site.Lists.GetByTitle(SPList);
ViewCollection viewItems = list.Views;
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = list.Views[index].ViewQuery;
ListItemCollection listItems = list.GetItems(camlQuery);

clientContext.Load(list);
clientContext.Load(viewItems);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
share|improve this answer
    
Thank you man!! My ViewQuery looks like: <Where> <Gt> <FieldRef Name=\"acis_savjetovanje_datumpocetka\" /> <Value Type=\"DateTime\"> <Today /> </Value> </Gt> </Where> <OrderBy> <FieldRef Name=\"acis_savjetovanje_datumpocetka\" Ascending=\"FALSE\" /> </OrderBy> If I understand you good I should add tags <view> and <Query> around this from ViewQuery. to create good ViewXML. Is that idea? –  trip Jan 26 '11 at 9:30
    
Yup, that's it. Just use string concatenation, so do something like "<View>" + camlQuery.ViewXml + "</View>" –  James Love Jan 26 '11 at 10:26
    
Thanx again. That's it. You rock :) –  trip Jan 26 '11 at 12:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.