Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

CRUD (Create Read Update Delete) Operations on a SharePoint List programmatically

Article:
Viewed:  11012 
Posted On:  10/08/2013 13:35:14 
How to perform CRUD operation on a SharePoint List programmatically... 

In this article I am going to explain how we can perform CRUD (Create/Add, Read/Search, Update, and Delete) operation in a SharePoint List programmatically.

Below is the SharePoint List…

SharePoint List.png


Image 1.

Below is my Program Output:

SharePoint Programe Output.png


Image 2.

Here in Drop down ID of all items in my List will populate automatically when my program run…


1.   
Select ID and click on Search, will show records in text boxes and in list.
2.    Select Item Id and click on Delete, will delete items.
3.    Type Values In text box and click on ADD, will add new item.
4.    Select records and update value, will update existing record.
5.    Click on Show All Items will show all records in SharePoint List.

Now select Item ID and click on Search…

Search Items.png


Image 3.

If Click on Show All Items

Search All Records in Shareoint List.png


Image 4.

Select Item Id. Click on Search and update value…

Update.png


Image 5.

Now below is my Code:

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 SharePoint__CRUD_Operations_on_a_List
{
  publicpartialclass Form1 : Form
  {
     public Form1()
    {
          InitializeComponent();
    } 

    private void Form1_Load(object sender, EventArgs e)
    {
         BindAllRecords();
    } 

    private void BindAllRecords()
    {
          using (SPSite site = newSPSite("http://localhost:7000"))
         {
                using (SPWeb web = site.OpenWeb())
                {
  SPList list = web.Lists["MyContacts"];
SPListItemCollection items = list.Items;

cmbItemId.DataSource = items.GetDataTable();
cmbItemId.DisplayMember = "Id";
cmbItemId.ValueMember = "Id";
                }

            }

        }

private void 
btnAllItems_Click(object sender, EventArgs e)
        {
GetAllItems(0);
        } 

private void btn_Search_Click(object sender, EventArgs e)
        {
using (SPSite site = newSPSite("http://localhost:7000"))
        {
using (SPWeb web = site.OpenWeb())
                 {
SPList list = web.Lists["MyContacts"];
SPListItemCollection items = list.Items;
foreach (SPListItemlistItemin items)
                {
if (listItem["ID"].ToString() == cmbItemId.SelectedValue.ToString())
                        {
textBoxTitle.Text = (string)listItem["Title"].ToString();
textBoxName.Text = listItem["Name"].ToString();
textBoxAddress.Text = listItem["Address"].ToString();
textBoxContactNo.Text = listItem["ContactNo"].ToString();
textBoxCountry.Text = listItem["Country"].ToString();
                        }
                 }
lblErrorMsg.Text = "Search Result";
lblErrorMsg.Visible = true;
DataView dv = items.GetDataTable().DefaultView;
dv.RowFilter="ID=" + cmbItemId.SelectedValue;
dataGridViewAllItems.DataSource = dv;
                }
          }
        } 

private void btn_AddItem_Click(object sender, EventArgs e)
{
using (SPSite site = newSPSite("http://localhost:7000"))
        {
using (SPWeb web = site.OpenWeb())
                {
web.AllowUnsafeUpdates = true;
SPList list = web.Lists["MyContacts"];
SPListItem Item = list.Items.Add();
Item["Title"] = textBoxTitle.Text;
Item["Name"] = textBoxName.Text;
Item["Address"] = textBoxAddress.Text;
Item["ContactNo"] = textBoxContactNo.Text;
Item["Country"] = textBoxCountry.Text;

textBoxTitle.Text =
"";
textBoxName.Text = "";
textBoxAddress.Text = "";
textBoxContactNo.Text = "";
textBoxCountry.Text = "";
 Item.Update();
web.AllowUnsafeUpdates = false;
                }
             }

lblErrorMsg.Text =
"Item Added Successfully...";
lblErrorMsg.Visible = false;
GetAllItems(0);
        } 

private void GetAllItems( intItemID)
        {
using (SPSite site = newSPSite("http://localhost:7000"))
        {
using (SPWeb web = site.OpenWeb())
                {
SPList list = web.Lists["MyContacts"];
SPListItemCollection items = list.Items;
if (ItemID == 0)
dataGridViewAllItems.DataSource = items.GetDataTable();
else
                    {
DataView dv = items.GetDataTable().DefaultView;
dv.RowFilter = "ID=" + cmbItemId.SelectedValue;
dataGridViewAllItems.DataSource = dv;
                    }
                }
          }
        }

    private void btn_UpdateItem_Click(object sender, EventArgs e)
    {
            using (SPSite site = newSPSite("http://localhost:7000"))
            {
                   using (SPWeb web = site.OpenWeb())
                   {
    web.AllowUnsafeUpdates = true;
SPList list = web.Lists["MyContacts"];
                         SPListItem Item = list.GetItemById(Convert.ToInt32(cmbItemId.SelectedValue));
Item["Title"] = textBoxTitle.Text;
Item["Name"] = textBoxName.Text;
Item["Address"] = textBoxAddress.Text;
Item["ContactNo"] = textBoxContactNo.Text;
Item["Country"] = textBoxCountry.Text;

textBoxTitle.Text =
"";
textBoxName.Text = "";
textBoxAddress.Text = "";
textBoxContactNo.Text = "";
textBoxCountry.Text = "";
Item.Update();
web.AllowUnsafeUpdates = false;
lblErrorMsg.Text = "Item Updated Successfully...";
lblErrorMsg.Visible = true;
                  }
            }

GetAllItems(
Convert.ToInt32(cmbItemId.SelectedValue));          
        }

       private void btnDeleteItem_Click(object sender, EventArgs e)
       {
            using (SPSite site = newSPSite("http://localhost:7000"))
            {
                  using (SPWeb web = site.OpenWeb())
                  {
web.AllowUnsafeUpdates = true;
                        SPList list = web.Lists["MyContacts"];
                        SPListItem Item = list.GetItemById(Convert.ToInt32(cmbItemId.SelectedValue));
                        Item.Delete();
                web.AllowUnsafeUpdates = false;
lblErrorMsg.Text = "Item Deleted Successfully...";
lblErrorMsg.Visible = true;
                  }
            }

BindAllRecords();
GetAllItems(0);

        } 

    }

}

  Comment:
 
By Sangeetha  On  20/02/2018 07:29:17
please provide the ascx page also.
 
By amine  On  26/01/2017 07:42:31
you have html ?
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us