How to: LINQ with SharePoint - .NET 3.5 Framework with SharePoint Part 2

Tobias Zimmergren
Tobias Zimmergren

| Author: | Tobias Zimmergren (tobias at zimmergren dot net) |
| Blog: | http:https: |


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:

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)
            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 + "

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:

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:

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

SharePointLINQ.NET 3.5

Tobias Zimmergren Twitter

Hey, I'm Tobias! I write about my experiences in designing, architecting, securing, and operating distributed cloud services. Nice to meet you 👋

Reactions and mentions

Hi, I'm Tobias 👋

Tobias Zimmergren profile picture

Find out more about me.

Recent comments

Recommended books!

Prepare for the AZ-500 exam!
Exam Ref AZ-500 Microsoft Azure Security Technologies
Azure Security Handbook!
Azure Security Handbook: A Comprehensive Guide for Defending Your Enterprise Environment
Prepare for the SC-900 exam!
Exam Ref SC-900 Microsoft Security, Compliance, and Identity Fundamentals