CAML query filtering is not working

1.2k views Asked by At

I've googled that it can be in case when query is not well-formed. But I'm verifying it for several hours and didn't find any mistake. Problem is common: query doesn't filters and returns all data.

I have no idea what is wrong, because I checked this query in Stramit CAML viewer and it worked fine.

        private static SPListItemCollection GetItemsForThisUserAndEvent(SPList list)
        {
            const string queryMask = 
@"<Where>
    <And>
        <Eq>
            <FieldRef Name='{0}' LookupId='TRUE' />
            <Value Type='Lookup'>{1}</Value>
        <Eq>
        <Eq>
            <FieldRef Name='{2}' LookupId='TRUE' />
            <Value Type='User'>{3}</Value>
        <Eq>
    </And>
<Where>";

            var query = string.Format(queryMask, MemberListFieldLookupEventName, SPContext.Current.ListItem.ID,
                                                 MemberListFieldLogin, SPContext.Current.Web.CurrentUser.ID);
            return list.GetItems(query);
        }
1

There are 1 answers

0
Servy On BEST ANSWER

The overload of SPList.GetItems that would accept a string is actually this overload which accepts a params string[] argument of a list of fields that should be returned while returning all items in that list. It's not expecting a CAML query.

You need to stuff your CAML into an SPQuery object and then give that SPQuery object to GetItems:

list.GetItems(new SPQuery() { Query = query });