2

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

<Where>
<Or>
  <Membership Type='CurrentUserGroups'>
    <FieldRef Name='Users'/>
 </Membership>
 <Eq>
  <FieldRef Name='Users'/>
  <Value Type='Integer'>
    <UserID/>
  </Value>
 </Eq>
</Or>
</Where>

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!

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

2 Answers 2

2

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

2
  • 1
    Removing the <Query> tag returns no items at all (instead of all items with the tag). Commented 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. Commented Oct 20, 2012 at 18:01
1

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.

3
  • He doesn't need to use @ - that doesn't do anything about escaping empty spaces.
    – Ryan
    Commented Oct 19, 2012 at 7:14
  • Where can I see the 'query you get'? Commented 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. Commented 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.