Thursday, May 15, 2008

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

  • siteName should be the url of your sharepoint site.

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 you want to add the item in the User List then use following steps:

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 you want to update the item in the User List whose EMPLID is “101” then use following steps:

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 you want to delete the item in the User List whose EMPLID is “101” then use following steps:

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 full filling the user requirement.

Share:

4 comments:

Anonymous said...

Thanks for Great help Buddy :)

its realy helpfull for me

thanks :)

Gaurav R said...

now tell me how to do the same using Gridview or SPGridview

Anjana Reddy said...

thanks man.am new to sharepoint and it saves my time..keep posting

Anjana Reddy said...

thanks man u saved my time and am new to sharepoint.keep posting new things..

About Me

My photo
Based in Bangalore, INDIA, Amit is a MCC. He has an MCA from the U.P.T.U., INDIA and holds Microsoft certifications including SharePoint 2007/2010/2013. He is an contributor to MSDN and http://amitkumarmca04.blogspot.com/. Amit have experience in SharePoint,ASP.Net with C#, Classic ASP, VB,Macros,HTML,JavaScript,JSON,BootStrap and AngularJS.

Followers

Labels