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

I have the following "base" query:

            <FieldRef Name='Field1' />
            <Value Type='Boolean'>1</Value>
               <FieldRef Name='Field 2' />
               <Value Type='Text'>TextQuery</Value>
      <FieldRef Name='OrderField' Ascending='True' />

I would like to be able to dynamically add And blocks based on whether drop down boxes have a selected value. Example:

 strQuery = "";

 if (ddlNewField.SelectedIndex != 0)
   strQuery += "<And>...</And>";

The strQuery value would then be concatenated somewhere in the base query.

It obviously doesn't work. After a google search it looks like blocks need to be paired with only one other value or And block. I'm currently having coders-block.

How can I accomplish this?

Thanks in advance.

edit: I can change the base query if necessary

share|improve this question

1 Answer 1

up vote 2 down vote accepted

I've done similar like

string baseQry = "<and><eq>...</eq><eq>...</eq></and>"
string finalQry = baseQry;

and then if selected index is not 0, or wherever you want to add it

finalQry = "<and>" + finalQry + "<eq>new one here</eq></and>"

And running the above code will continually build out the statement. When you're ready to use it, add the where and orderby

finalQry = "<where>" + finalQry + "</where><orderby>...</orderby>"


share|improve this answer
That definitely works, thanks. Now I'm trying to figure out why the query doesn't refine the gridview I'm attaching the data set to. –  David King Dec 27 '11 at 18:30
Almost, but this requires that baseQry exists, so requires minimum of two values. Still good start on the topic. –  Jussi Palo May 16 '12 at 9:33

Your Answer


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.