Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11508

Get SPList Columns and the data

$
0
0

Hi there

Below source from console app is printing SPList Columns Internal name as well as the Data for the SPFields.

But it appears to be incorrect. Like to print Column name, then data for that column.

Example:

Title : SharePoint 2010

StartDate: 12/03/2013

I think that it is feasible without SPQuery or SPView, without hard coding column names to get the data.

Help in refactoring below source with better approach is greatly appreciated.

static void SharePointData()
        {
            using (SPSite site = new SPSite("http://intranet.contoso.com"))
            {
                using (SPWeb web = site.RootWeb)
                {
                    SPList list = web.Lists["Tasks"];
                    if (list != null)
                    {
                        SPFieldCollection fields = list.Fields;
                        SPListItemCollection items = list.Items;
                        Console.WriteLine("Printing Column names");
                        Console.WriteLine("@@@@@@@@@@@@@@@@@@");
                        foreach (SPField field in fields)
                        {
                            Console.WriteLine(field.InternalName);
                        }
                        Console.WriteLine("##################");
                        Console.WriteLine("Printing Columns Data");
                        foreach (SPItem item in items)
                            {
                                foreach (SPField field in item.Fields)
                                {
                                    Console.BackgroundColor = ConsoleColor.Blue;
                                    Console.WriteLine(item[field.InternalName]);
                                }
                            }
                    }
                    Console.ReadKey();
                }
                //Console.Read();
            }
        }


Viewing all articles
Browse latest Browse all 11508

Trending Articles