Playing with SharePoint Custom Lists Using SPList Class (SharePoint Object Model)
As a developer you can access SharePoint content using either Microsoft.SharePoint.dll (SharePoint Object Model) or web services. In this post I will be discussing the access using the object model and in the next coming posts I will explain it using web services and JavaScript as well.
Let's elaborate together more about a Site Collection Structure:
Summing up our theory: In order for us to access a custom list, we must first create 3 objects, one that points to the site collection that contains the site that contains the custom list; then create a second object that points to the site that contains the custom list, and finally a third object that points to the custom list.
Moving now to the fun part, In order for us to understand this more, let's create a custom list with 2 fields: Shisha (Huka or Hubbly Bubbly) Flavor Name and Flavor Rating.
Now let's create a new webpart and call it Shisha Flavor Rating Form. We will use this webpart to add new list items to our custom list.
Now we will start by defining the controls that we will access globally:
We will access both text boxes and label from the button event handler. Moving on we define the controls we need to use in the UI and place that code inside the CreateChildControls function:
If you try building and deploying the web part it will look like below:
To make our web part work we need to add some logic and implement the event handler:
The event handler is really simple, its not how you are supposed to write the code. You will need to dispose afterwards. First we create an object that points to a site collection called https://i-bander/. Then we create another that points to a site called i-bander which exists under the https://i-bander/ site collection. Finally we create an object that point to our custom list that we created previously called "Shisha Flavor Rating". To add a new list item we create an object of type SPListItem, fill in the column values, and finally call the Update method which will write back to the list.
Let's test our code:
Comments
- Anonymous
March 28, 2012
Super. I loved the Shisha example. - Anonymous
March 28, 2012
The comment has been removed - Anonymous
March 09, 2013
love it..