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 post I will explain it using web services and JavaScript as well.

Let’s elaborate together more about a Site Collection Structure:

asset01
  • 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.

    asset02

    Now let’s creae a new webpart and call it Shisha Flavor Rating Form. We will use this webpart to add new list items to our custom list.

    asset03

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

    asset04

    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:

    asset05

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

    asset06

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

    asset07

    The event handler is really simple. First we create an object that points to a site collection called http://decisionware.local. Then we create another that points to a site called i-bander which exists under the http://decisionware.local 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:

    asset08

    If you are interested in playing with the source code you can download it from here.

    6 Comments

    1. Craig Watson says:

      I have been a gamer since I was 8 back on the Atari :) Love the article. XBOX 360

    2. oj says:

      can you write about web services? a screen cast would be nice

    3. Radwan Al-fayez says:

      can you do an example using web services?

    4. egxn dev says:

      +1 :-)

    5. ted says:

      This is very handy. Web Services example please :-)

    Leave a Reply