Adding SharePoint 2007 Lists using a SmartClient
 
Published: 30 Oct 2007
Abstract
This article examines how to add SharePoint 2007 Lists using a SmartClient with the help of C# language.
by Steven Barden
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 24587/ 65

Introduction

The purpose of this article is to demonstrate using C# code in a SmartClient to programmatically develop various componenets of a SharePoint 2007 instance. This article will cover the act of opening a SharePoint 2007 website, iterate and list the existing Lists, and add new generic Lists to a SharePoint site.

Assumptions

1.    You are running or have access to an instance of Windows Server 2003 running (at minimum) WSS 3.0 or MOSS.

2.    You have installed Visual Studio 2005. This may be on a workstation separate of your WSS/ MOSS installation.

3.    You have desktop development access to the WSS server. This application is run on the development server.

Steps

1.    Develop C# code in a SmartClient to connect to the SP web.

2.    Iterate the existing collection of Lists.

3.    Add new generic Lists and review the updated collection.

4.    Provide a starting point to develop further solutions.

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
 try
 {
 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.";
 this.displayLists();
 }
 catch (Exception ee)
 {
 // Handle the errors
 this.displayInformation(ee.ToString());
 }
 }

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
   this.topLevelWebsite.Close();
   this.subWebsite.Dispose();
 }

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.

Downloads

Conclusion

In this article you learned how to add SharePoint 2007 Lists using a SmartClient with the help of C# language.



User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





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


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-16 1:48:37 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search