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:

image
  • The highest level is the site collection itself. We will access it using the SPSite object.
  • Each site inside the site collection can be accessed using the SPWeb object.
  • Each list inside a SPWeb can be accessed using the SPList object.
  • Each list item inside a SPList can be accessed using SPListItem object.
  • We can have access to Site Collections using the absolute URL or the GUID. Same thing goes for sites under site collections and custom lists.

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.

image

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.

image

Now we will start by defining the controls that we will access globally:

image

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:

image

If you try building and deploying the web part it will look like below:

image

To make our web part work we need to add some logic and implement the event handler:

image

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 http://i-bander/. Then we create another that points to a site called i-bander which exists under the http://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:

image

Comments (3)

  1. Anonymous says:

    Super. I loved the Shisha example.

  2. I used to smoke a lot of Shisha back in Jordan. Man I miss those days..!

Skip to main content