Adding SharePoint 2007 Lists using a SmartClient
page 4 of 6
by Steven Barden
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 24857/ 58

Starting Your Local Application

1.    Begin by starting a Windows Smart Client application.

2.    In your SmartClient application, create references to Microsoft.SharePoint assembly.

3.    Place the following controls on your form:

·         Button - buttonOpenConnection - enact the process of displaying existing lists

·         ListBox - listBoxLists - to actually display the existing lists

·         TextBox - textBoxInfo - to display and information or errors during runtime

·         Button - buttonCreateNewList - enact the process of creating a new list

·         TextBox - textBoxNewListName - to name the new list

·         TextBox - textBoxNewListDescrition - a multi-line textbox to add a description to the list

4.    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.

5.    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

6.    Next, build a void method that will perform the following actions and call it in the Form initialization after the existing InitializeComponents method.

Listing 1

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.

7.    Once the collection of Lists is found you can easily cycle it to display all by adding the titles to the ListBox items collection

8.    The main bulk of the work is performed in the createNewGenericList method.

Listing 2

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,
    newListDescription, newListType);
 // 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", 
    newFieldType, true);
 //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

Here, the 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 want.

·         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.

9.    Finally, we use the FormClosing event to perform cleanup by calling our method that closes.

Listing 3

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.

View Entire Article

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2022  |  Page Processed at 2022-06-24 9:04:03 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search