Presently sponsored by: ScriptRunner - Get your free PowerShell Security e-Book!
| Author: | Tobias Zimmergren (tobias at zimmergren dot net) |
| Blog: | http:https:https://zimmergren.net//zimmergren.net//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:
- .NET Framework 3.5 is installed
- You’ve configured your SharePoint environment for use with .NET 3.5 - See this article for more information and help on this issue: How to: Get up and running with .NET 3.5 in your SharePoint environment
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!
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
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: 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 :-)