SharePoint for Developers: 3.3 Hands-on - querying lists

The best way to get data from SharePoint lists is to query them. Using queries you guarantee that only list items you really need are retrieved from database and constructed to objects by SharePoint. Most of the time you don't need the full collection of list items, so it is not point to ask all the items and then use only few of these. Let's see how we can query SharePoint lists.

 

NB! This blog is moved to gunnarpeipman.com

Click here to go to article

13 Comments

  • In our current project, we are using LINQ to query the Lists. It's going ok, but I don't really know whether it is considered a good practice or not. How is it compared to CAML in terms of performance and efficiency ?

  • Can you give me some examples about how do you use LINQ on SharePoint lists? When talking about performance you should always use CAML queries.

  • It looks similar to this:

    var x = from d in SPContext.Current.Web.Lists["YOUR_LIST NAME"].Items.GetDataTable().AsEnumerable()
    where d["ATTRIBUTE"].Equals(SomeValue)
    select d;

    it looks more like traditional SQL and you can even do joins.

  • Reggio, this code has hard performance loss on larger lists. Just take reflector and check out what is going on inside Items property of SPList. You may be surprised. :)

  • ...if does not work for you, try

  • 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 requestno

    I get 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?

  • Hi Gunnar,

    I'm trying to get this CAML to work. I'm doing this from within SPD and this is the selectcommand of a spdatasource. For some reason it doesn't do anything with the GroupBy tag. Is it even supported in this scenario ?

    Also, I want to format the output of this query to something like "MM/yyyy". Does CAML support Date Formatting in any way ?

    I would really appreciate your help on this.

  • Hi Juan and thanks for your question. CAML has very strict syntax of dates. You have to format dates appropriately so CAML can parse them.

  • Thank you so much.

  • Very informative one. I learnt how to query in SP2010. you have explained all the merits and demerits of it. Very good and useful tips.

    http://gloriatech.com/microsoft-sharepoint-services.aspx

  • When you have



    1977-12-13T23:00:00Z



    it should be



    1977-12-13T23:00:00Z



    Caused me some problems before :)

  • Hi
    Thanks for the post. I have quick question I need to write a query like this but i donot have access to code behind How can I achieve this with the SP designer 2007?
    I have a list and I need to select distict value of a perticular column.

    Thanks,
    Lakshmi

  • Your article was excellent and eurtdie.

Comments have been disabled for this content.