CAML in the Object Model

CAML (Collaborative Application Markup Language) is an XML-based query language that helps you querying, building and customizing Web sites based on Windows SharePoint Services. Today I would like to give you a brief overview of some of the methods that allow the use of CAML. Some examples are as listed below:

 

         Adding columns to a list.

         Retrieve field schema in a list.

         Use SPQuery to filter items based on some condition.

         Use SPSiteDataQuery to query list items across multiple lists, which may be located in multiple Web sites in the same Web site collection.

 

Adding columns to a list

 

Ok to start off I have a list named “Assignments” based on the tasks template, on my SharePoint root site, by default the list has the columns as shown in the figure below.

 

 

 

 

I want to use CAML and add a new column named “Rating” to the list. I can do this easily like this.

 

 

SPSite MySite = new SPSite("http://usmanz");

SPWeb MyWeb = MySite.RootWeb;

 

SPList TargetList = MyWeb.Lists["Assignments"];

 

SPFieldCollection fields = TargetList.Fields;

 

string fieldCAML = "<Field Type=\"Number\" DisplayName=\"Rating\" Name=\"Rating\"></Field>";

 

fields.AddFieldAsXml(fieldCAML); 

 
Seems simple enough? ok Now lets look into how can we filter out list items based on some condition using CAML. I will use the same assignments list for this example.
 
//Query to filter out list items where Rating = 5
 

SPQuery query = new SPQuery();
query.Query =
"<Where><Eq><FieldRef Name=’Rating’/><ValueType=’Text’>5</Value></Eq></Where>"; SPListItemCollection items = list.GetItems(query);

 //Query to filter out list items where Rating = 5 and
//also order the items in ascending order based on the modified column

 SPQuery query = new SPQuery();

query.Query = "<Where><Eq><FieldRef Name=’Rating’/><ValueType=’Text’>5</Value></Eq></Where><OrderBy><FieldRef Name=’Modified’ Ascending=’TRUE’/></OrderBy>";

SPListItemCollection items = list.GetItems(query);

Well that again was quite simple, now lets say I want to query across multiple lists, which may be located in multiple Web sites in the same Web site collection. For this we will use the SPSiteDataQuery Class. Its quite simple to use.
 
 
SPSiteDataQuery query = new SPSiteDataQuery();
query.Webs = "<Webs Scope=\"Recursive\">";
 
//only look through tasks lists. 107 is for a list based on tasks template
 
query.Lists = "<Lists ServerTemplate=\"107\" />";
 
//sets the inner XML that describes the view fields used in the query.
 
query.ViewFields = "<FieldRef Name=\"Title\" />“ ;
 
//get only the items where the start date is equal to  7/01/2006
 
query.Query= "<Where>" +
  "<Eq>" +
    "<FieldRef Name=\"StartDate\" />" +
    "<Value Type=\"DateTime\">7/01/2006 </Value>" +
  "</Gt>" +
"</Where>";

 
DataTable dt = rootWeb.GetSiteData(query);
 
 
Advertisements

16 Comments »

  1. Unknown said

    Hi,Do you need digital signage, digital sign, ad players and ad displays? Please go Here:www.amberdigital.com.hk(Amberdigital).we have explored and developed the international market with professionalism. We have built a widespread marketing network, and set up a capable management team dedicated to provide beyond-expectation services to our customers.

    amberdigital Contact Us
    E-mail:sstar@netvigator.com
    website:www.amberdigital.com.hk
    alibaba:amberdigital.en.alibaba.com[acifd

  2. Unknown said

    Hi,Do you need advertising displays, digital signages, advertising player and LCD displays? Please go Here:www.amberdigital.com.hk(Amberdigital).we have explored and developed the international market with professionalism. We have built a widespread marketing network, and set up a capable management team dedicated to provide beyond-expectation services to our customers.
    amberdigital Contact Us
    website:www.amberdigital.com.hk
    alibaba:amberdigital.en.alibaba.com[edfbchddjiidhj]

  3. Unknown said

    Hi,Do you need digital signages, advertising displays, digital sign, advertisement displays and advertising players? Please go Here:www.amberdigital.com.hk(Amberdigital).we have explored and developed the international market with professionalism. We have built a widespread marketing network, and set up a capable management team dedicated to provide beyond-expectation services to our customers.
    amberdigital Contact Us
    website:www.amberdigital.com.hk
    alibaba:amberdigital.en.alibaba.com[idgbjfjjbccjij]

RSS feed for comments on this post · TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: