Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I would like to write CAML query for SharePoint 2007 which have Contains query.

The only problem is that I want to use logic like:

If my search query has value, search for that with contains query If my search query is empty, select all items from list

I have tried with several queries (this is my null case):

<Query>
    <Where>
        <Or>
            <IsNull>
                <FieldRef Name="ImageFilter"/>
            </IsNull>
            <Contains>
                <FieldRef Name='ImageFilter'></FieldRef>
                <Value Type='Text'></Value>
            </Contains>
        </Or>
    </Where>
</Query>

And

<Query>
    <Where>
        <Contains>
            <FieldRef Name='ImageFilter'></FieldRef>
            <Value Type='Text'></Value>
        </Contains>
    </Where>
</Query>

But nothing works,

Could you please help me with that?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

If I understand this correctly, if you have a search query passed to whatever function is generating the CAML query, you want use the search term to find items that contain that search term. If no search term is present, you want to return all items. While I'm not sure what you're using to generate your CAML, the easiest way to do this is to not include the Query in your call if the search term is not included. If there is a search term, then this should work

<Query>
  <Where>
    <Contains>
      <FieldRef Name='ImageFilter'>
      <Value Type='Text'>{searchterm}</Value>
    </Contains>
  </Where>
</Query>

Where {searchterm} is the term you're looking for.

share|improve this answer
    
This is what I thought :( I need to handle that in different way. Thanks –  Tomasz May 20 at 20:52

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.