Category Archives: LINQ

Recycling list items using LINQ to SharePoint

Another interesting method that I found while investigating LINQ to SharePoint is the RecycleOnSubmit method.



If you want to move an item to the Recycle Bin, use the first method but if you want to entirely delete it use the second one.

And of course don’t forget to call context.SubmitChanges() to commit the changes to the content database.


Recursive and Folder-Scoped LINQ Queries in SharePoint 2010

As you might experienced in SharePoint 2007, a CAML query is by default non recursive. In other words, if you execute a CAML query, you will end up with items from the list root folder. This is still the same in SharePoint 2010. You have to define extra query options as shown below in case you want to query all folders and sub folders within a list (Recursive Query) :

qry.ViewAttributes = “Scope=’Recursive'”;

And if you want to scope your query to a certain folder :

qry.Folder = list.ParentWeb.GetFolder("Folders DocLib/2008");

What about LINQ to SharePoint ?

The default behavior is the same, however if recursive querying is the desired behavior, you can use the ScopeToFolder method from your EntityList<T> object.

Note the following queries:

var q = dc.Projects.Where(p => p.DueDate < DateTime.Now.AddMonths(1));
var q = dc.Projects.ScopeToFolder("", true).Where(p => p.DueDate < DateTime.Now.AddMonths(1));

The first one executes against the list root folder while the second one is recursive. You might be wondering about the empty string that I’m passing to ScopeToFolder method. Well, let’s see what MSDN says about the parameters that the method expects.

public IQueryable<TEntity> ScopeToFolder( string folderUrl, bool recursive )

folderUrl –> The URL of the folder.
recursive –> true to include items in subfolders; false to exclude them.

That’s it, if you want to scope your LINQ query to a specific folder, pass the folder URL as the first parameter to the method & “true” if you want the recursive behavior of the query. In the second query, I just wanted the recursive behavior and I didn’t want to scope my query to any folders so I passed an empty string, this does the trick.

Hope this helps!

Using Lambda Expressions with RunWithElevatedPrivileges

Elevation of privilege, a feature that was added in WSS 3.0 & MOSS 2007, enables you to programmatically perform actions in code by using an increased level of privilege. The SPSecurity.RunWithElevatedPrivilegesmethod enables you to supply a delegate that runs a subset of code in the context of an account with higher privileges than the current user.

For some reason, I have never seen a code snippet that uses a lambda expression instead of the anonymous method syntax so I thought of showing you how to save some typing by using Lambda.

Commonly used syntax:

{     // Do things by assuming the permission of the "system account".


Much easier syntax:

{     // Do things by assuming the permission of the "system account".

When I asked the following question on Twitter : “Why do people still use the anonymous method syntax rather than Lambda Expressions with SPSecurity.RunWithElevatedPrivileges() ? #SharePoint”

I got the following answer from Todd Bleeker : “Delegate syntax has been around a lot longer. Lambda is still confusing and even scary for most. <Todd />”

I totally agree with Todd, but guys if you are not comfortable with LINQ and lambda expressions, you should familiarize yourself with them before diving deep into developing SharePoint 2010 solutions.

As you all might know, Now in SharePoint 2010 you can use LINQ syntax to retrieve items from your lists instead of using CAML queries. Microsoft has included a LINQ provider for SharePoint in the Microsoft.SharePoint.Linq namespace, some classes in this namespace are responsible for translating the LINQ Queries to CAML ones which are eventually translated into SQL. You can still write CAML Queries and fetch items in the old ways but LINQ can really make your life easier specially when it comes to joins.

LINQ is Better Than ForEach


LINQ is Better Than ForEach

Chris Eargle has done a great job comparing LINQ and ForEach loops from the performance and maintainability perspective.

Here is Chris’ conclusion: The vast majority of code I come across that either can be written in LINQ or refactored to LINQ has no noticeable, negative performance impact, but it has a positive impact on maintainability.

Click here to read the full post !

Querying MSMQ Messages Using LINQ

In a project that I’m currently working on, I needed to retrieve messages from MSMQ based on some criteria, I used LINQ to do that and would like to share this code with you. MessageQueue messageQueue = new MessageQueue(Constants.MessageQueueName); messageQueue.Formatter = new BinaryMessageFormatter(); var TrackingRecords = from System.Messaging.Message message in messageQueue where message.Label.Contains(Constants.MessageLabel) select (ICLGRecord)message.Body; // Itterate through the Messages foreach (var TrackingRecord in TrackingRecords) { } // Remove All Items From MSMQ messageQueue.Purge();