How to get Column names from a SharePoint list?

11,690

You want to get the DefaultView for your list (SPList.DefaultView), then inspect the ViewFields element.

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spview.viewfields.aspx

Share:
11,690
Abe Miessler
Author by

Abe Miessler

Software Engineer who works with Javascript/Node.js, Python, C#, Go, SQL Server, MongoDB, MySQL and a whole lot more. I enjoy learning new technologies when they are the best tool for the job. I usually fill the role of a full stack engineer but always seem to enjoy working with data the most. 80th recipient of the Gold SQL badge 50th recipient of the Gold SQL Server badge Hobbies include web application security and machine learning.

Updated on July 20, 2022

Comments

  • Abe Miessler
    Abe Miessler almost 2 years

    I created a List and added three columns to it. When I look at the list in my "List Settings" page, I see the three columns I created and some default ones as well (Title, Created By and Modified By). When I view the Items in the list I just see the columns I created.

    My issue is that when I try to get a list of columns for my list from code I get a whole slew of other ones that I don't see anywhere (Version, Attachments, Item Child Count, etc.)

    Here is the code I am using to get this list:

            List<string> visFields = new List<string>();
            foreach (SPField field in myList.Fields)
            {
                if (!field.Hidden)
                {
                    visFields.Add(field.Title);
                }
            }
    
            return visFields;
    

    Is looking at the fields the wrong way to go about this? How can I get the same list of columns that is displayed when you view the items in the list?

  • Abe Miessler
    Abe Miessler about 13 years
    Perfect, this is just what I was looking for.