Looking for something?

|---------|--------------------------------------------------| | Author: | Tobias Zimmergren (tobias at zimmergren dot net) | | Blog: | http://www.zimmergren.net |


In my previous article title "How to: Get up and running with .NET 3.5 in your SharePoint environment" I talked about how you can manually set up your SharePoint environment for use with Microsoft .NET Framework 3.5.

In this article I will talk about how you can incorporade some of the technologies used in .NET 3.5 to query a SharePoint list. More precisely, I will talk about how you easily can use LINQ from the .NET 3.5 framework to get started with .NET 3.5 in SharePoint.

A more in-depth article might be posted later, but this one is simply providing simple code instructions to what a Web Part that utilizes LINQ can look like!


In order to follow along with this walkthrough, you should have the following bulletpoints checked:

Creating a custom Web Part which utilizes .NET 3.5 in SharePoint

The code below will give you the heads up on how to fetch the SPListItem objects from an SPList object and sort them alphabetically. You can of course use the ‘where’-clause with LINQ aswell to filter out which objects (SPListItem objects) to fetch from the SPList.

Example task list to fetch the data from: image

Using the following code, I’ve used LINQ to retreive all the list items and sort them ascending by title!

using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.SharePoint;

namespace Zimmergren.net35.LINQWebPartSample {     public class SPLINQ : System.Web.UI.WebControls.WebParts.WebPart     {         protected override void Render(System.Web.UI.HtmlTextWriter writer)         {             base.Render(writer);             SPList taskList = SPContext.Current.Web.Lists["Tasks"]; 

*            // Get items, order by title alphabetically and assign to taskListItems*             var taskListItems = from SPListItem tItem in taskList.Items                                 orderby tItem.Title                                 ascending select tItem; 

            foreach (SPListItem taskItem in taskListItems)                 writer.WriteLine(taskItem.Title + " n");         }     } }

The bold text in the above code block is the LINQ statement to fetch all items in the SPList and order them ascending by Title. You can of course make much more complex queries using LINQ in order to fetch other objects based on different criteria.

This will produce a simple output like this, sorting the items alphabetically: image

Summary and Download

This was a very (very) basic and simple example of how you can use any .NET 3.5 technology to get started with some new cool stuff.

I utilized a basic LINQ expression in this article which of course can be heavily modified if you want to retreive other things from your list(s). Perhaps a future article will take on some more advanced LINQ expressions in conjunction with SharePoint?! :-)

Anyway, you can download the sample project from here: Zimmergren.net35.LINQWebPart.zip

Thanks for tuning in and please leave a comment

As always, there’s plenty of readers but few people showing their appreciation through the comments – please leave a comment :)

Hope it helps :-)

Technorati Tags: Microsoft, SharePoint, MOSS, WSS, LINQ, .NET, Integration

0 Comments 28 September 2008
Tobias Zimmergren

Tobias Zimmergren

Hi, I'm Tobias. I am a Microsoft MVP for SharePoint and I use this site to share my thoughts on tech with you on topics like SharePoint, Office 365, Azure and general web development.

  Malmö, Sweden

Related Post

Comments powered by Disqus

Sign up to my e-mail newsletter to get news about Azure, SharePoint and Office 365 and get instant notifications when I publish new posts!

Enter valid email