U2U Caml Query Builder (windows version) folder support

It has been a while that I worked on the Windows version of the U2U Caml Query Builder. In fact we were thinking about the replacement of it by the Feature version. But it seems a lot of developers are still working with the Windows version.

Recently I got a question about querying items in sub folders of a document library, for which support is missing in the current versions of the query tool.

If you use the object model you typically use an object of type SPQuery. In that case you can set properties of the object to query sub folder:

  • Set the ViewAttributes property to "Scope=\"Recursive\"" to query the root folder and sub folders of a certain document library
  • Set the Folder property to the name of a folder to search through the items in a certain sub folder.

Following a post of Mart Muller, you can also use SPSiteDataQuery to query through all folders and sub folders of a document library: http://blogs.tamtam.nl/mart/CommentView,guid,3fca3180-6dd5-40c1-9509-8b785d306e7b.aspx

If you need to pass your CAML queries through the web services of SharePoint, you need to include the necessary CAML into your query. As the windows version of the U2U Caml Query Builder supports both working with the object model as working with the web services, I modified the tool so that it can help you building your queries for sub folder support.

You can download the new windows version here.

I will not leave you without any word of explanation. So, for those who are interested, here is how you can build a CAML query with folder support.

When you start the windows version of the tool, you have to specify the URL to your SharePoint site. You can specify some credentials if necessary, and you can choose if you want to work through the object model or through the web services of SharePoint. So click the latter.

Login

Click the Connect button. In the left pane a treeview will be populated with the lists and document libraries of the SharePoint site of your choice.

You can build a good old query by selecting a list, but you can also build a query for use with web services, meaning you can get the necessary xml for the Query node, the ViewFields node and the QueryOptions node required by the GetListItems method of the Lists web service. If this is the case, right-click the list of your choice and select GetListItems from the context menu.

GetListItems

This shows you a tabbed control. In the View Fields tab you can select the fields you want to have included in the result.

ViewFields

Click the Query tab to build the query itself. Build your query like you always do: select the fields you want to include in the Order By clause; select the fields and set the criteria for the fields you want to include in the Where clause. Nothing new until now.

Query

The document library I use in this example has the following structure:

  • 1 document in the root folder with the name "SharePoint developers course.docx"
  • 1 document in the folder "SharePoint For Power Users" with the name "Chapter 1.rtf"
  • 1 document in the folder "SharePoint For Developers" with the name "Chapter 2.doc"

So I build a query to find my file starting with the name "c" (which is located in the sub folder).

To set some extra query options, click the Query Options tab and now pay attention! If you leave all settings like in the screen shot, and you press the Test button, you will get no result back. This is because the default is that only the root folder is queried.

No QueryOptions

If you want to query a certain sub folder, you can check the "Query sub folder" check box. The tool fills out the name of the document library because this is the root of the path, and you can add the name of a sub folder. You see in the screen shot that my document is returned in the result.

image

The query generated by the tool will look like the following:

image

If you want to look through all folders and sub folders, click the "Query all folders and sub folders" check box. This will include the <ViewAttributes> element into the <QueryOptions> element.  When the query executes against my document library, the 2 documents in the 2 sub folders start with "c" and are returned as result.

image

The CAML generated by the tool looks as follows:

image

In your code you can load the generated xml into an xml document and use it as follows:

image

Have fun!