Begin by starting a Windows Smart Client application.
In your SmartClient application, create references to
Place the following controls on your form:
Button - buttonOpenConnection - enact the process of displaying
ListBox - listBoxLists - to actually display the existing lists
TextBox - textBoxInfo - to display and information or errors
Button - buttonCreateNewList - enact the process of creating a
TextBox - textBoxNewListName - to name the new list
TextBox - textBoxNewListDescrition - a multi-line textbox to add
a description to the list
Obviously there will be many locations in this project where you can
select the names or naming convention you desire. This will not affect the
solution, but simply make the project yours.
Next, add the following object to be used globally by your project:
An SPSite object to contain the top level SP site
An SPWeb object to contain the subweb
An SPListsCollection to contain the collection of lists that
already exist and that you will add during your development
Next, build a void method that will perform the following actions and
call it in the Form initialization after the existing InitializeComponents
private void Init()
// Init the top level website
this.topLevelWebsite = new SPSite(topLevelWebsiteAddress);
// Open the actionable sub-web. If we are acting against the
// top level website there is no need to provide a location here
this.subWebsite = topLevelWebsite.OpenWeb();
// Get the Lists Collection
this.websiteListCollection = subWebsite.Lists;
Instantiate your top level site object and pass it the url of the site you are using.
Do the same with the subweb. You will notice that since I am
again developing the top level site, I have not passed any information pointing
to a subweb. This enables the object to act upon the main site.
Use the Lists collection object to grab the collection of lists
from the subWeb objects Lists collection.
Once the collection of Lists is found you can easily cycle it to display
all by adding the titles to the ListBox items collection
The main bulk of the work is performed in the createNewGenericList
private void createNewGenericList(string newListName, string newListDescription)
// Create a guid to hold the new list id
this.textBoxInfo.Text = "The new list is being created.";
// Assume the top level website is availible
// Assume the subweb is availible
// Assume the list of Lists is availible
// Prepair the desired List type to be used
SPListTemplateType newListType = SPListTemplateType.GenericList;
// Add a new List to the Lists collection
System.Guid newListGuid = this.websiteListCollection.Add(newListName,
// Get a reference to the new list
SPList newListInstance = this.subWebsite.Lists.GetList(newListGuid, false);
// Get the collection of fields
SPFieldCollection newListFieldsCollection = newListInstance.Fields;
// Create a simple, single, new field type of text
SPFieldType newFieldType = SPFieldType.Text;
// Add the new field to the new List
string newFieldInstance = newListFieldsCollection.Add("Example Text Field",
//Cleanup and notify
this.textBoxNewListName.Text = string.Empty;
this.textBoxNewListDescrition.Text = string.Empty;
this.textBoxInfo.Text = "The new list has been created.";
catch (Exception ee)
// Handle the errors
following is performed:
All code is wrapped in a try/catch block.
A new SpListTemplateType is instantiated of type GenericList. In
later articles I will cover far more complex examples, but at this time the use
of a generic list will do for our needs.
Using information gathered from the provided textboxes, we pass
the required information to the Add method of the website list collection and
get back the ID of type Guid.
Now we instantiate an instance of a list and assign it the new
list by using the guid.
With that, we get the collection of fields from the list.
We create a new field object and provide it with the information
We also create a new filed instance in the field collection by
passing the field and the name we are going to assign to it.
Finally, we use the FormClosing event to perform cleanup by calling our
method that closes.
private void cleanUp()
// Close the connections
Clearly this is a very simple example with a lot of room for
adding more complex code to suit your needs. One obvious example of extending
this code would be to provide for other forms of lists.