Results 1 to 5 of 5

Thread: How to use CAML query with SharePoint 2010

  1. #1
    Join Date
    Jan 2011
    Posts
    35

    How to use CAML query with SharePoint 2010

    I have installed the Microsoft Office 2010 on my machine and I want do something with the SharePoint. I hope that there are some developers over here who must be having some knowledge about using the CAML query along with the SharePoint 2010. I have tried it but it ended with the following error. Please help me, I am newbie!

    Description: An unhandled exception occurred during the execution of the current web request. Check the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.ArgumentException: Value does not fall within the expected range.

  2. #2
    Join Date
    May 2009
    Posts
    637

    Re: How to use CAML query with SharePoint 2010

    A major development of SharePoint 2010 as compared to its predecessor is the introduction of the joints in CAML queries. Indeed, if you've used SharePoint CAML in 2007, you will certainly realize that it was impossible to run through the joints CAML queries. This severely limited the use of queries involving multiple tables. With SharePoint 2010, it is now possible to use join and Projected fields to perform joins between tables. We will develop a Visual Web Part, which handles the execution of different CAML queries and display the result to see the full power of the joints (for those unfamiliar with this concept in SQL). Either you're targeting the wrong site with AllWebs [""], either the list has a name different from the value of the variable list. If your list is in the root site collection site, oSiteCollection.RootWeb seems to me more suitable than oSiteCollection.AllWebs.

  3. #3
    Join Date
    May 2009
    Posts
    543

    Re: How to use CAML query with SharePoint 2010

    In CAML, it is possible to perform such joins INNER LEFT or type. The difference between these two types of joints is very important. We see this in the explanation of our example. Come back to your class and add the following directive:
    Code:
     using Microsoft . SharePoint ;
    Then state the following function:
    Code:
     private void AfficherMonumentEtVille ( string type) { }

    The purpose of this function will display all the monuments as well as information about their city and their country. The type parameter will allow to define whether the join type is INNER or LEFT and allow us to understand the difference. The first thing to do in this function is to retrieve a reference to the list and create an object SPQuery to perform the query:
    Code:
     SPList list = SPContext . Current . Web . Lists [ " Monuments " ] ; SPQuery query = new SPQuery () ;

    We will base ourselves on the list because they are monuments to the information of monuments and information related to this monument that we want to retrieve. We will now define the property Join the petition. It is in this property, we will define the format of the join. We will directly display the code Join and we will explain below:
    Code:
     query . Joins = string . Format ( " <Join   Type='{0}'   ListAlias='VillesList'> " + " <Eq> " + " <FieldRef   Name='Ville'   RefType='Id'   /> " + " <FieldRef   List='VillesList'   Name='ID'   /> " + " </Eq> " + " </Join> " , type) ;
    As we have said, the property will allow Join contain code showing how CAML or the joints will be performed. Here, for starters, we will make a single joint. Basically, we will join the table to table Monuments Cities City field through the table monument. Thus, we will recover the information of each monument as well as information about the city in which these monuments are located.

  4. #4
    Join Date
    Apr 2009
    Posts
    569

    Re: How to use CAML query with SharePoint 2010

    To do a CAML query in a SharePoint list you can use the class SPQuery. Here's an example.
    Code:
    try { SPSite site = new SPSite("serveur_url"); SPWeb web = new site.OpenWeb(); SPList list = web.Lists["votre_liste"]; SPQuery query = new SPQuery(list.DefaultView); query.Query = " Une Valeur "; SPListItemCollection items = list.GetItems(query); foreach (SPListItem item in items) { Console.WriteLine(item["Title"])); } } catch(SPException SpEx) { //Traitement d'erreur }
    Here we use the operator "<Eq>, we could eg use <Contains a search corresponding to a SQL LIKE. You must clearly specify the internal name of the field list and not the title "Title". There are also graphical tools to generate CAML queries. A little research on CAML Query Builder on google guide you to this type of tool.

    To specify which fields to include in a CAML query
    Code:
     try { SPSite site = new SPSite("serveur_url"); SPWeb web = new site.OpenWeb(); SPList list = web.Lists["votre_liste"]; SPQuery query = new SPQuery(list.DefaultView); query.Query = " Une Valeur "; SPListItemCollection items = list.GetItems(query); foreach (SPListItem item in items) { Console.WriteLine(item["Title"])); } } catch(SPException SpEx) { //Traitement d'erreur }
    In the previous example, we specify the view "DefaultView". We could have used a view defined in the list. It is also possible to specify fields to include as follows:
    Code:
     query.ViewFields = "<FieldRef Name='Champ1'/><FieldRef Name='Champ2'/>....";

  5. #5
    Join Date
    Nov 2008
    Posts
    1,054

    Re: How to use CAML query with SharePoint 2010

    CAML.NET. This tool is intended for the (many) redundant tasks when dealing integrate or develop for SharePoint, to know how to write CAML queries. I hear you tell me that there are already tools to ease this task as CAML Query Builder and Stramit SharePoint 2007 CAML Viewer but this tool to address many cases where previous tools are useless, namely:
    • CAML is hard to read, once the request is generated, any change or substitution can quickly lead to major problems due to typos difficult to detect
    • CAML is not strongly typed
    • Other tools required to create the necessary environment for the application, not CAML.NET

Similar Threads

  1. Replies: 1
    Last Post: 18-11-2011, 01:13 AM
  2. pdf not opening from sharepoint 2010 with acrobat X pro
    By Upkeerat in forum Windows Software
    Replies: 5
    Last Post: 13-11-2011, 08:50 PM
  3. New event receivers for SharePoint 2010
    By Tynan in forum MS Office Support
    Replies: 4
    Last Post: 25-02-2011, 08:21 AM
  4. Replies: 4
    Last Post: 07-02-2011, 06:53 AM
  5. SharePoint 2010 Top 10 New Features
    By Brexton in forum MS Office Support
    Replies: 5
    Last Post: 29-01-2011, 08:26 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,485,989.45843 seconds with 17 queries