Based on feedback from my earlier question, I have constructed a query using the excellent CAML Builder tool:

  <Membership Type='CurrentUserGroups'>
    <FieldRef Name='Users'/>
  <FieldRef Name='Users'/>
  <Value Type='Integer'>

This indeed returns a filtered list of rows.

However, trying to implement the same query in code fails to filter the list (all items are returned):

     SPQuery query = new SPQuery();
     query.Query =
        "<Where><Or><Membership Type='CurrentUserGroups'><FieldRef Name='Users'/></Membership>" +
        "<Eq><FieldRef Name='Users'/><Value Type='Integer'><UserID /></Value></Eq></Or></Where>";
     var items = spList.GetItems(query);

I have attempted to use the ViewXml property instead, but no success either.

EDIT: I have removed the enclosing <Query> tags as suggested, but now rather than returning all items, the query now return no items at all!

  • try removing the Query tag, you're sending it to query.Query, so it's already in there. Oct 19, 2012 at 1:31

2 Answers 2


The query inside an SPQuery.Query should not have the <Query> tag.

  • 1
    Removing the <Query> tag returns no items at all (instead of all items with the tag). Oct 19, 2012 at 16:26
  • If the query is not returning anything, maybe it's working and the filters need to be modified. What exactly is the query trying to achieve? That would be useful to know to see if any of the clauses need to be modified. Oct 20, 2012 at 18:01

Hi in your SPQuery please remove tag, also please escape character to remove empty space in your query.

query.Query = @"<Where><Or><Membership Type='CurrentUserGroups'><FieldRef Name='Users'/></Membership>" + "<Eq><FieldRef Name='Users'/><Value Type='Integer'><UserID /></Value></Eq></Or></Where>";

you can also limit on results returned to one if you would like to by

query.RowLimit = 1;

If this fails, can you please debug can you please post the query value which you get? This should positively work.

  • He doesn't need to use @ - that doesn't do anything about escaping empty spaces.
    – Ryan
    Oct 19, 2012 at 7:14
  • Where can I see the 'query you get'? Oct 19, 2012 at 16:27
  • I have updated my original post. Please check. Also, if this fails, try to debug your query and post the value you get. Nov 10, 2012 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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