Sunday, December 27, 2009

Working with List Objects and Collections in SharePoint

To perform actions on list data in a SharePoint Web site, you must first obtain an SPWeb object to serve as an entry point to the object model, allowing you to access lists, items, documents, users, alerts, etc.

Objects
Most classes in the Microsoft.SharePoint and Microsoft.SharePoint.Administration namespaces start with SP. Generally, classes in the Microsoft.SharePoint assembly that don't start with this prefix represent Web form controls.
Windows SharePoint Services typically does not save modifications of object properties to the database until you call the Update method on the given object. The following example shows how to change the title and description for the Tasks list.

Sample Code:
SPList myList = myWeb.Lists["Tasks"];
myList.Title="New_Title";
myList.Description="List_Description";
myList.Update();

Collections
Just as lists are at the center of a SharePoint site, so are collections at the center of its object models. You can use each collection to add, delete, enumerate, and update a type of object. Collection classes generally share the following traits:
Has a name that ends in "Collection."
Implements the System.Collections.ICollection interface.
Has a Count property of type Int32.
Has an Int32 indexer that can be used to get the nth item in the collection.
Has an indexer that takes an item identifier.
Has Add and Delete methods.


Calling the Add method for a collection usually updates the database on the back-end server with the appropriate data, except when additional information is required in order to update data. In this case, using the Add method returns an object that you can use to gather the information. For example, to add an item to a list, first use the Add method of the Microsoft.SharePoint.SPListItemCollection class to return an SPListItem object, assign values to appropriate properties of the object, and then call the Update method to effect changes within the content database.

Indexers
Indexers provide a useful means to access individual items in collections. To return an item, use square brackets ([]) in C# to contain either an index or a string that identifies the item within the collection.
For example, if mySite represents an instance of the SPWeb class, then SPList myList = mySite.Lists["Announcements"] returns the Announcements list in C#.
To return only a subset of items from the list, you can call the list object's GetItems method and pass an SPQuery object to specify the subset:
SPListItemCollection myItems = myList.GetItems(myQuery)

You can use an indexer to return a specific field from a list (for example, myList.Items["Field_Name"] in C#. You can also specify a field name in an indexer and iterate through the collection of items in a list in order to return values from the field. This example displays the Due Date, Status, and Title values for each item in a list:

Sample Code
foreach(SPListItem myItem in myItems)
{
Response.Write(SPEncode.HtmlEncode(myItem["Due Date"].ToString()) + "
");
Response.Write(SPEncode.HtmlEncode(myItem["Status"].ToString()) + "
");
Response.Write(SPEncode.HtmlEncode(myItem["Title"].ToString()) + "
");
}

The next example shows how to return all Title and Status values for the Tasks list on a SharePoint Web site.
SPWeb mySite = SPContext.Current.Web;
SPList myTasks = mySite.Lists["Tasks"];SPListItemCollection myItems=myTasks.Items;
foreach(SPListItem myItem in myItems)
{
Response.Write(SPEncode.HtmlEncode(myItem["Title"].ToString()) + " :: " + SPEncode.HtmlEncode(myItem["Status"].ToString()) + "
");
}

You can also use indexers to modify values in a list. In the following example, a new list item is added to a collection and the values for a URL column are assigned to the item:
SPListItem myNewItem = myList.Items.Add();
myNewItem["URL_Field_Name"] ="URL, Field_Description";
myNewItem.Update();

The following example sets the Status and Title values for a list item.
SPListItem myItem = myItems[0];
myItem["Status"]="Task_Status";
myItem["Title"]="Task_Title";
myItem.Update();

An indexer throws an ArgumentOutOfRange exception if it does not find the specified item.
[Courtsey MSDN]

No comments:

Post a Comment