Colombo 05, Sri Lanka
24-07-2010
Dehiwala, Sri Lanka
21-05-2010
Dehiwala, Sri Lanka
21-05-2010
Colombo, Sri Lanka
16-06-2010
Microsoft SharePoint 2007 Development – Fun with Lists

by Prabath Fonseka

4th Article of the Series - Microsoft SharePoint 2007 Development

Today I’m taking you through a fun ride which you will not want to forget. The reason I say so is today’s topic.

List Management

List is more like a Table in a Database. Of all the tasks a SharePoint programmer can do in SharePoint, working with Lists is the mostly common task.

Namespace: Microsoft.SharePoint
Classes: SPList, SPListItem, SPListItemCollection

With MOSS 2007 and WSS, we get different kinds of lists. To name them…

  • Announcements list
  • Contacts list
  • Discussion Board list
  • Links list
  • Calendar list
  • Tasks list
  • Project Tasks list
  • Issue Tracking list
  • Survey list
  • Custom list
  • Custom list in Datasheet view
  • KPI list (SharePoint Server 2007 only)
  • Languages and translators (SharePoint Server 2007 only)
  • Import spreadsheet

This article doesn’t cover what SharePoint is or its usage. Targeted audience is the programmers who are already familiar with SharePoint who’s looking for a programming introduction to Lists in SharePoint and leverage the features of SPList and SPListCollection classes in SharePoint Object Model.

 

Enumerating Lists

Yes you see it right, its Lists. Before getting to the list items, let’s try to get a collection of lists implemented in a site. So basically in other words we are trying to enumerate through any SPListCollection instance.

First we need to create a SPSite object and get all webs in to a SPWeb object by calling the AllWebs() method of the SPSite object. Then it’s just a matter of iterating through the SPLists objects available in the SPWeb object.

Create a new C# console application and paste the code below changing the SPSite class’s input parameter url.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace AllLists
{
class Program
{
static void Main(string[] args)
{
SPSite site = new SPSite("http://dc:8844");
SPWeb web = site.AllWebs[0];
foreach (SPList list in web.Lists)
{
Console.WriteLine("{0}{1} - {2} items.",
list.Hidden ? "*" : "",
list.Title, list.ItemCount);
Console.WriteLine("Created by {0}", list.Author.Name);
Console.WriteLine("{0}", list.Description);
Console.WriteLine("................................");
}
Console.WriteLine("\n{0} lists found.", web.Lists.Count);
Console.ReadKey();
}
}
}

 

Enumerating list items in Lists

It is the SPList you have to iterate through to find the SPListItem. But you might run in to some errors if you do the list iteration against a top level site collection instead of a team site.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace AllLists
{
class Program
{
static void Main(string[] args)
{
SPSite site = new SPSite("http://dc:8844/sales/");
SPWeb web = site.AllWebs[0];
foreach (SPList list in web.Lists)
{
Console.WriteLine("{0}{1} - {2} items.",
list.Hidden ? "*" : "",
list.Title, list.ItemCount);
Console.WriteLine("Created by {0}", list.Author.Name);
Console.WriteLine("{0}", list.Description);
Console.WriteLine("------------------------");
foreach (SPListItem item in list.Items)
{
Console.WriteLine("\t * {0}", item.Title);
}
Console.WriteLine("\n");
Console.WriteLine("................................");
}
Console.WriteLine("\n{0} lists found.", web.Lists.Count);
Console.ReadKey();
}
}
}

Make sure you add reference to Microsoft.SharePoint.dll assembly prior writing the code above.

 

Managing Lists

When working with Lists, the source container is always an object created from SPListCollection. Given code give below, I create two Task Lists, selecting the “Task” template.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace ManagingLists
{
class Program
{
static void Main(string[] args)
{
SPSite rootSite = new SPSite("http://dc:8844");
SPWeb rootWeb = rootSite.AllWebs[0];
SPListTemplate sourceTemplate = rootWeb.ListTemplates["Tasks"];
Guid newListGuid = rootWeb.Lists.Add("My First Task List",
"My First Task List", sourceTemplate);
Guid secondNewListGuid = rootWeb.Lists.Add("My Second Task List",
"My Second Task List", sourceTemplate);
SPList newList = rootWeb.Lists[newListGuid];
SPList secondNewList = rootWeb.Lists[secondNewListGuid];
secondNewList.Delete();
newList.Description = "My Custom Task List - From Code";
newList.Update();
Console.WriteLine("List creation completed.");
Console.ReadLine();
}
}
}

Browse your SharePoint site, where you could see both the Task Lists you created using code above.

 

Managing List Items

Managing List Items is same as managing Lists. Okay, let’s add a task item in to task list in code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
namespace ManagingListItems
{
class Program
{
static void Main(string[] args)
{
SPSite rootSite = new SPSite("http://dc:8844");
SPWeb web = rootSite.AllWebs[0];
SPList taskList = web.Lists["My Task List"];
SPListItem newTask = taskList.Items.Add();
newTask["Title"] = "My Task List Title";
newTask["DueDate"] = SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now.AddDays(30));
newTask["PercentComplete"] = 0.1;
newTask.Update();
newTask = taskList.Items.Add();
newTask["Title"] = "This Task will be deleted.";
newTask.Delete();
taskList.Update();
Console.WriteLine("Work completed.");
Console.ReadLine();
}
}
}

Browse your SharePoint site, go to “My Task List”. Congratulations, you have successfully created a list item

 

Summery

The SharePoint object model is so powerful that you have the full control over each and every component SharePoint provides. This article provides you an entry point to accessing and managing SharePoint Lists in code. [Sample code courtesy SharePoint SDK and Sams Publishing]

All the source code for sample applications will be posted in the blog given below.

 

About the Author

Prabath Fonseka is currently working as a Tech Lead having over 9 years of experience in the computer software systems architecture, analysis, design and development in the software development sector including teaching computer software packages at academic sector.

You are welcome to send comments to prabathfonseka@gmail.com

 

Your rating: None Average: 5 (2 votes)

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.