0

I am having Sql Query like

// sTitle is my input string
 string sTitle='something';
 Select Title From EmployeeList WHERE Title like '%sTitle%' or 0=sTitle OrderBy Title Desc;

How do i Build it in CAML Query? In CAML Query we can match with the fieldNames but i need to compare with integer or some constant with my InputString

And one more thing How do i achieve the Descending concept in CAML query

2 Answers 2

0

Here is a sample CAML Query:

<Query>
   <Where>
      <Contains>
         <FieldRef Name='Title' />
         <Value Type='Text'>1234</Value>
      </Contains>
   </Where>
   <OrderBy>
      <FieldRef Name='Title' Ascending='False' />
   </OrderBy>
</Query>

In CAML Like may be done using Conatins. Moreover, OrderBy is used for sorting. I would suggest you to use CAML Query builder. It will help you understand the concepts of CAML.

5
  • I need to compare my input string with some constant as we compare in sql ... Is it possible?
    – Sarath
    Feb 9, 2015 at 8:26
  • CAML Query compares the field values of a list item. So if your constant is stored in some field, you can compare the input string with that. Feb 9, 2015 at 8:28
  • ya i understand that.. but something we are querying in sql, sameway I am trying to acheive it on sharepoint... So for implementing my case what would be ur advise?
    – Sarath
    Feb 9, 2015 at 8:33
  • @Sarath, you can upvote or mark the reply as answer if it helped resolve your issue. Same is true for teylyn's answer. Feb 9, 2015 at 13:23
  • For Vote I need 15 points.. And ur answers are satisfied half of t content oly..
    – Sarath
    Feb 9, 2015 at 14:04
0

One way to generate the CAML query is to start with a SharePoint list view.

If you can use the view dialog to define the filter and the sort, then you can grab the corresponding CAML with SharePoint Designer.

In this case, you may want to create a view on the EmployeeList. Filter by Title with "contains" and the desired string.

Use the Sort section to sort descending by the desire value.

Once the view shows the correct results, open the page with SharePoint Designer, select the web part and in code view look for the Query tag near the top of the web part definition. That's your CAML query.

You can also pass filter parameters into the query with either a filter web part or a URL parameter. If you need details for that, pipe up.

4
  • little info. But I need to compare my input string with some constant as we compare in sql ... Is it possible?
    – Sarath
    Feb 9, 2015 at 8:26
  • Yes, it is possible. Pass in the constant as a URL parameter. Create a parameter in the XSLTListViewWebPart that reads the URL parameter. Then filter the list by the parameter.
    – teylyn
    Feb 9, 2015 at 8:30
  • ya.. For Descending, Is there any way in camel queries?
    – Sarath
    Feb 9, 2015 at 8:34
  • Yes. Look at the "OrderBy" in a CAML query, like the one posted by @Nadeem. Can you see where it says Ascending='FALSE' -- Can you interpret what that might mean?
    – teylyn
    Feb 9, 2015 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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