2

What is the best practice way to get all items from SPList(with query or without)

This is my ways :

  static void Main(string[] args)
    {
        try
        {
            using (SPSite mySite = new SPSite("http://sf-spsdev04/sites/gkk92"))
            {
                using (SPWeb myWeb = mySite.OpenWeb())
                {
                    SPList myList = myWeb.Lists["Employees"];

                    SPListItemCollection myItems = myList.GetItems();


                    //1st way
                    foreach (SPListItem item in myItems)
                    {
                        Console.WriteLine(item["Title"]);
                    }

                    //2nd way
                    foreach (SPListItem item in myList.GetItems())
                    {
                        Console.WriteLine(item["Title"]);
                    }

                    //3th way
                    foreach (SPListItem item in myWeb.Lists["Employees"].Items)
                    {
                        Console.WriteLine(item["Title"]);
                    }
                }
            }
        }
        catch(Exception ex)
        {
            throw new Exception(ex.ToString());
        }

What are yours ?

3
  • Did you find the given information in answer is appropriate?
    – Hardik
    Commented Jun 30, 2016 at 12:07
  • Yes of course :))
    – Goshky
    Commented Jun 30, 2016 at 12:50
  • 1
    Ok than close the question with accepting the answer so others can get benefit of it. :)
    – Hardik
    Commented Jun 30, 2016 at 12:51

2 Answers 2

6

Using SPList.Items property is bad practice. Each time the property is accessed, it requests all items with all fields from database. Instead you should always use SPList.GetItems().

  • SPList.GetItems(params string[]) - you can use it to load only the fields that you need. As far as I remember it'll only return items in the root folder of the list.
  • SPList.GetItems(SPQuery) - you'll need to create the query yourself. You should specify view fields and set scope to RecursiveAll if you want to get items from subfolders. You can completely skip the <Query> tag if you want all items.

I know it requires far more code tha using Items, but this is the right way to do it.

1
  • What is query that returned all items from SPList ?
    – Goshky
    Commented Jun 29, 2016 at 10:48
1

If it is just a case to get all items than I always use DataTable to get allitems using below code & do the operations.

static void Main(string[] args)
{
    try
    {
        SPWeb oWebsite = SPContext.Current.Web;
        SPList oList = oWebsite.Lists["Employees"];
        SPListItemCollection collListItems = oList.GetItems();

        DataTable dt = collListItems.GetDataTable();

        oWebsite.Dispose()
    }
    catch(Exception ex)
    {
        throw new Exception(ex.ToString());
    }
}
6
  • 3
    Why only data table? Why not other options? Commented Jun 29, 2016 at 8:45
  • I have updated the code in my answer. By this way your SPWeb & SPSite will be disposed after the operation & you can continue working on DataTable values.
    – Hardik
    Commented Jun 29, 2016 at 8:51
  • That is already done by OP. He has already used using syntax. So dispose is not an issue. Commented Jun 29, 2016 at 8:53
  • 2
    Yes you are right but in his case he must have to do operations with the open scope only but by using the DataTable you can work with close scope of SPWeb or SPSite.
    – Hardik
    Commented Jun 29, 2016 at 8:54
  • @Goshuy, Is above information is helpful to you?
    – Hardik
    Commented Jun 29, 2016 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.