Accessing the List Items in a List

Programming

For starting with we can create a SharePoint Console Application. Use the New Project > SharePoint > Console Application project item.

Accessing the List Items in a List

For proceeding with this example create a list derived from the Contacts template and name it as "My Contacts". Add some items inside it with First Name and Last Name set.

Use the following code inside the Console application and execute it.

using (SPSite site = new SPSite("http://appes-pc/my/personal/dotnet")) // Site Collection

{

using (SPWeb web = site.OpenWeb()) // Site

{

SPList list = web.Lists["My Contacts"]; // List

int i = 1;

foreach (SPListItem item in list.Items)

{

Console.WriteLine("Item: " + (i++).ToString());

Console.WriteLine(item.Name);

Console.WriteLine(item.Title);

Console.WriteLine(item.GetFormattedValue("First Name"));

Console.WriteLine(item.GetFormattedValue("Last Name"));

Console.WriteLine(string.Empty);

}

}

}

Console.ReadKey(false);

If you receive any build errors regarding namespace, change the Project Properties > Target to .Net Framework 3.5.

Now try building the application and it should work fine. You can see the following output. SharePoint 2010 Administration & Development



www.jeanpaulva.com 104




Please note the usage of Title, Name properties and the GetFormattedValue() method.

Accessing the items in a Library

Now we can proceed with accessing the document library items programmatically. For proceeding with you need to create a document library inheriting from Document Library template and name it as "My Docs". Upload one or two documents into it. We are proceeding to get the file name and length in this example.

Enter the following code in the console application.

using (SPSite site = new SPSite("http://appes-pc/my/personal/dotnet")) // Site Collection

{

using (SPWeb web = site.OpenWeb()) // Site

{

SPDocumentLibrary library = web.Lists["My Docs"] as SPDocumentLibrary; // Library

int i = 1;

foreach (SPListItem item in library.Items)

{

Console.WriteLine("Item: " + (i++).ToString());

Console.WriteLine(item.File.Name);

Console.WriteLine(item.File.Length);

Console.WriteLine(string.Empty);

}

}

}

Executing the application you can see the following results


SPControl
 
The SPControl class acts as the base class while developing server controls. It resides in the namespace Microsoft.SharePoint.WebControls.

SPControl provides static methods that returns reference to the current site, web, web application, module. The methods are:
 
SPControl.GetContextSite()

SPControl.GetContextWeb()

SPControl.GetContextWebApplication()

SPControl.GetContextModule()
 
SPException
 
The SPException class can be used to handle exception inside a try catch block. It represents the exceptions thrown by the server object model.
 
try

{

// Code here

}

catch (SPException ex)

{

// Handle Exception

}
 
SPUser
 
The SPUser class can be used to access user information for a SharePoint site. Enter the following code to get the number of users and their names.
 
using (SPSite site = new SPSite("http://appes-pc/my/personal/dotnet"))

{

using (SPWeb web = site.OpenWeb())

{

foreach (SPUser user in web.AllUsers)

Console.WriteLine("User: " + user.Name);

}

}
 
On running the code you can see the results based on your machine users. SharePoint 2010 Administration & Development



www.jeanpaulva.com 106
 



Note
 
You can add a new list item or library item using the Items.Add() method. For updation and deletion use the item.Update() and item.Delete() methods respectively. More code coverage on these areas will be provided later.

Comments