Introduction 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).
1. add the reference to Microsoft.SharePoint. After that create the object of SPSite:
{codecitation class="brush:csharp; gutter: true;" width="650px"}
SPSite oSiteCollection = new SPSite(siteName);
{/codecitation}
2.siteName should be the url of your sharepoint site.
Now create the object of SPWeb with the help of SPSite object:
{codecitation class="brush: csharp; gutter: true;" width="650px"}
SPWeb oWebsiteRoot = oSiteCollection.OpenWeb(); oWebsiteRoot.AllowUnsafeUpdates = true;
{/codecitation}
After that create the connection to the User List and create object of SPListItemCollection :
{codecitation class="brush: csharp; gutter: true;" width="650px"}
SPList oList = oWebsiteRoot.Lists["User"]; SPListItemCollection listItemCOll;
{/codecitation}
3.If you want to add the item in the User List then use following steps:
{codecitation class="brush: csharp; gutter: true;" width="650px"}
SPListItem oListItem = oList.Items.Add(); oListItem["FIRST_NAME"] = “Amit”; oListItem["LAST_NAME"] = “Kumar”; oListItem["EMAIL_ADDR"] = “mcapassion[at]gmail.com”; oListItem.Update();
{/codecitation}
After that new item(row) would be added in the User List.
4.If you want to update the item in the User List whose EMPLID is “101” then use following steps:
Create the object of SPQuery:
{codecitation class="brush: csharp; gutter: true;" width="650px"}
SPQuery query = new SPQuery();
//Create query for fetching record which are having EMPLID=101
query.Query="<Where><Eq><FieldRef Name='EMPLID'/><Value Type='Text'>101</Value></Eq></Where>";
//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 for(int icount=0'icount { //Take the reference of that item SPListItem listItem = listItemCOll[icount]; listItem["FIRST_NAME"] = “A”; listItem["LAST_NAME"] = “Kumar”; listItem["EMAIL_ADDR"] = “mcapassion[at]gmail.com”; //Update the Item listItem.Update(); }
}
{/codecitation}
5.If you want to delete the item in the User List whose EMPLID is “101” then use following steps:
Create the object of SPQuery:
{codecitation class="brush: csharp; gutter: true;" width="650px"}
SPQuery query = new SPQuery(); //Create query for fetching record which are having EMPLID=101
query.Query="<Where><Eq><FieldRef Name='EMPLID'/><Value Type='Text'>101</Value></Eq></Where>";
//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(); } }
{/codecitation}
The above mentioned steps help you to create Custom Web parts/User controls in SharePoint for full filling the user requirement.
Thank you | About the Author |
 | | Amit Kumar | Description :5+ years of work experience in ASP.NET C# applications development and database design. 2+ years in SharePoint Worked on SQL Server Worked on Object oriented design and UML Programming Languages/Framework : C#.Net, VB.Net, VB 6.0, VBA,Com/DCom Web Languages : HTML, DHTML, ASP, JavaScript, ASP.Net Operating Systems : WIN 98/2000NT/XP/2003 Server DBMS/RDBMS : MS-Access 2000, ORACLE 8.0, MS-SQL Server 2000, MS-SQL Server 2005 Designing Tools : Microsoft Visio 2003 Versioning Tools : Microsoft Visual Source Safe 6.0 Content Management : Plumtree 4.5 WS, SharePoint (MOSS) Experience in developing Portals using Plumtree 4.5 WS and SharePoint. Experience in user-interface design, user controls, web parts and layout for web-based applications using HTML, CSS, ASP and ASP.net.
Occupation :Software Engineer Company : Haryana. Location : India |
|
|