Tuesday, May 11, 2010

Microsoft SharePoint 2007 Development – Fun with Lists

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();
}
}
}
image 

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();
}
}
}

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

2 comments:

Sharepointengine said...

Hello,
nice blog and good information provider
SharePoint Development means integrating SharePoint into the existing software development environment. SharePoint is a Microsoft Product, which is used to develop information portals in the organization. These information portals enable the people to connect and collaborate with the information available in the organization.

SharePoint is a fastest growing product and is implemented by over 18000 companies all over the world. It provides you various benefits

Thanks
Sharepoint Consulting

Abdul Hafees said...

Nice article.