Creating List using Code
![Image 1](/KB/sharepoint/832883/image001.png)
For this demo, you are using the above team site.
Part A: Create and Delete a List of Authors
(1) Open Visual Studio 2010
(2) File > new > Project
![Image 2](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Select Windows
- Windows Form Application
- Name: WindowsFormsApplications1
- Click Ok button
(3) In Solution Explorer
- Right click on Reference > Add
![Image 3](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Select .NET tab
- Select Microsoft.SharePoint
- Click Ok
(4) In Solution Explorer
- Right click on Properties > Open
- In the properties window, set the properties as shown below in images.
![Image 4](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
![Image 5](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
(5) Open Form1 page
![Image 6](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Create two button names and one listbox as shown in the above figure.
(6) Form1.cs page
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private const string SiteUrl = "http://tosu-pc/SitePages/Home.aspx";
public Form1()
{
InitializeComponent();
}
private void CreateListButton_Click(object sender, EventArgs e)
{
using (var site = new SPSite(SiteUrl))
{
var web = site.RootWeb;
var listId = web.Lists.Add("Authors", string.Empty, SPListTemplateType.GenericList);
var list = web.Lists[listId];
list.OnQuickLaunch = true;
list.Update();
listBox1.Items.Add("Authors list created");
}
}
private void DeleteListButton_Click(object sender, EventArgs e)
{
using (var site = new SPSite(SiteUrl))
{
var web = site.RootWeb;
var list = web.Lists.TryGetList("Authors");
if (list != null)
{
list.Delete();
}
listBox1.Items.Add("Authors list deleted");
}
}
}
}
(7) Run (F5)
![Image 7](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Click Create List button
![Image 8](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
(8) Now browse the team site (http://tosu-pc/SitePages/Home.aspx)
- Refresh the page
![Image 9](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Authors list is created as shown in above figure
- Click Authors list
![Image 10](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
Delete a List of Authors
(9) Return to Visual Studio Run mode
![Image 11](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Now click the Delete List button
![Image 12](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Authors list deleted is display in listBox1
(10) Browse the Team site
- Refresh it
- Authors list is deleted from list as shown in figure below:
![Image 13](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
Part B: Change the Name of Default Column (Title) to User Name
(1) Update the Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private const string SiteUrl = "http://tosu-pc/SitePages/Home.aspx";
public Form1()
{
InitializeComponent();
}
private void CreateListButton_Click(object sender, EventArgs e)
{
using (var site = new SPSite(SiteUrl))
{
var web = site.RootWeb;
var listId = web.Lists.Add("Authors", string.Empty, SPListTemplateType.GenericList);
var list = web.Lists[listId];
list.OnQuickLaunch = true;
list.Update();
var title = list.Fields["Title"];
title.Title = "User Name";
title.Update();
listBox1.Items.Add("Authors list created");
}
}
private void DeleteListButton_Click(object sender, EventArgs e)
{
using (var site = new SPSite(SiteUrl))
{
var web = site.RootWeb;
var list = web.Lists.TryGetList("Authors");
if (list != null)
{
list.Delete();
}
listBox1.Items.Add("Authors list deleted");
}
}
}
}
(2) Run F5
(3) Check in Browser
![Image 14](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Title column is replaced by User Name
Part B: Add New Column in List
(1) Update the Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private const string SiteUrl = "http://tosu-pc/SitePages/Home.aspx";
public Form1()
{
InitializeComponent();
}
private void CreateListButton_Click(object sender, EventArgs e)
{
using (var site = new SPSite(SiteUrl))
{
var web = site.RootWeb;
var listId = web.Lists.Add("Authors", string.Empty, SPListTemplateType.GenericList);
var list = web.Lists[listId];
list.OnQuickLaunch = true;
list.Update();
var title = list.Fields["Title"];
title.Title = "User Name";
title.Update();
var empFieldName = list.Fields.Add("Employee", SPFieldType.Boolean, false);
var rateFieldName = list.Fields.Add("Salary/Rate" , SPFieldType.Currency, true);
var bioFieldName = list.Fields.Add("Bio", SPFieldType.Note, false);
var bio = list.Fields[bioFieldName] as SPFieldMultiLineText;
bio.NumberOfLines = 8;
bio.RichText = false;
bio.Update();
var view = list.DefaultView;
view.ViewFields.Add(empFieldName);
view.ViewFields.Add(rateFieldName);
view.ViewFields.Add(bioFieldName);
view.Update();
listBox1.Items.Add("Authors list created");
}
}
private void DeleteListButton_Click(object sender, EventArgs e)
{
using (var site = new SPSite(SiteUrl))
{
var web = site.RootWeb;
var list = web.Lists.TryGetList("Authors");
if (list != null)
{
list.Delete();
}
listBox1.Items.Add("Authors list deleted");
}
}
}
}
(2) F5
![Image 15](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
(3) Check in the Browser
- Refresh it
![Image 16](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- List Authors is created with column name: User Name, Employee, Salary/Rate and Bio
- Click Add New Item
![Image 17](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- After entering Item in pop up and finally click save
- The view being populatated as shown below:
![Image 18](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
Part C: Use Validation
- If not Employee, then salary is less than $50
(1) Update the Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private const string SiteUrl = "http://tosu-pc/SitePages/Home.aspx";
public Form1()
{
InitializeComponent();
}
private void CreateListButton_Click(object sender, EventArgs e)
{
using (var site = new SPSite(SiteUrl))
{
var web = site.RootWeb;
var listId = web.Lists.Add("Authors", string.Empty, SPListTemplateType.GenericList);
var list = web.Lists[listId];
list.OnQuickLaunch = true;
list.ValidationFormula = "IF([Employee], TRUE, [Salary/Rate] <=50)";
list.ValidationMessage = "The maximum rate for a contributor is $50";
list.Update();
var title = list.Fields["Title"];
title.Title = "User Name";
title.Update();
var empFieldName = list.Fields.Add("Employee", SPFieldType.Boolean, false);
var rateFieldName = list.Fields.Add("Salary/Rate" , SPFieldType.Currency, true);
var bioFieldName = list.Fields.Add("Bio", SPFieldType.Note, false);
var bio = list.Fields[bioFieldName] as SPFieldMultiLineText;
bio.NumberOfLines = 8;
bio.RichText = false;
bio.Update();
var view = list.DefaultView;
view.ViewFields.Add(empFieldName);
view.ViewFields.Add(rateFieldName);
view.ViewFields.Add(bioFieldName);
view.Update();
listBox1.Items.Add("Authors list created");
}
}
private void DeleteListButton_Click(object sender, EventArgs e)
{
using (var site = new SPSite(SiteUrl))
{
var web = site.RootWeb;
var list = web.Lists.TryGetList("Authors");
if (list != null)
{
list.Delete();
}
listBox1.Items.Add("Authors list deleted");
}
}
}
}
(2) F5
- Make sure Authors list is deleted before creating
![Image 19](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- View in the browser
![Image 20](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Click Add new item
![Image 21](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
Part D: Add Items into the List using VS
(1) Update the Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private const string SiteUrl = "http://tosu-pc/SitePages/Home.aspx";
public Form1()
{
InitializeComponent();
}
private void CreateListButton_Click(object sender, EventArgs e)
{
using (var site = new SPSite(SiteUrl))
{
var web = site.RootWeb;
var listId = web.Lists.Add("Authors", string.Empty, SPListTemplateType.GenericList);
var list = web.Lists[listId];
list.OnQuickLaunch = true;
list.ValidationFormula = "IF([Employee], TRUE, [Salary/Rate] <=50)";
list.ValidationMessage = "The maximum rate for a contributor is $50";
var titleFildName = "Title";
var title = list.Fields[titleFildName];
title.Title = "User Name";
title.Update();
var empFieldName = list.Fields.Add("Employee", SPFieldType.Boolean, false);
var rateFieldName = list.Fields.Add("Salary/Rate" , SPFieldType.Currency, true);
var bioFieldName = list.Fields.Add("Bio", SPFieldType.Note, false);
var bio = list.Fields[bioFieldName] as SPFieldMultiLineText;
bio.NumberOfLines = 8;
bio.RichText = false;
bio.Update();
list.Update();
var view = list.DefaultView;
view.ViewFields.Add(empFieldName);
view.ViewFields.Add(rateFieldName);
view.ViewFields.Add(bioFieldName);
view.Update();
var item = list.Items.Add();
item[titleFildName] = "Abhishek";
item[empFieldName] = false;
item[rateFieldName] = 30;
item.Update();
item = list.Items.Add();
item[titleFildName] = "Rajkumar";
item[empFieldName] = true;
item[rateFieldName] = 50;
item.Update();
item = list.Items.Add();
item[titleFildName] = "Rahul";
item[empFieldName] = true;
item[rateFieldName] = 60;
item.Update();
item = list.Items.Add();
item[titleFildName] = "milind";
item[empFieldName] = false;
item[rateFieldName] = 40;
item.Update();
listBox1.Items.Add("Authors list created");
}
}
private void DeleteListButton_Click(object sender, EventArgs e)
{
using (var site = new SPSite(SiteUrl))
{
var web = site.RootWeb;
var list = web.Lists.TryGetList("Authors");
if (list != null)
{
list.Delete();
}
listBox1.Items.Add("Authors list deleted");
}
}
}
}
(2) F5
- Make sure list is deleted first.
![Image 22](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Check in the browser.
![Image 23](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Click on milind.
![Image 24](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
Part E: Transfer the Code to Actual SharePoint
(1) Open the another instance of Visual Studio 2010
(2) File > New > Project
![Image 25](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
![Image 26](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
![Image 27](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
(3) In solution Explorer
- Right click on Features
![Image 28](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Add Feature
(4) Rename the Features as CreateList
![Image 29](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Make changes as shown in the above pic and save
![Image 30](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Right click on CreateList > Add Event Receiver
![Image 31](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
(5) Update the CreateList.EventReceiver.cs
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
namespace ListAndLibraries1.Features.CreateList
{
[Guid("cc22c160-bb85-4906-a4ee-8f49d4e93203")]
public class CreateListEventReceiver : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
var web = properties.Feature.Parent as SPWeb;
if (web == null) return;
var listId = web.Lists.Add("Authors", string.Empty, SPListTemplateType.GenericList);
var list = web.Lists[listId];
list.OnQuickLaunch = true;
list.ValidationFormula = "IF([Employee], TRUE, [Salary/Rate] <=50)";
list.ValidationMessage = "The maximum rate for a contributor is $50";
var titleFildName = "Title";
var title = list.Fields[titleFildName];
title.Title = "User Name";
title.Update();
var empFieldName = list.Fields.Add("Employee", SPFieldType.Boolean, false);
var rateFieldName = list.Fields.Add("Salary/Rate", SPFieldType.Currency, true);
var bioFieldName = list.Fields.Add("Bio", SPFieldType.Note, false);
var bio = list.Fields[bioFieldName] as SPFieldMultiLineText;
bio.NumberOfLines = 8;
bio.RichText = false;
bio.Update();
list.Update();
var view = list.DefaultView;
view.ViewFields.Add(empFieldName);
view.ViewFields.Add(rateFieldName);
view.ViewFields.Add(bioFieldName);
view.Update();
var item = list.Items.Add();
item[titleFildName] = "Abhishek";
item[empFieldName] = false;
item[rateFieldName] = 30;
item.Update();
item = list.Items.Add();
item[titleFildName] = "Rajkumar";
item[empFieldName] = true;
item[rateFieldName] = 50;
item.Update();
item = list.Items.Add();
item[titleFildName] = "Rahul";
item[empFieldName] = true;
item[rateFieldName] = 60;
item.Update();
item = list.Items.Add();
item[titleFildName] = "milind";
item[empFieldName] = false;
item[rateFieldName] = 40;
item.Update();
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
var web = properties.Feature.Parent as SPWeb;
if (web == null) return;
var list = web.Lists.TryGetList("Authors");
if (list != null)
{
list.Delete();
}
}
}
}
(6) Change the Feature Receiver not activated automatically
![Image 32](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Now Feature is not activated automatically, we have to activate it manually.
(7) Deploy
![Image 33](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Refresh the page
- Site Action > Site Settings
![Image 34](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Inside Site Actions
- Select Manage Site template
![Image 35](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Author List is created with Activate Feature
- Click Activate
![Image 36](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Authors List is created
- Click Authors list
![Image 37](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
(8) To check the validation
![Image 38](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Click List tab
- Click List Settings in the Ribbon
![Image 39](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Click Validation Settings
![Image 40](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
(9) Deactivated the Feature
- Site Actions > Site Settings
- Inside Site Actions > Manage Site Features
![Image 41](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Click Deactivate as shown in the above figure.
![Image 42](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Click Deactivate this feature
![Image 43](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)