Click here to Skip to main content
14,972,848 members
Articles / Productivity Apps and Services / Sharepoint
Article
Posted 28 Oct 2014

Tagged as

Stats

11.8K views
8 bookmarked

SharePoint2010 - Creating, Deleting, Updating, Activating, Deactivating List using Code

Rate me:
Please Sign up or sign in to vote.
4.80/5 (4 votes)
28 Oct 2014CPOL2 min read
Creating, deleting, updating, activating, deactivating List using code in SharePoint 2010

Creating List using Code

Image 1

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

  • Select Windows
  • Windows Form Application
  • Name: WindowsFormsApplications1
  • Click Ok button

(3) In Solution Explorer

  • Right click on Reference > Add

    Image 3

  • 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

Image 5

(5) Open Form1 page

Image 6

  • Create two button names and one listbox as shown in the above figure.

(6) Form1.cs page

C#
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

  • Click Create List button

    Image 8

(8) Now browse the team site (http://tosu-pc/SitePages/Home.aspx)

  • Refresh the page

    Image 9

  • Authors list is created as shown in above figure
  • Click Authors list

    Image 10

Delete a List of Authors

(9) Return to Visual Studio Run mode

Image 11

  • Now click the Delete List button

    Image 12

  • 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

Part B: Change the Name of Default Column (Title) to User Name

(1) Update the Form1.cs

C#
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();

                // update the Default column (Title)

                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

  • Title column is replaced by User Name

Part B: Add New Column in List

(1) Update the Form1.cs

C#
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();

                // update the Default column (Title)

                var title = list.Fields["Title"];
                title.Title = "User Name";
                title.Update();

                // Add New column

               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

(3) Check in the Browser

  • Refresh it

    Image 16

  • List Authors is created with column name: User Name, Employee, Salary/Rate and Bio
  • Click Add New Item

    Image 17

  • After entering Item in pop up and finally click save
  • The view being populatated as shown below:

    Image 18

Part C: Use Validation

  • If not Employee, then salary is less than $50

(1) Update the Form1.cs

C#
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;

                //  use validation [if not employee than sal is less than 50$

                list.ValidationFormula = "IF([Employee], TRUE, [Salary/Rate] <=50)";
                list.ValidationMessage = "The maximum rate for a contributor is $50";

                list.Update();

                // update the Default column (Title)

                var title = list.Fields["Title"];
                title.Title = "User Name";
                title.Update();

                // Add New column

               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

  • View in the browser

    Image 20

  • Click Add new item

    Image 21

Part D: Add Items into the List using VS

(1) Update the Form1.cs

C#
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;

                //  use validation [if not employee than sal is less than 50$

                list.ValidationFormula = "IF([Employee], TRUE, [Salary/Rate] <=50)";
                list.ValidationMessage = "The maximum rate for a contributor is $50";
                
                // update the Default column (Title)

                var titleFildName = "Title";
                var title = list.Fields[titleFildName];
                title.Title = "User Name";
                title.Update();

                // Add New column

               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();

                // Add items into the List

               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

  • Check in the browser.

    Image 23

  • Click on milind.

    Image 24

Part E: Transfer the Code to Actual SharePoint

(1) Open the another instance of Visual Studio 2010

(2) File > New > Project

Image 25

Image 26

Image 27

(3) In solution Explorer

  • Right click on Features

    Image 28

  • Add Feature

(4) Rename the Features as CreateList

Image 29

  • Make changes as shown in the above pic and save

    Image 30

  • Right click on CreateList > Add Event Receiver

    Image 31

(5) Update the CreateList.EventReceiver.cs

C#
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
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        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;

            //  use validation [if not employee than sal is less than 50$

            list.ValidationFormula = "IF([Employee], TRUE, [Salary/Rate] <=50)";
            list.ValidationMessage = "The maximum rate for a contributor is $50";

            // update the Default column (Title)

            var titleFildName = "Title";
            var title = list.Fields[titleFildName];
            title.Title = "User Name";
            title.Update();

            // Add New column

            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();

            // Add items into the List

            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();
        }

        // Uncomment the method below to handle the event raised before a feature is deactivated.

        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

  • Now Feature is not activated automatically, we have to activate it manually.

(7) Deploy

Image 33

  • Refresh the page
  • Site Action > Site Settings

    Image 34

  • Inside Site Actions
  • Select Manage Site template

    Image 35

  • Author List is created with Activate Feature
  • Click Activate

    Image 36

  • Authors List is created
  • Click Authors list

    Image 37

(8) To check the validation

Image 38

  • Click List tab
  • Click List Settings in the Ribbon

    Image 39

  • Click Validation Settings

    Image 40

(9) Deactivated the Feature

  • Site Actions > Site Settings
  • Inside Site Actions > Manage Site Features

    Image 41

  • Click Deactivate as shown in the above figure.

    Image 42

  • Click Deactivate this feature

    Image 43

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Abhishek Sinha Mumbai
Software Developer
India India
• Microsoft certified professional
• Microsoft certified technology specialist(C#, HTML5 with JavaScript and CSS3)
• Microsoft certified solution developer (Web Application)
• Microsoft certified solution developer (Windows Store Apps using C#)

Website : http://abhishek-dotnet.com/

Comments and Discussions

 
SuggestionThis code will only work on a SharePoint server Pin
losvce9-Jul-15 9:07
Memberlosvce9-Jul-15 9:07 
GeneralMy Vote 5 Pin
Shemeemsha (ഷെമീംഷ)28-Oct-14 23:48
MemberShemeemsha (ഷെമീംഷ)28-Oct-14 23:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.