Get the Names of all Fields in a SharePoint List

Below Code block explains how to Get the Names of all fields in a SharePoint list Programmatically.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using Microsoft.SharePoint;  
  6.   
  7. namespace SharePoint  
  8. {  
  9.     class ListFields  
  10.     {  
  11.         static void Main()  
  12.         {  
  13.             SPSecurity.RunWithElevatedPrivileges(delegate()  
  14.                 {  
  15.                     string siteUrl = "http://abhay-pc:1212";                              
  16.                     SPSite site = new SPSite(siteUrl);  
  17.                     Console.WriteLine(site.ToString() + "\n");  
  18.                     SPWeb web = site.OpenWeb();                     
  19.                     SPList list = web.Lists["EmployeeDetails"];  
  20.                     Console.WriteLine(list.ToString() + "\n");  
  21.                     foreach (SPField f in list.Fields)  
  22.                     {  
  23.                         Console.WriteLine(f.StaticName.ToString() + " - " + f.TypeDisplayName.ToString() + "\n");  
  24.                     }  
  25.                 });  
  26.             Console.ReadLine();  
  27.   
  28.         }  
  29.     }  
  30. }  
 
Ebook Download
View all
Learn
View all