Prash's Blog

Programmatically add fields to SPList and its Default View November 13, 2009

Filed under: SharePoint — prazjain @ 2:33 pm
Tags: ,

I would show using a code snippet how to create a new SPList in SPWeb, add new fields to the list and then add those fields in the default view of the list.

Step through of the code:

  1. Create a new list in SPWeb
  2. Add new fields in the list
  3. Add the fields in the default view of the list

I have taken an example of creating a text field, you can as well use any data type i.e Checkbox, Choice etc.

Code Sample:


/// <summary>

/// Sets up the list

/// </summary>

/// <param name="web"></param>

private Guid SetUpHistoryList(SPWeb web)

{

Guid historyListGuid = web.Lists.Add("History", "History", SPListTemplateType.GenericList);

web.Update();

SPList historyList = web.Lists[historyListGuid];

historyList.Fields.Add("Information", SPFieldType.Text, false);

SPView defaultView = historyList.DefaultView;

defaultView.ViewFields.Add(historyList.Fields["Information"]);

defaultView.Update();

return historyListGuid;

}

Advertisements
 

One Response to “Programmatically add fields to SPList and its Default View”

  1. […] This code has been taken from the blog post Programmatically add fields to SPList and its Default View. […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s