Mauro 的个人资料Taming the Jungle照片日志列表 工具 帮助

日志


7月12日

Down with CAML

CAML as used in Microsoft SharePoint is a “XML-based” query/update language. It’s my pet example of XML abuse. Or “just because you can do something it doesn’t mean you should”.
 
When compared to the SQL, CAML looses big time:
 
  • CAML is not a standard, you need to learn it
  • CAML is way harder to learn and use than SQL
  • CAML is very complex
  • I can’t see what day to-day stuff you can do in CAML and can’t do in SQL (there may exist some arcane example as the exception that proves the rule)
  • CAML is a “lower-level” abstraction than SQL. You have to build a three very similar to the one a compiler would after reading your SQL code.
After having to use it to write queries as part of a Sharepoint project, I toyed with the idea of writing a “SQL where-clause” to CAML converter. So I could write a simple, SQL-like WHERE clause and it would generate the corresponding CAML expression. Well, I didn’t need to. My fellow MVP, Carlos Segura Sanz did just that:

So instead of writing this:

<Query>  <Where>    <Or>      <And>        <Eq>          <FieldRef Name="Column1" />          <Value Type="Text">Value1</Value>        </Eq>        <Eq>          <FieldRef Name="Column2" />          <Value Type="Text">Value2</Value>        </Eq>      </And>      <And>        <Eq>          <FieldRef Name="Column3" />          <Value Type="Integer">10</Value>        </Eq>        <IsNotNull>          <FieldRef Name="Column3" />        </IsNotNull>      </And>    </Or>  </Where>  <GroupBy>    <FieldRef Name="Column1" />  </GroupBy>  <OrderBy>    <FieldRef Name="Column1" />    <FieldRef Name="Column2" Ascending="True" />    <FieldRef Name="Column3" Ascending="False" />  </OrderBy></Query>
You just write this:
WHERE ((Column1 = "Value1") AND (Column2 = "Value2")) OR ((Column3 = 10)
AND (Column3 <> NULL)) GROUPBY Column1 ORDERBY Column1, Column2 ASC, Column3 DESC

He even has a DLL you can use in your projects to do this at runtime!
Thanks, Carlos.