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;
list.Fields.Add(fldName);
list.Update();
 
site.AllowUnsafeUpdates = false;
web.AllowUnsafeUpdates = false;

using PowerShell


 
[system.reflection.assembly]::loadwithpartialname("microsoft.sharepoint")
$site= New-Object Microsoft.SharePoint.SPSite ("http://mysite")
$web=$site.OpenWeb()
$list=$web.Lists["mylist"]
$list.Fields.Add("mycolumn", "Text", 0)

No comments:

Post a Comment