Click here to Skip to main content
Licence CPOL
First Posted 24 Oct 2010
Views 6,475
Bookmarked 4 times

Recursive and Folder-Scoped LINQ Queries in SharePoint 2010

By | 24 Oct 2010 | Article
Tips for LINQ to SharePoint
 
Part of SharePoint Zone sponsored by
See Also

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):

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!

License

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

About the Author

Ayman M. El-Hattab

Architect

Egypt Egypt

Member

Ayman El-Hattab is a SharePoint MVP, Developer, Author & Speaker. He is a Microsoft Certified Solution Developer as well as a Microsoft Certified Technology Specialist in SharePoint. Ayman writes articles about topics in relation to SharePoint and its related technologies for online magazines, publicly speaks for numerous user groups, offline communities and SharePoint Saturdays, is the founder of www.SharePoint4Arabs.com, organizes events for Egypt SharePoint User Group and you will always find him active at MSDN and Technet forums. Ayman has also been recognized by Microsoft as Most Valuable Professional (MVP) in April 2010.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberOslec21:33 27 Apr '11  
General[My vote of 1] not an article PinmvpMark Nischalke16:25 13 Apr '11  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 24 Oct 2010
Article Copyright 2010 by Ayman M. El-Hattab
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid