Writing CAML Queries For Retrieving List Items from a SharePoint List

Introduction

The Collaborative Application Markup Language (better known as CAML) is an XML-based query language that helps you querying, building and customizing Web sites based on Windows SharePoint Services. The XML elements define various aspects of a WSS site.

In this first series of articles I will explain in detail how to build and execute CAML queries to retrieve list items from a SharePoint list. List items can be retrieved in several different ways: using the SharePoint object model, using the SharePoint Lists web service or even by using Powershell. Use the SharePoint object model when your code runs on the server (like f.e. when you’re developing a web part or an application page). Use the SharePoint Web Services when your code doesn’t run on the server where SharePointis installed, for example when you develop office clients or windows applications. Powershell, at the other side, can be used by administrators when they quickly need to retrieve some information. In any way the CAML query is the same.

Building the CAML

CAML is an XML-based query language. Its root element is Query. Within the Query element two other elements are possible but not required: the OrderBy element and the Where element.

The OrderBy element is the simplest one. It is used to sort the returning list items. You have to specify the field(s) on which you want to sort the items and the sort direction. The syntax looks as follows:

<OrderBy>
   <FieldRef Name='LastName' Ascending='False' />
</OrderBy>

The OrderBy clause is not required and you can specify one or more fields on which you want to sort. If you omit the Ascending attribute, your resulting rows will be sorted in ascending order. If you want to order in descending order you have to specify Ascending=’False’.

The Where clause is used to specify one or more filter criteria. This clause can be very simple but can end up being rather complex. In its most simple form you specify an operator, a field name for which you want to specify a criterion, and a value.

<Where>
   <Eq>
     <FieldRef Name='LastName' />
      <Value Type='Text'>Janssens</Value>
  </Eq>
</Where>

Operators

You have different operators:

Eq                     Equals

Neq                  Not equal

Gt                     Greater than

Geq                    Greater than or equal

Lt                      Lower than

Leq                     Lower than

IsNull                Is null

BeginsWith     Begins with

Contains          Contains

Fields

The FieldRef element can be any field of the list on which you want to execute the CAML query. If you use the Name attribute you need to specify the internal name of the field. But you can also use the IDattribute to specify the Guid of the field.

Value

The Valueelement specifies the value part of the criterion. The attribute Typeis optional and specifies the data type of the field you want to specify the criterion for. If omitted the data type is considered as being Text. In all other cases you have to specify the Type attribute. DateTime fields are a special case and will be described in more detail later in this article.

If the field type is a Lookup you need to specify the text value.For example you have an Employees list and the Country field is a lookup field referring to the Countries list. In that case an employee living in Belgium will have f.e. following value: #15;Belgium. If you have to query for the employees living in Belgium you will have to write your query as follows:

<Where>
    <Eq>
     <FieldRef Name='Country' />
     <Value Type='Lookup'>Belgium</Value>
  </Eq>
</Where>

You can find this not good coding practice because the name of the country can change in time. In that case you can also query on the id of the country specifying the LookupId attribute in the FieldRef element:

<Where>
  <Eq>
     <FieldRef Name='Country' LookupId='True' />
     <Value Type='Lookup'>15</Value>
  </Eq>
</Where>

If you want to specify two filter criteria you also have to specify a join operator And or Or.

<Where>
   <And>
      <Eq>
        <FieldRef Name='LastName' />
        <Value Type='Text'>Janssens</Value>
     </Eq>
     <Geq>
        <FieldRef Name='Age' />
        <Value Type='Number'>21</Value>
     </Geq>
   </And>
</Where>

If you want to specify more filter criteria you have to nest them in a very specific way:

   <Where>
    <And>
        <And>
           <Eq>
             <FieldRef Name='LastName' />
             <Value Type='Text'>Janssens</Value>
           </Eq>
           <Geq>
             <FieldRef Name='Age' />
             <Value Type='Number'>21</Value>
           </Geq>
        </And>
        <Lt>
          <FieldRef Name='Age' />
          <Value Type='Number'>60</Value>
        </Lt>
    <And>
   </Where>

For each extra criterion you have to add an extra join operator at the outside of the query and add the criterion at the end:

    <Where>
    <And>
       <And>
           <And>
              <Eq>
                <FieldRef Name='LastName' />
                <Value Type='Text'>Janssens</Value>
              </Eq>
              <Geq>
                <FieldRef Name='Age' />
                <Value Type='Number'>21</Value>
              </Geq>
           </And>
           <Lt>
             <FieldRef Name='Age' />
             <Value Type='Number'>60</Value>
           </Lt>
       </And>
        <Eq>
          <FieldRef Name='Country' />
          <Value Type='Lookup'>Belgium</Value>
        </Lt>
    </And>
   </Where>

Retrieving List Items with CAML using the SharePoint Object Model

If you need to retrieve items from a list when developing web parts, application pages or custom field types you can best use the SPQueryobject from the SharePoint object model. This object is located in the Microsoft.SharePoint namespace of the Microsoft.SharePoint.dll located in the Global Assembly Cache.

Instantiate the object as follows:

SPQuery qry = new SPQuery();

The most important property is the Query property, which needs to be set to your CAML query:

string camlquery = "<OrderBy><FieldRef Name='Country' /></OrderBy><Where>"
    + "<Eq><FieldRef Name='LastName' /><Value Type='Text'>Smith</Value></Eq>"
    + </Where>";
qry.Query = camlquery;

At this point you can execute the query on your list:

SPListItemCollection listItemsCollection = list.GetItems(qry);

A small remark with the GetItems method of the SPList instance: this method returns a collection of type SPListItemCollection. It is possible that it is easier working with a DataTable. In that case you can execute the query as follows:

DataTable listItemsTable = list.GetItems(qry).GetDataTable();

The query will not only return all list items that have their last name set to Smith, but also all columns of each list item. In cases you work with large lists it can be important to retrieve a subset of list items containing only the columns you need. In that case you will have to set the ViewFields property of the SPQuery object. You can do this by specifying all columns you want to see returned:

qry.ViewFields = "<FieldRef Name='FirstName' /><FieldRef Name='LastName' />";

This will return the first name and the last name of the retrieved employees, but also the system fields like the ID and the Created date.

The major disadvantage of the SPQuery object is that you can query only one list. If you want to query more than one list you will have to use the SPSiteDataQuery. More on this object in a later article because it earns special attention.

It is common knowledge by now but let me remind you that it’s always a good idea to use SPQuery to retrieve a subset of list items. You can loop through the list item collection to find the list items that match your needs but this will have a serious negative impact on the performance of your work.

Retrieving List Items with CAML using the SharePoint Web Services

If you are developing office clients or any other application that will not run on the server where SharePoint is installed you will need to use the SharePoint Web Services to retrieve (or update) information from SharePoint. If you want to query a list you will need to execute the GetListItems method from the Lists.asmxSharePoint web service.

Working with the SharePoint web services is different in Visual Studio 2005 then in Visual Studio 2008.

Retrieving List Items from the Lists.asmx using Visual Studio 2005

First you have to reference  the web service in your Visual Studio project. If you work with Visual Studio 2005 you have to add a Web Reference to theLists.asmx.

Then you have to instantiate the web service and pass the url of the SharePoint site, plus the location and name of the SharePoint web service. The SharePoint web services are all located in the ISAPI directory of the SharePoint 12 hive but are accessible from outside via the _vti_bin directory.

Initialize the Lists.asmx web service:

 ListService listsWs = new ListService.Lists();
listsWs.Url = siteUrl + @"/_vti_bin/lists.asmx";

Then you have to set the credentials. In case you don’t need to specify a username or password you can proceed as follows:

listsWs.Credentials = System.Net.CredentialCache.DefaultCredentials;

If you have to specify a user name and password:

listsWs.Credentials = new System.Net.NetworkCredential(userName, password);

If you also have to specify a domain, use the other overload:

listsWs.Credentials = new System.Net.NetworkCredential(userName, password, domain);

Then you are ready to call the GetListItems method, which requires the following syntax:

resultNode = _sharepointSite.ListsWSS.GetListItems(listName, viewName,
    queryNode, viewFieldsNode, rowLimit, queryOptionsNode, webID);

Not all arguments are required. Arguments like view name, row limit and web ID are optional.

The query node argument will contain the CAML query as explained in previous sections but need to be enclosed within a <Query> and </Query> tag:

XmlDocument camlDocument = new XmlDocument();
XmlNode queryNode = camlDocument.CreateElement("Query");
queryNode.InnerXml = "<OrderBy><FieldRef Name='Country' /></OrderBy><Where>"
    + "<Eq><FieldRef Name='LastName' /><Value Type='Text'>Smith</Value></Eq>"
    + </Where>";

The same applies to the ViewFields node:

XmlNode viewFieldsNode = camlDocument.CreateElement("ViewFields");
viewFieldsNode.InnerXml = "<FieldRef Name='FirstName' />"
    + "<FieldRef Name='LastName' />";

As you will have noticed, you also need a QueryOptions node. This node will be explained later in this series. If you have no QueryOptions to specify you can pass in an empty node:

XmlNode queryOptionsNode = camlDocument.CreateElement("QueryOptions");

Retrieving List Items from the Lists.asmx using Visual Studio 2008

But in case you are working with Visual Studio 2008 you will have to add a Service Reference to theLists.asmxweb service. If you don’t want to work asynchronously you have to uncheck the Generate Asynchronous Operations option in the Advanced dialog. When closing the dialog box Visual Studio automatically adds an app.config or web.config file. Open this file and locate the security section. The following XML is generated automatically:

<security mode="None">
    <transport clientCredentialType="None" proxyCredentialType="None"
        realm="" />
    <message clientCredentialType="UserName" algorithmSuite="Default" />
</security>

Replace it with the following:

<security mode="TransportCredentialOnly">
  <transport clientCredentialType="Ntlm"/>
</security>

Return to your code and instantiate the web service:

ListsWS.ListsSoapClient ws = new ListsWS.ListsSoapClient();

You also have to set the necessary credentials. If you can work with the default credentials you can write the following:

 ws.ClientCredentials.Windows.ClientCredential =
       System.Net.CredentialCache.DefaultNetworkCredentials;
 ws.ClientCredentials.Windows.AllowedImpersonationLevel =
       System.Security.Principal.TokenImpersonationLevel.Impersonation;

If necessary you can also pass user name, password and domain:

 ws.ClientCredentials.Windows.ClientCredential =
       new System.Net.NetworkCredential("Administrator", "secret", "U2UCOURSE");
 ws.ClientCredentials.Windows.AllowedImpersonationLevel =
       System.Security.Principal.TokenImpersonationLevel.Impersonation;

If necessary you can set the URL to the web service dynamically:

 ws.Endpoint.Address =
     new System.ServiceModel.EndpointAddress(
     "http://wss.u2ucourse.com/_vti_bin/lists.asmx");

Now you are ready to call the GetListItems method of the web service. There is no difference in calling this method from within Visual Studio 2005:

XmlDocument camlDocument = new XmlDocument();
 XmlNode queryNode = camlDocument.CreateElement("Query");
queryNode.InnerXml = "<OrderBy><FieldRef Name='Country' /></OrderBy><Where>"
    + "<Eq><FieldRef Name='LastName' /><Value Type='Text'>Smith</Value></Eq>"
    + </Where>";
 XmlNode viewFieldsNode = camlDocument.CreateElement("ViewFields");
viewFieldsNode.InnerXml = "<FieldRef Name='FirstName' />"
    + "<FieldRef Name='LastName' />";
 XmlNode queryOptionsNode = camlDocument.CreateElement("QueryOptions");
 resultNode = _sharepointSite.ListsWSS.GetListItems(listName, viewName,
    queryNode, viewFieldsNode, rowLimit, queryOptionsNode, webID);

The returned XML is rather complex and needs some special attention. I will come back on this in a later article.

Query Options

Executing a query is not only about CAML. When working with the SPQuery object you can set different properties to influence the returned list items. When working with the SharePoint web services, these options are translated into CAML and are part of the QueryOptions element.

RowLimit

Setting this property limits the number of rows returned in the result set.

When working with the SPQuery object:

qry.RowLimit = 10;

When working with the GetListItems method of the Lists.asmx web service:

XmlNode queryOptionsNode = camlDocument.CreateElement("QueryOptions");
queryOptionsNode.InnerXml = "<RowLimit>10</RowLimit>";

IncludeMandatoryColumns

When this Boolean property is set to True, the result set will not only return the columns as defined in the ViewFields property, but also the columns that you defined in the list as required.

When working with the SPQuery object:

qry.IncludeMandatoryColumns = true;

When working with the GetListItems method of the Lists.asmx web service:

XmlNode queryOptionsNode = camlDocument.CreateElement("QueryOptions");
queryOptionsNode.InnerXml =
    "<IncludeMandatoryColumns>True</IncludeMandatoryColumns>";

DatesInUtc

Setting this Boolean property specifies whether the query returns dates in Coordinated Universal Time (UTC) format. The syntax is similar to that of the previously described property.

ExpandUserField

When you omit this property or set it to false, user fields will return the login name of the user.

Setting this Boolean property to true  (or include it in the QueryOptions node), user fields are returned as follows:

Karine Bosch,#U2UCOURSE\karine,#karine@U2UCOURSE.COM,#,#Karine Bosch

The returned value includes the login name, e-mail address, Session Initiation Protocol (SIP) address, and title, when present, which causes a user field to behave as a multilookup field.  The syntax is similar to that of the previously described property.

There are some other query options to set but they will be explained in detail in the next section.

Special Types of Queries

Some special types of lists require more specialized CAML queries. In some cases it only affects the CAML but in other cases you have to set extra SPQuery properties. If you execute this query against the Lists web service it will affect another argument of the GetListItems method, i.e. the QueryOptions argument.

Building queries for working with Folders

A first variant I will explain is how you can work with folders and sub folders. A folder is a special list item on a list or document library. If you execute a standard CAML query you will end up with list items from the root folder.

If you want to query all folders and sub folders of a list or document library, you have to define extra query options. If you are working with the object model you have to set the ViewAttributes property of the SPQuery object as follows:

qry.ViewAttributes = "Scope='Recursive'";

If you work with GetListItems method of the Lists.asmx SharePoint web service, you have to define an extra node with the QueryOptions element:

XmlNode queryOptionsNode = camlDocument.CreateElement("QueryOptions");
queryOptionsNode.InnerXml = "<ViewAttributes Scope=\"Recursive\" />";

If you want to query a specific sub folder using the SPQuery object, you have to set the Folder property:

qry.Folder = list.ParentWeb.GetFolder("Folders DocLib/2008");

When working with the web services you have to do the following:

XmlNode queryOptionsNode = camlDocument.CreateElement("QueryOptions");
queryOptionsNode.InnerXml = "<Folder>Folders DocLib/2008</Folder>";

Building Queries for working with DateTime Values

Filtering on DateTime fields also requires some extra attention. First of all when querying for a specific date, you have to use the SharePoint datetime notation:

<Where>
   <Ge>
        <FieldRef Name="StartDate" />
        <Value Type="DateTime">2008-08-10T10:00:00Z</Value>
   </Ge>
</Where>

But in this case the date is hard coded and the time part will not be taken into account. This query will return all list items with a start date as of 10 August 2008, also those starting before 10 o’clock. If you want your query to take into account the time part, you have to use a special attribute IncludeTimeValue that you can set on the FieldRef element:

<Where>
   <Ge>
        <FieldRef Name="StartDate" IncludeTimeValue="TRUE" />
        <Value Type="DateTime">2008-08-10T10:00:00Z</Value>
   </Ge>
</Where>

This query will return all list items with a start date as of 10 August 10 o’clock.

As already said, this way the date is hard coded. If you want your query a bit more dynamic, you can always use the element Today.

<Where>
   <Ge>
        <FieldRef Name="StartDate" />
        <Value Type="DateTime"><Today /></Value>
   </Ge>
</Where>

Today will not take a time part into account. Unfortunately a Now element doesn’t exist.

You can also add or subtract a number of days from today’s date. In that case you have to add the Offset attribute to the Today element. The Offset attribute accepts a positive value for adding days and a negative value for subtracting days.

 <Where>
   <Ge>
        <FieldRef Name="StartDate" />
        <Value Type="DateTime"><Today Offset="10" /></Value>
   </Ge>
</Where>

As all this is pure CAML, the query is the same whether you work with the object model or the SharePoint web services.

Building Queries for Calendar Lists

Calendar lists also require a little more attention. A calendar list is based on the Event content type. Normal events can be retrieved with the usual CAML queries. It’s only when you start working with recurring events (events that occur once a day or twice a week or month) that you run into problems.

The Event content type defines a field named fRecurrence, which is set to 1 when a recurring event is created.

Defining a recurring event

Defining a recurring event

When the Calendar view is rendered a recurring event is split into a number of event instances, one for each recurrence.

Calendar view showing event instances for recurring event

Calendar view showing event instances for recurring event

The definition of the recurrence is XML stored in another field defined by the Event content type, i.e. RecurrenceData. An example of such a definition looks as follows:

<recurrence>
   <rule>
     <firstDayOfWeek>su</firstDayOfWeek>
     <repeat>
        <weekly mo="TRUE" we="TRUE" weekFrequency="1" />
     </repeat>
     <repeatForever>FALSE</repeatForever>
   </rule>
</recurrence>

Fortunately you don’t have to parse the XML yourself to get the actual instances of the recurring event. You can use a combination of CAML syntax and query options to achieve this.

When working with the SPQuery object you need to set the ExpandRecurrences property to true. You also have to specify a date in the CalendarDate property. This date will be used to compare the recurring event instances.

Next you have to add a DateRangesOverlap element to the Where clause of the CAML query. This element is used in queries to compare the dates in a recurring event with the date specified in the CalendarDateproperty to see whether they overlap. The CAML looks as follows:

<Where>
   <DateRangesOverlap>
      <FieldRef Name=\"EventDate\" />
      <FieldRef Name=\"EndDate\" />
      <FieldRef Name=\"RecurrenceID\" />
      <Value Type=\"DateTime\"><Month /></Value>
   </DateRangesOverlap>
</Where>

The <Month /> element is used to specify that all occurrences within a month need to be returned. You can also specify Day, Week or Year.

In case you need to work with the GetListItems method of the Lists.asmx web service, you translate the query options into XML:

XmlNode queryOptionsNode = camlDocument.CreateElement("QueryOptions");
queryOptionsNode.InnerXml = "<ExpandRecurrences>TRUE</ExpandRecurrences>"
 + "<CalendarDate>2008-08-12T10:00:00Z</CalendarDate>";

Read more about Calendar lists here: http://blogs.msdn.com/sharepoint/archive/2007/05/14/understanding-the-sharepoint-calendar-and-how-to-export-it-to-ical-format.aspx

This is it about retrieving List Items using CAML. Don’t hesitate to post your comments and questions below.

