Learning SharePoint   Enhance Your SharePoint Knowledge

Subscribe to Learning SharePoint Subscribe
Twitter Learning SharePointFollow Us
in Learning SharePoint
Check Out Our New SharePoint 2013 series!

Get list items in Sharepoint 2010

07/09/2010  
Category: SharePoint 2010


In SharePoint 2010, you have various new classes and namespaces to facilitate quick and easy data retrieval. Lets discuss advantages, disadvantages and code snippets for some of the methods that you can use.

Using Client Object model -

The Client Object Model (OM) is a new programming interface for SharePoint 2010 where code runs on a user’s client machine against a local object model and interacts with data on the SharePoint Server.
Used When - When you need to access sharePoint data from a remote application for e.g. need to access data from WPF or Windows application using client OM like javascript.

Advantages :
. You can execute or line up all the server tasks in one SharePoint server server call.
. It returns you Json object that you can bind to a client side object.
. Used when a SharePoint List\site object is needs to be returned.
. Anything can be accessed down to site collection level.

Disadvantages :
. Can only use relative url of sharepoint site. You cannot do cross-site scripting.
. Not good for accessing Relationship items. For e.g. accessing Parent items and then its related child items.

Code Snippet - Code snippet to retrieve data from “Test list” using Caml query

ClientContext clientContext = new ClientContext(“http://SPSite”);
List list = clientContext.Web.Lists.GetByTitle(“Test list”);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml =

@”<View><Query><Where>

<Eq><FieldRef Name=’Category’/><Value Type=’Text’>Development</Value>

</Eq></Where></Query><RowLimit>100</RowLimit></View>”;

ListItemCollection listItems = list.GetItems(camlQuery);
clientContext.Load(
listItems,
items => items
.Include(
item => item["Title"],
item => item["Category"],
item => item["Estimate"]));
clientContext.ExecuteQuery();
foreach (ListItem listItem in listItems)
{
Console.WriteLine(“Title: {0}”, listItem["Title"]);
Console.WriteLine(“Category: {0}”, listItem["Category"]);
Console.WriteLine(“Estimate: {0}”, listItem["Estimate"]);
Console.WriteLine();
}

Using ECMAScipt

See the Post Get all list items using ECMAscript SharePoint 2010

Using Server Object Model –

Access data using SharePoint 2010 API’s

Used when - When you need to access data from an application which is on the SharePoint Server itself. For e.g. Retrieving data in a Custom webpart.

Advantages :
. Can use CAML query or LINQ to return data and bind it with asp.net controls.
. Get site\List object for further query.

Disadvantages :

. Wont work on Computer not having SharePoint installed.
. Cannot use API’s in a client side application.

Code Snippet -
Query to get all the Items from a list where Category field is equal to “Sp2007″

// Get SiteColl
SPSite curSite = new SPSite(“http://myPortal”);

//Get Web Application
SPWeb curWeb = curSite.OpenWeb();

// Create a SPQuery Object
SPQuery curQry = new SPQuery();

//Write the query (I suggest using U2U Query Bulider Tool)
curQry.Query = “<Where><Eq><FieldRef Name=’Category’ />
<Value Type=’Text’>
SP2007 </Value></Eq></Where>”;

// Set the Row Limit
curQry.RowLimit = 100;

//Get the List
SPList myList = myWeb.Lists["ListName"];

//Get the Items using Query
SPListItemCollection curItems = myList.GetItems(curQry);

// Go through the resulting items
foreach (SPListItem curItem in curItems)
{
string ResultItemTitle = curItem["Title"].ToString();
}

Using REST –

REST (Representational State transfer) is a protocol (powered by ADO.NET services) which is used for getting data out of sharepoint via Url.

The syntax of the url would be

http://SPServer/_vti/bin/ListData.svc/{Entity}[({identifier})]/[{Property}]

In above statement -
ListData.svc – This is a wcf srevices that follows REST protocol.
Entity – Is SharePoint List name.
Identifier – Can be Id of the List item.
Property – Column of the Item.

You can also get Filtered data via Url

For e.g. below url will get data from Cities list where City = ‘NJ’

/_vti/_bin/ListData.svc/{Entity}?filter=Cities/ City eq ‘NJ’

Advantages :
. You can quickly access data from sharepoint even when you are not in the sharepoint context.
. It can do cross site scripting Supported by IE 8.
. Good for accessing Relational data like Parent and child since the result is an xml with all the parent child nodes.

Disadvantages :
. Does not return an object for Site\List.

Using LINQ to SharePoint -

LINQ is the Entity based programming on the server. It is mostly used for performing LINQ queries on SharePoint list\libraries.

Used When - When you need get data from a rich relational structure or need to perform join queries on lists, even when they are not related.

Advantages :
. Fast data retrival.
. Can write SQL like queries.
. Returns xml output, thus can be used to reterive relational (Parent-child) data.

Disadvantages :
. Cannot use LINQ in Client OM. It runs on server only.
. It is converts the LINQ query into CAML therefore, adding an extra step for data retrieval.

Code snippet -

// Get DataContext from page context
DataContext data = new DataContext(SPContext.GetContext(this.Context).Web.Url);

// Get the SharePoint list
EntityList Customers = data.GetList(“Customers”);

// Query for customers from London
var londonCustomers = from customer in Customers
where customer.City == “London”
select customer;

foreach (var londonCust in londonCustomers)
{
Console.Writeline(“id = {0}, City = {1}”, londonCust.CustomerId, londonCust.City);
}

Subscribe to Learning SharePoint Subscribe
Twitter Learning SharePointFollow Us
in Learning SharePoint
Advertisement

Random Posts

Programmatically add audience for web parts sharepoint 2010
Programmatically add audience for»

In this Post we will set the audience for a WebPart which Web Parts page, you can use the SPLimitedWebPartManager»

Number of items sites and Site Collections in SharePoint 2010
Number of items sites»

Site collection limits The following table lists the recommended guidelines for site collections. Number of Sites – 250,000 per site»

Monitoring SharePoint 2010 – Tutorial
Monitoring SharePoint 2010 –»

To ensure the availability and reliability of your SharePoint Server 2010 environment, you must actively monitor the physical platform, the»

Recent Forum Posts

Linq to Sharepoint

by RS on May 18, 2012

comments on the code

by spbsmile on May 18, 2012

connection(add) EditControlBlock and my program

by spbsmile on May 18, 2012

Custom master pages used in multiple SharePoint 2010 sites

by ubersteve on May 18, 2012

upload file and click on button but cant upload file into list by using below code

by santhoshreddy on May 18, 2012