7

So, here is some lines of code that I write to retrieve data from SharePoint list using CAML query.

string qryCaml = string.Format(
    @"<Where>
          <And>
             <Eq>
                <FieldRef Name='{0}'/>
                     <Value Type='Text'>{1}</Value>
             </Eq>
             <Eq>
                 <FieldRef Name='{2}'/>
                      <Value Type='Text'>{3}</Value>
             </Eq>
           </And>
      </Where>", QueryFieldName1, QueryValue1, QueryFieldName2, QueryValue2);

As you can see in the code, I declare all the Value Type as 'Text'.

So, even though the column has a Value Type of Choice/Lookup, the code will still run the value type as Text, and things goes smoothly without any issues.

My questions are, is it better to specified the value type? and is there any performance difference between specifying the value type than just stated it as 'Text'(Maybe faster queries)?

I'm very new to SharePoint and I want my code to be as dynamic as possible, that's why I put all the Value Type as Text.

Thank you.

1 Answer 1

10

You have several options for your type attribute in your value element. You can use Boolean, Choice, Currency, DateTime, Guid, Integer, Lookup, Note, Text, User. These type attributes are required, and can't be omitted.

As regards to performance, CAML itself is faster than say LINQ, which at first need to be converted to CAML and then query the database. But regarding your question on value type, I havn't noticed any performance diffences, nor have I read of any performance differences between different types. My experience tells me what really effects performance is the number of data being returned - which has the most impact. If you only need three rows, never query for a thousand as in query.RowLimit = 1000;.

SPQuery query = new SPQuery();
query.Query = "<Where>
                   <And>
                       <Eq>
                           <FieldRef Name='" + QueryFieldName1 + "'/>
                           <Value Type='Text'>" + QueryValue1 + "</Value>
                       </Eq>
                       <Eq>
                           <FieldRef Name='" + QueryFieldName2 + "'/>
                           <Value Type='Text'>" + QueryValue2 + "</Value>
                       </Eq>
                   </And>
               </Where>";
query.ViewFieldsOnly = true;
query.RowLimit = 3;

Another aspect on performance is the number of rows present in the list. It's faster to query a list of 1'000 elements than a list of 1'000'000 elements. Still, having the expected data type is never wrong, and if you do know the data type in the first place - why not use it.

Reference:

1
  • 1
    Thanks man for the answer, suppose it's better to specified the Value Type rather than using 'Text'.
    – kamal
    Jun 20, 2013 at 6:50

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.