Click here to Skip to main content
15,884,176 members
Articles / Productivity Apps and Services / Sharepoint

Recursive and Folder-Scoped LINQ Queries in SharePoint 2010

Rate me:
Please Sign up or sign in to vote.
1.00/5 (2 votes)
24 Oct 2010CPOL1 min read 22.8K   4   2
Tips for LINQ to SharePoint

Introduction

As you might have 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):

C#
qry.ViewAttributes = "Scope='Recursive'";

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

C#
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:

C#
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.

C#
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!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Egypt Egypt
Ayman El-Hattab is a Regional Technology Solution Professional focusing on helping software professionals and organizations build better SharePoint Solutions using Microsoft Application Lifecycle Management (ALM) technologies and tools. Ayman has been in this role since 2010 and has presented at many conferences all over the Middle East & Africa about ALM, SharePoint, C#, asp.net and Business Intelligence technologies. Ayman is also a Microsoft Most Valuable Professional [SharePoint MVP] , ALM Ranger, published author and an enthusiastic speaker who enjoys working with the online and offline developer communities all over the world. Ayman is the founder of MEA ALM Community & SharePoint4Arabs, community lead at Egypt SharePoint User Group and an organizer of several SharePoint Saturday events. Outside of work, Ayman can be found watching soccer games, playing xbox or watching documentary movies.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Oslec27-Apr-11 21:33
Oslec27-Apr-11 21:33 
General[My vote of 1] not an article Pin
Not Active13-Apr-11 16:25
mentorNot Active13-Apr-11 16:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.