How to programmatically read, add and edit item in a SharePoint list
I have a company that supports an Internet portal on SharePoint, called HotSpotPlace. The portal provides information, advertisements, services and so on. The company has a merchant partner - a company selling consumer goods online. The partner wants clients to be able to make orders for products on any portal page, because the portal is very popular. Users need to be able to register online and for each product, a history of the last purchase date has to be preserved for analytical purposes. These are the requirements.
To fulfill requirements we are going to:
- Create a list of provided products.
- Create a list of orders.
- Develop a web part.
The web part will appear on each page of the portal. The web part has to have the following functionalities:
- Read the products from the product list.
- Add new order.
- Update the date, so last purchase date will be preserved.
Let's start with lists. Firstly we create a Product list. The list contains product title and last purchase date.
The next step is to create an orders list which contains product title, quantity, and delivery address.
Now we are ready to start with the web part. In order to make the web part we will use Visual Studio 2008. On the Navigation Pane from the "File" menu, select "New Project". We will name this project "WPStationeryDocLib". Under "Project Types", select SharePoint, and under "Visual Studio installed Templates" select "WebPart Template". Type the project name and press OK.
Next is to select the trust level for this project, and we chose Partial Trust.
For better visibility we rename the default name of the project folder from WebPart1 to WPHotSpotOrder
Add using System.Collections.Generic;
The next step is to write a method to get all products. We will use the result later to populate a drop down list with products. In this method it is shown how to programmatically read items from a list.
Next we need to add controls to make a form. To achieve this we have to add labels, textboxes, dropdown lists and a button. For better visibility we will put these controls in a table. We have to declare controls and implement the method CreateChildControls, as shown below.
Here is the CreateChildControls implementation.
The next step is to implement btnSend_Click. We call the method AddItem. In this method it is shown how to programmatically add an item in a list.
Our next task is to preserve the purchase date for the ordered product. For this purpose we have a column named 'LastPurchaseDate' in the 'Products' list. To complete this task we have to write the method UpdateDate. Here it is shown how to update an item in a list.
Now we have to call this method, by changing the btnSend_Click implementation as shown bellow.
Next we build, package and deploy the solution. For testing purposes we are going to add the web part on an empty page. We open our test environment, create a page and add the web part. Now we have our custom web part, which allows us to work as an interface to receive orders and requests.
After sending a request we can open the Products list to see that the Last purchase date changes, which means it allows the programmatic editing of a list item.
Now we can put this web part where ever we want on our portal and web designers can write css for better visibility and according to the portal styling. "Out of the box", a SharePoint list form may not be suitable for your needs if you have a stylish portal as it comes with predefined styles. But this way you can desgn it according to the needs of the organization. Last but not least, you are able to add, read and update items using code.
- Check out these other tips:
About the author
View all my tips
Article Last Updated: 2011-08-30