Tuesday, June 14, 2011

3 ways for adding new column in SharePoint list !

In this post we will see 3 different methods for adding columns in SharePoint list or document library or discussion board.

using browser

Go to the list which you want to add column

On the page that displays the list, click list’s settings and create columns.

Type a name for the column, choose the column type and click OK.

using Object model

SPSite site = new SPSite(siteUrl);
SPWeb web = site.OpenWeb();
site.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = true;
SPList list = web.Lists["mylist"];
SPFieldText fldName = (SPFieldText)list.Fields.CreateNewField(SPFieldType.Text.ToString(), "mycolumn");
fldName.Required = true;
fldName.MaxLength = 50;
site.AllowUnsafeUpdates = false;
web.AllowUnsafeUpdates = false;

using PowerShell

$site= New-Object Microsoft.SharePoint.SPSite ("http://mysite")
$list.Fields.Add("mycolumn", "Text", 0)

No comments:

Post a Comment