CAMLQueryBuilder in new drop

Jul 28, 2009 at 9:12 PM

Hi Guys,

As far as I understand CAML, you should encapsulate in "And" or "Or" tags no more then 2 operations. In your code:

 

        public SPQuery Build()
        {
            SPQuery spQuery = new SPQuery();

            StringBuilder queryBuilder = new StringBuilder("<Where>");

            if (filters.Count > 1)
            {
                queryBuilder.Append("<And>");
            }

            foreach (CAMLFilter filter in this.filters)
            {
                queryBuilder.Append(filter.FilterExpression);
            }

            if (filters.Count > 1)
            {
                queryBuilder.Append("</And>");
            }

            queryBuilder.Append("</Where>");

            spQuery.Query = queryBuilder.ToString();
            return spQuery;
        }

query builder with more then 2 filters will failed:

<where>
    <and>
          <eq>...</eq>
          <eq>...</eq>
          <neq>...</eq>
    </and>
</where>

whereas

<where>
    <and>
        <and>
              <eq>...</eq>
              <eq>...</eq>
        </and>
        <neq>...</eq>    
    </and>
</where>

will work.

Do you plan to include a "true" builder in your next realease or CamlQueryBuilder is a temporary solution?

Cheers,

   Jakub G

 

Aug 18, 2009 at 8:39 PM
Edited Aug 18, 2009 at 8:40 PM

Hi Jakub G,

Currently CAMLQueryBuilder is developed based on RI Requirement and we are shipping Sources for SharePoint Guidance Library in our Release, users can extend for other operations.


Thanks
Appaji

Sep 21, 2010 at 3:43 PM

Here is the fix

public SPQuery Build()        {            SPQuery spQuery = new SPQuery();
            StringBuilder queryBuilder = new StringBuilder("<Where>");
            for (int i = 1; i < filters.Count; i++)            {                queryBuilder.Append("<And>");            }            int count = 1;
            foreach (CAMLFilter filter in this.filters)            {                queryBuilder.Append(filter.FilterExpression);                if (count > 1 && count <= filters.Count)                {                    queryBuilder.Append("</And>");                }                count++;            }

            queryBuilder.Append("</Where>");
            spQuery.Query = queryBuilder.ToString();            return spQuery;        }

Sep 25, 2010 at 2:06 AM
Edited Sep 25, 2010 at 2:06 AM
gbelzile wrote:

Here is the fix

public SPQuery Build()        {            SPQuery spQuery = new SPQuery();
            StringBuilder queryBuilder = new StringBuilder("<Where>");
            for (int i = 1; i < filters.Count; i++)            {                queryBuilder.Append("<And>");            }            int count = 1;
            foreach (CAMLFilter filter in this.filters)            {                queryBuilder.Append(filter.FilterExpression);                if (count > 1 && count <= filters.Count)                {                    queryBuilder.Append("</And>");                }                count++;            }

            queryBuilder.Append("</Where>");
            spQuery.Query = queryBuilder.ToString();            return spQuery;        }

  Hi Jakub G,

 Above solution can fix this issue. Below is the output for 4 filters. But it is not an universal function for the SPQuery building. it only work on the "And" operation.

<Where>
  <And>
    <And>
      <And>
        <eq>...</eq>
        <eq>...</eq>
      </And>
      <neq>...</neq>
    </And>
    <neq>...</neq>
  </And>
</Where>

 

Thanks,

Richie Wang

Sep 27, 2010 at 10:34 PM

gbelzile, RichieWang

Thanks, however I was not looking for the solution. I wast just curios why MS is giving P&P Guide that contains errors. Lots of folks use these documents as how to guide or as libraries to use in the code. Releasing SPG with errors inside is what MS should not do.

I could understand why this solution did support only AND (and OR - not sure about that) operators but why the code was released with bug - i don't know. And I don't believe that this was a requirement, I can see the "client", saying "I need that bug in app!" - life would be too beautiful :)

Cheers