3

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.
Ievgen
  • 4,261
  • 7
  • 75
  • 124

1 Answers1

2

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?

Community
  • 1
  • 1
Dennis G
  • 21,405
  • 19
  • 96
  • 133
  • 1
    This is so bad. Sharepoint disappointing me. I want programmatically update my list in site to match template.Becouse after redeployment of feature new fields not appear in lists. Maybe there is another way to do it? – Ievgen Feb 23 '11 at 12:06
  • 2
    Sorry, you cannot update existing list based on a new list definition. You can only update content types - see this post for more information: http://social.msdn.microsoft.com/Forums/en/sharepointdevelopment/thread/0cd38a26-476b-459c-a5b2-15267e9b8f55 – Dennis G Feb 23 '11 at 12:13