Twitter Digg Delicious Stumbleupon Technorati Facebook Email
  • jimboalba
    Excellent post about CAML. This helped me on working with web services between VS2005 and VS2008.
  • Nelty T
    Hi there,

    I'm wondering if you could help me. I installed the Vacation and Absence Request Template from Microsoft for Windows Sharepoint Services 3.0. Is there a way to filter the calendar view or the pending approval view to the department group? For example, Sally created a new time off request and she belongs to the Finance Department, I want to filter the view so that everyone that belongs to the Finance group could see each other requests and everyone else won't be able to see it.

    So far, I've managed to get to the CAML code behind my list through the Sharepoint Desdigner and modify the ListViewXML where statement to:
    <Where><Or><Membership Type="CurrentUserGroups"><FieldRef Name="Author"/></Membership><Eq><FieldRef Name="Author"/><Value Type="Integer"><UserID Type="Integer"/></Value></Eq></Or></Where>

    And when I go to the Calendar View, I can only see Sally, but not anyone else in Finance Department.
    I've been struggling with this and I would really aprreciate any help.

    Thank you,

    Nelty T
  • Vince Yotsukura
    This is a very helpful article! Can anyone tell me whether it is possible to insert a bitwise AND operator into a CAML query? I am trying to filter for certain bitwise values in an integer field and I'm looking for a way to incorporate this into my CAML query. The SQL equivalent of this part of the query would be:

    WHERE (DOCSTATUS & 8 = 0)
    AND (DOCSTATUS & 1 = 1)

    DOCSTATUS is the integer field in my SharePoint library.
  • rachel
    Hi Karine,

    In Building Queries for Calendar Lists, if I want to use SPD to build a workflow to update recurrencedata field in Calendar A when Calendar B is changed, how do I parse the xml without export to ical?

    Thank you!
  • Tarang Gupta
    in "Building Queries for working with DateTime Values" section IncludeTimeValue='TRUE' attribute is part of value tag, not fieldref tag.
  • "Today will not take a time part into account. Unfortunately a Now element doesn’t exist."

    However, you can use undocumented IncludeTimeValue='TRUE' to include take time part into account.
    http://www.wirwar.com/blog/2008/07/30/sharepoin...
  • Scot Burbacher
    What if you wanted to query based on the active user? Something to the effect of:
    <Where>
    <Eq>
    <FieldRef Name='Employee' />
    <Value Type='Text'>[Current User]</Value>
    </Eq>
    </Where>

    This particular syntax doesn't work, but I imagine there's got to be a way to filter a list based on the current user. Please help!
  • dsoutter
    Great Article!! I have done this from an InfoPath Form in the past, but not using Visual Studio. I found this very helpful when trying to build CAML queries for adding/updating items is SharePoint lists (using the UpdateListItems method) when developing Workflows using Visual Studio.

    See this also - SharePoint CAML
  • sarangasl
    Nice !!!!!!!!

    Try this too,
    SharePoint Lists and more
  • sweha
    Hi,

    I am trying to get listitems using the same method. I have a order no field in the list and some other fields are Customers which is a lookup and date which is a calculated column and Agency which is a text.

    I am querying by order no.

    I can retrieve Agency as it is a simple text, but I can't seem to get Customer name and Date as one is a lookup and one is a date. It is my necessity that I query by order number only and for each order number, get the rest of the data for each listitem.

    Can you share your ideas please?
  • sandeepnahta
    Offset option didn't work . it has to be OffsetDay='-7'
  • suzanne
    I solved my problem by using U2U CAML builder. Great app!!!
  • suzanne
    I installed the Employee training site template and have lists associated with that template. One of the lists is Past Registrations where items from another list are moved by workflow after an event. Weird thing: a query of this list (with no conditions) does not return all list items. What can I do?
  • scvinod
    Hi Ms.Karine,

    I'm not able to retrieve a column from a sharepoint list that has some empty values.I'm getting this error "Object referrence not set to instance of an object".Can anyone please help me out.
  • boxbob
    did you ever get the resolved, I read that it's part of the web service design to reduce the amount of xml. seems lame. at least provide an option to return all fields
  • sharepoint guy
    you need to use <Today OffsetDays="1" /> instead of <Today Offset="1"/> to make query work.
  • scvinod
    Hi all,

    Is it possible to write an IF function in a CAML query or is it possible to use a valure returned from a function???
  • scvinod
    Hi Karine and others,

    Hey guys i have column called "Managers" in my list in which i have to more than 1 value like this "vinod:sc" which means vinod and sc are the 2 managers.Is it possible in my CAML query to get the 2 values separately ???
  • bamarob55
    I'm having trouble getting all the fields to return. I'm having trouble with any fields that have a space in the field name. I.e.:

    ndViewFields.InnerXml = "<FieldRef Name='Last Name'/><FieldRef Name='Author'/><FieldRef Name='Department'/><FieldRef Name='Crew'/>"

    works fine to return Author, Department, and Crew, but the Last Name field doesn't get returned. Any tips you can provide would be much appreciated.

    I'm using the "Retrieving List Items with CAML using the SharePoint Web Services" method.

    Thanks,

    Robert
  • scvinod
    @bamarob55

    that must becoz the internal field name of of the column "last Name"would different from what you see.Please get the internal field name by this code "list.Fields["Manager"].InternalName.ToString();" and then put the name tht u get in the CAML query.
  • asdasd
    Not able to get Recurrence Data in the response even after adding <ExpandRecurrences>TRUE</ExpandRecurrences>
    Any suggestions
  • vinod
    Hi Karine n others,

    Is it possible to write a CAML query within a CAML query.Please help me out.Thank you.
  • Zarko
    I am building a custom workflow in SharePoint and I ise CAML query to retrive data about status of the task. However, at one point, an error started to arise: One or more field types are not properly installed... I checked internal nemes and everything is fine. The strangest thing is that the same code worked before. What can be the cause? I would extremely appriciate your help.

    Thanks in advance
  • Excellent post, Karine!

    This is one of the best posts that I've read on CAML. It's straightforward yet thorough. Unlike other articles that I've read about CAML, this one covers all scenarios.

    Thank you very much!
  • Tim Landgrave
    Is there any (easy) way to call Lists.ASMX from an Infopath 2007 form and use the results in a dropdown?
  • Dylan
    Hi Boske,

    This is the only place that I found this useful information about getting Calendar data throug lists.asmx webservice. But I got a problem and not sure why it behave this way. Basically, I was able to get calendar data from this month with all recurrence events. But when I click Next month, I put this code like you suggest in QueryOptions to set CalendarDate to the next month but it doesn't seem to effect anything. It's always return me the all events for the current month and upto the same day next month (e.i: if I query from today 4/21/2009, I'll get everything from April upto May 21) I can't seem to get further. Please help...

    Thanks



    XmlNode queryOptionsNode = camlDocument.CreateElement("QueryOptions");queryOptionsNode.InnerXml = "<ExpandRecurrences>TRUE</ExpandRecurrences>" + "<CalendarDate>2009-05-21T01:00:00Z</CalendarDate>";
  • Florian
    Hello, I m lookin to retrieve the direct link to a document in my CAML query result, who can I do that??
    I'm usintgSPSIteDataQuery accross multiple SubSite and use the SPWeb.GetSiteData(myCamlQuery)
    thanks in advance!
  • Andrew
    Hello. Thank you for the helpful article.

    I'm curious - has the method for expanding recurring calendar events over webservices been tested?

    I have been able to get it working "server-side", as described in the iCal article; however - no combination of options seems to work for expanding recurring events using the web services interface.
  • John Dieter
    How can you debug SPException "Cannot Complete this action" ? My query looks fine. But if I try to access myListItemCollection.Count I get the error
  • sayvidyasagar
    Excellent introduction on CAML query creation. In real time I am facing some prolem, can any one please suggest me the solution. In SharePoint list I have 5 columns: Title, Description, submitter, status and Published Date. My query should be like:
    (Status = ‘Published’ AND Published Date=’10/10/2008’) AND (title contains ‘xyz’ OR descrition contains ’abcd’ OR submitter conains ‘aaa’)
  • agnel
    My list contains folders as list items.Can anyone help how to sort folder in list.
  • Akshaya
    Hi

    I use the U2U caml builder gor queries and I use the shpt web services to retrieve data.

    I am facing a problem. When I want to get the data from a particular view of the list, I pass the view name of the getlistitems method and it returns me the rows belonging to that view only.

    However, when I try getting the data from a view based on a query, the query returns the data from the all items view rather than from that particular view only, and as a result I get extra rows, what should i do to prevent this?

    Thanks a lot

    Akshaya
  • john
    Thank you, thank you. I wish I had saw this post last week. This cleared up a lot of questions!
  • martin
    ...also, be sure that authentication as enabled at endpoint... (lost a good hour there)
  • this is the best, simple, attractive, satisfying, shortest article i have read in my life. you have an amazing way!!!!
  • masha
    thank you for great article=)
  • Santro
    Hello Karine,
    Thanks a lot for the wonderful post. I read the full post and just got every thing which I was looking for.
  • Heiko Hatzfeld
    Thanks for a great post.

    Very interesting and informative overview.
  • There exist a number of community tools that help you generate your CAML. Another tool is the U2U CAML Query Builder. Besides a user interface in which you can design your queries, you can also use the DLLs from within your code to generate the necessary CAML.
    Karine
  • vin
    Hi Karin,

    After successfully retrieving the records using the GetListItems method, how can I use the result so I can insert it in a SQL table?

    Thanks in advance
  • Garth
    Karin,
    Thank you for this posting. I've been doing a fair amount of CAML lately and have used this page as a starting point for alot of my work.

    Thanks
  • Great article!

    I would like to point out a great tool by John Holliday called CAML.net which helps write the CAML code for you and prevents quite a lot of typo errors. It is available on CodePlex at http://codeplex.com/camldotnet/

    .b
  • wow, great introduction to CAML from the CAML girl herself :)
blog comments powered by Disqus