5

So if I use SPServices to GetListItems with CAMLQuery, is it possible to insert multiple values inside Value Type='Number'> here

I would like to evaluate things throught a flow in my page, add to an array, and then query using SPServices. Do I have to create a string for each value from the array, and do like a CAMLQueryString += string from .each value of array, and then CAMLQuery = CAMLQueryString inside the SPServices options kind of thing?

Perhaps this is a CAML question, or an SPServices question? I am not really sure.

Thanks Justin

4 Answers 4

12

It is more a caml question than an SPServices question.

Often one uses or and and operators to combine multiple values, which, like @rjcp3 said, can be messy.

There is another operator, less known, IN operator to choose a range of values (see the xml example below).

To simplify the creation of CAML queries in javascript, I'd recommend SharePoint EcmaScript Caml Builder - an open source javascript library released under the Ms-PL licence on codeplex. Creds go to omlin.

Here is an example from camljs.codeplex.com:

caml = camlBuilder.Where()
    .LookupIdField("Category").In([2, 3, 10])
    .And()
    .DateField("ExpirationDate").LessThanOrEqualTo("{Now}")
    .OrderByDesc("ExpirationDate")
    .ToString()

This results in the following CAML query which you can put into a SPServices request:

<Where>
  <And>
    <In>
      <FieldRef Name="Category" LookupId="True" />
      <Values>
        <Value Type="Integer">2</Value>
        <Value Type="Integer">3</Value>
        <Value Type="Integer">10</Value>
      </Values>
    </In>
    <Leq>
      <FieldRef Name="ExpirationDate" />
      <Value Type="Date">
        <Now />
      </Value>
    </Leq>
  </And>
</Where><OrderBy>
  <FieldRef Name="ExpirationDate" Ascending="False" />
</OrderBy>
4
  • 1
    Did not know about the resourced mentioned above (camljs)... Looks very good and easy to use, specially for jQuery knowledgeable folks... Looks like its design embraces the chainable approach. Thanks.
    – Paul T.
    Oct 16, 2012 at 0:26
  • @Anatoly...thanks for the information. i didn't realize operator get nested like that, and I didn't know about the IN. Actually I think I am looking more for a multiple OR idea from an array. so if i have a bunch of values within an array and I just want to do a select where this Number = any value in the array (which i guess is OR OR OR OR)?? I think? thanks for your help; always appreciated!
    – Justin
    Oct 16, 2012 at 14:22
  • @PaulT. It is cool that is chainable like jQuery, endeed. @Justin, it can differ from situation to situation. But to me multiple <OR> nodes seem to be exactly what <IN> operator means. It specifies an element which element is equal to one of the values specified = "Number = any value"... Of course, it can be formulated as a series of <OR>nodes. Hope it helps. Oct 16, 2012 at 14:38
  • 1
    yeah i can totally see what you are saying now. thanks Anatoly, this is exactly what I was looking for and didn't even realize that operator was there. i am also going to check out that library as well. thanks again
    – Justin
    Oct 17, 2012 at 13:17
1

You can't add multiple values directly to one node, but you can add multiple <Or> or <And> elements (it just gets a little messy with complex statements).

1
  • i think the idea is having multiple ORs, but as many as exist within the array if that makes sense? so to start, query with a where clause on single field where the value can be array index[0] or [1] or [2].... i think this is kinda what i am shooting for
    – Justin
    Oct 16, 2012 at 14:24
1

I create similar dynamic CAMLQueries in SPServices itself. As much as people say they hate CAML, the reverse notation makes it relatively easy to build fairly complex queries programmatically. If you take a look at the way I build the CAMLQuery for the GetListItem query in SPCascadeDropdown to populate the child column, especially where the parent is a multi-select, you'll get the idea.

4
  • yeah that is cool. thanks Mark. love SPServices btw! i suppose I am still a beginner but using it heavily.
    – Justin
    Oct 16, 2012 at 14:19
  • yeah so building out the camlquery in advance is kind of the same approach i am taking with this, and bits of strings get create in .each loops and other query, etc. so that is right on the money. i guess for me is was just learning a little more CAML and how to structure it. i kind of like it too actually. thanks!
    – Justin
    Oct 17, 2012 at 13:20
  • @Marc, the plugin mentioned above would be a very valuable addition to SPServices. ;)
    – Paul T.
    Oct 18, 2012 at 3:11
  • Paul: since it looks like a useful plugin on its own, I think it should stand that way. SPServices can't handle everything without getting really bloated. Oct 18, 2012 at 11:20
1

Hard to say exactly what you are asking but I am guesing you need to construct a CAML query with a series of numbers, like a SQL "In" operator.

SharePoint 2010 has a CAML In operator, which is documented here: In element (Query).

In the Community Content section at the bottom, a user has inserted a code example. is this what you're looking for?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.