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

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

share|improve this question

4 Answers 4

up vote 8 down vote accepted

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>
share|improve this answer
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 '12 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 '12 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. –  Anatoly Mironov Oct 16 '12 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 '12 at 13:17

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

share|improve this answer
    
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 '12 at 14:24

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

SP2010 has a CAML In operator, which is documented here: http://msdn.microsoft.com/en-us/library/ff625761.aspx

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

share|improve this answer
    
yeah IN was exactly what i needed. thanks –  Justin Oct 17 '12 at 13:20

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.

share|improve this answer
    
yeah that is cool. thanks Mark. love SPServices btw! i suppose I am still a beginner but using it heavily. –  Justin Oct 16 '12 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 '12 at 13:20
    
@Marc, the plugin mentioned above would be a very valuable addition to SPServices. ;) –  Paul T. Oct 18 '12 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. –  Marc D Anderson Oct 18 '12 at 11:20

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.