Search code examples
.netsharepoint-2010sharepoint-2007sharepoint-api

Sharepoint 2010 SPListTemplate how to get list of fields?


I need to get all fields from my list template? How can i do this?

var web = site.OpenWeb();
var template = web.ListTemplates["SomeTemplate"];
template ... ???? -There is no method to get fields.

Solution

  • There is no built-in method to get all fields from a list template. The only way you can get the fields is by parsing the Schema XML of the list and getting all <Field> and <FieldRef> tags.

    Easier is to create a list instance, which you can query later on with the following examples.

    To get all fields from a list you can use the SPList.Fields Property, e.g. like so:

    foreach (SPField spField in myList.Fields)
    {
        //your code here
    }
    

    MSDN SPListItem.Fields

    You can also get all fields from a list item "in reverse" SPListItem.Fields Property. You might also be interested in this SO thread: Check if a List Column Exists using SharePoint Client Object Model?