Thursday, June 12, 2008

Improve readibility of CAML in SPQuery using C# and Visual Studio

Introduction
If you want to break down line in C# while writing CAML to define an SPQuery query, you maybe try to look for C# continuation line character, the equivalent of _ in VB .net. Referring to C# Language Specification, C# doesn't need one. A line continues until a ; is reached.
However, If you try to break line in a string in Visual Studio you will have a "new line in constant " exception.
The solution is to use @ character before your string. Code sample
 SPSite mySite = new SPSite("http://frev1149:8080/");
 SPQuery myQuery = new SPQuery();
 myQuery.Query= @"
   <Where>
      <Eq>
        <FieldRef Name='StateMonthReport'/>
        <Value Type='Text'>Refusee</Value>
      </Eq>
   </Where>
   ";
 SPList myList = mySite.OpenWeb().Lists["Transmises"];
 SPListItemCollection myItems = myList.GetItems(myQuery);

1 comment:

Anonymous said...

Nice !!!!!!!!

SpQuery and CAML in Sharepoint, it is simple.

Try this too,
SpQuery and CAML in Sharepoint