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:

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

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
add comment

1 Answer

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>"

HTH

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
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.