Thursday, June 12, 2008  by Marc Charmois - About Me / Previous Posts / Archives - Home

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

Labels: ,

1 Comments:

Anonymous Anonymous said...

Nice !!!!!!!!

SpQuery and CAML in Sharepoint, it is simple.

Try this too,
SpQuery and CAML in Sharepoint

October 6, 2009 at 2:35 PM  

Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home