My new blog present here.

Featured Post

Insights into Sitecore Search: A Definitive Introduction

A key component of digital experience management is effective information retrieval. A simplified approach is required for websites, applications, and platforms containing a lot of material so that consumers can easily get the data they require. This need is met by Sitecore, a well-known name in the field of digital experience platforms (DXPs), which provides powerful and comprehensive search functionality. We will travel into the realm of Sitecore Search in this article, learning about its capabilities, architecture , and the enormous value it offers both developers and end users. Introduction to Sitecore Search    A headless content discovery platform powered by AI , Sitecore Search enables you to build predictive and custom search experiences across various content sources. To extract and index you

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.

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..
India Trip said…
Its useful information, thanks for sharing

Popular posts from this blog

Sitecore GraphQL Queries

Configuring Sitecore Next.js Headless SXA Multisite App in a Sitecore Container

Sitecore Experience Manager Cloud (XM Cloud) Building blocks