SharePoint Basic CRUD Operation Insert , Delete , Update , Select All Records

SharePoint Basic CRUD Operation Sample Code

AddItem :

private void AddItem()
{
try
{
using (SPSite oSite = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
SPList oList = oWeb.Lists.TryGetList(“Info”);

SPListItem oListItem = oList.AddItem();

oListItem[“Title”] = “Second”;
oListItem[“Name”] = “Microsoft”;
oListItem[“Contact_x0020_Number”] = “555”;

oListItem.Update();

lblMessage.Text = “Item Added”;
}

}

}
catch (Exception ex)
{
lblMessage.Text = ex.ToString();
}
}

Update Item :

private void UpdateItem()
{
using (SPSite oSite = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
SPList oList = oWeb.Lists.TryGetList(“Info”);

SPListItem oListitem = oList.GetItemById(int.Parse(TextBox1.Text));

oListitem[“Name”] = “Sample Code”;

oListitem.Update();

}
}
}
Delete Item :

private void DeleteItem()
{
using (SPSite oSite = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
SPList oList = oWeb.Lists.TryGetList(“Info”);

SPListItem oListitem = oList.GetItemById(int.Parse(TextBox1.Text));

oListitem.Delete();

lblMessage.Text = “Item Deleted”;
}
}
}

Get All list Item

private void LoadGridview()
{
lblMessage.Text = “Get List Data”;

try
{
using (SPSite oSite = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
SPList oList = oWeb.Lists.TryGetList(“Info”);

SPListItemCollection oListItemColl = oList.Items;

GridView1.DataSource = oListItemColl.GetDataTable();
GridView1.DataBind();

}

}

}
catch (Exception ex)
{
lblMessage.Text = ex.ToString();
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s