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

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!

share|improve this question
    
try removing the Query tag, you're sending it to query.Query, so it's already in there. –  David Lozzi Oct 19 '12 at 1:31
add comment

2 Answers

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

share|improve this answer
1  
Removing the <Query> tag returns no items at all (instead of all items with the tag). –  Philipp Schmid Oct 19 '12 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. –  Amadeo Gallardo Oct 20 '12 at 18:01
add comment

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.

share|improve this answer
    
He doesn't need to use @ - that doesn't do anything about escaping empty spaces. –  Ryan Oct 19 '12 at 7:14
    
Where can I see the 'query you get'? –  Philipp Schmid Oct 19 '12 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. –  Battlesmith Nov 10 '12 at 9:57
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.