Wednesday 5 June 2013

Add/Update/Delete operations on the Sharepoint List Item using C#(C-Sharp)

Add/Update/Delete operations on the Sharepoint List Item using C#(C-Sharp)

Add/Update/Delete operations on the Sharepoint List Item using C#(C-Sharp):
The list present in the SharePoint serves as a table which we used in the relational database. Its means in SharePoint we used list for storing user data instead of table used in RDB. These lists contain data which are required by the user for doing some computational operation or for other functionality. Here I am taking the scenario of the User List for storing user details. This list contain the details of user i.e. first name , last name and email id. With the help of this list I will explain you, how you can add/update/delete items in the list with the help of C#(C-Sharp).
First add the reference to Microsoft.SharePoint. After that create the object of SPSite:

SPSite oSiteCollection = new SPSite();
<!--[if !supportLists]-->
  • siteName should be the url of your sharepoint site.
<!--[endif]-->
Now create the object of SPWeb with the help of SPSite object:
SPWeb oWebsiteRoot = oSiteCollection.OpenWeb();
oWebsiteRoot.AllowUnsafeUpdates = true;
After that create the connection to the User List and create object of SPListItemCollection :
SPList oList = oWebsiteRoot.Lists["User"];
SPListItemCollection listItemCOll;
<!--[if !supportLists]-->
  • If you want to add the item in the User List then use following steps:
<!--[endif]-->
SPListItem oListItem = oList.Items.Add();
oListItem["FIRST_NAME"] = “Amit”;
oListItem["LAST_NAME"] = “Kumar”;
oListItem["EMAIL_ADDR"] = “mcapassion@gmail.com”;
oListItem.Update();
After that new item(row) would be added in the User List.
<!--[if !supportLists]-->
  • If you want to update the item in the User List whose EMPLID is “101” then use following steps:
<!--[endif]-->
Create the object of SPQuery:
SPQuery query = new SPQuery();
//Create query for fetching record which are having EMPLID=101
//Fetch item from the User List by passing query to the User List
listItemCOll = oList.GetItems(query);
if (listItemCOll.Count > 0)
{
//If record found in the list

{
//Take the reference of that item
SPListItem listItem = listItemCOll[icount];
listItem["FIRST_NAME"] = “A”;
listItem["LAST_NAME"] = “Kumar”;
listItem["EMAIL_ADDR"] = “mcapassion@gmail.com”;
//Update the Item
listItem.Update();
}
}
<!--[if !supportLists]-->
  • If you want to delete the item in the User List whose EMPLID is “101” then use following steps:
<!--[endif]-->
Create the object of SPQuery:
SPQuery query = new SPQuery();
//Create query for fetching record which are having EMPLID=101


//Check items exist in the User List or not
if (oList.Items.Count > 0)
{
//Fetch item from the User List by passing query to the User List
listItemCOll = oList.GetItems(query);
if (listItemCOll.Count > 0)
{
//If record found in the list
listItemCOll [0].Delete();
}
}
The above mentioned steps help you to create Custom Web parts/User controls in SharePoint for

No comments:

Post a Comment