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 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: