Skip to main content

Why is What are the best practices way to get all items from SPList

WhyWhat 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());
        }

WhoWhat are yours ?

Why is the best practices way to get all items from SPList

Why 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());
        }

Who are yours ?

What are the best practices to get all items from SPList

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 ?

Source Link
Goshky
  • 2.1k
  • 4
  • 25
  • 49

Why is the best practices way to get all items from SPList

Why 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());
        }

Who are yours ?