Wednesday, November 26, 2008

Tip! Yet another CAML query tool

CAML (Collaborative Application Markup Language) is an XML based markup language used with the family of Microsoft SharePoint technologies (Windows Sharepoint Services and Office SharePoint Server). Unlike plain XML, CAML contains specific groups of tags to both define and display (render) data. [wikipedia]

With other words CAML defines various aspects in SharePoint like: views, fields, site definitions, ...

With CAML you can also define queries, which is useful to extract some data from SharePoint lists. Now... writing a simple CAML query isn't that difficult. Given the following example:





It can be difficult if you have more conditional clauses:







Now I probably hear you say: "Sven, what are you talking about? I use the CAML query builder from U2U to construct my queries. That's pretty easy!". I know... that tool is really helpful but what if you have to construct your query dynamically? You can build a flexible query class to simplify the process of creating queries, but it's not necessary. Carlos Segura Sanz (MVP) did this already with his "Yet another CAML Query tool (ala SQL).

With that tool you can construct your query ala SQL. Later on, you can transform the SQL syntax to CAML syntax. Example:
using IdeSeg.SharePoint.Caml.ParseQuery;

string query = @" WHERE ContentType='sharepoint'
AND Firstname = 'Sven'
AND Lastname = 'Gillis'
GROUPBY Title DESC
ORDERBY _Author"
;
try
{
string camlQuery = CAMLParser.Query(query);
SPList list = null;
SPQuery query = SPQuery();
query.Query = camlQuery;
SPItemCollection col = list.getitems(query);
}
catch (ParserException ex) {}
Quite easy, isn't it?

Use "=null" as CAML IsNull operator and "<>null" as IsNotNull also you can use dates enclosed into sharp characters #01/01/1990# the sql Like operator is replaced by CAML Contains and the at "@" operator for the CAML BeginsWith. Also support parentheses for operator precedence.

Oh... btw if you download YACAMLQT you can also use a windows application to support your development. It looks something like this:


















I recently used it in a project and it works like a charm!

Resources:

Saturday, November 22, 2008

WCM: Publishing Site Definition

Creating a new publishing website typically begins by creating a new publishing site collection based on your custom publishing site definition. Nevertheless, it is not necessary to create a custom one because you can also start with the OOTB Publishing Portal Site Definition and then remove the parts that are superfluous. It is quite obvious that this method is not ideal. Starting off with the Blank Site Definition and activating the publishing features is a better option because then you have a quite clean environment to start with.

Probably the best option is to create a Publishing Site Definition from scratch. This way, you "almost" have full control of the provisioning process. Below a few examples which elements you can specify:
  • The welcome page url and the appropriate (custom) pagelayout
  • Automatically activate masterpage, styles, ... features
  • Available web templates
  • Automatically activate the ViewFormPagesLockDown feature - A special feature that restrict access for anonymous users to published content only
  • Alternate CSS url
  • And so on...
In one of my next posts I'll show you how to set a welcome page with your own pagelayout with a custom content type (specified in your publishing site definition).

Saturday, November 8, 2008

Off-topic: E.T.A.

Marvin has the most boring job - ever. But all is not as it seems...