Tuesday, March 15, 2011

Getting CAML Query from an existing view

Scenario:
Colleague of mine was working on a small WP and she has to retrieve data based on some date-time criteria. No this can be tricky for someone new to SP unless you know all the syntax.

Solution:
1. We all know about CAML Builder , we can use that to generate the CAML.
2. Another interesting idea is to create a view from UI. I mean go to browser and create a view with all the conditions you need.

That way you can see the results right away as if query is returning correct records or not.

Question: : All that is ok but how will I get to know whats the real CAML behind this view.
Answer :
Option 1 : You can use SharePoint Manager to find it. I love this tool. :)
Option 2 : Url Protocol ( Something handy when you cant install SP Manager)

Steps for Url Protocol:
1. Create a new view with all the conditions and fields you want in the view.
2. Once view is ready , go to edit View page and look at url
3. Copy the List query parameter value in a notepad.
4. Now browse to the url below with proper list id value

http://Server_Name/[sites/][Site_Name/]_vti_bin/owssvr.dll?Cmd=Display&List=GUID&XMLDATA=TRUE
5. This will give you complete schema and then you can find the newly created view in that.
6. If you look closely you can find the Query XML node with the CAML used by this view and also the Fields ( with internal name ) used by the view :)

Articles:
MSDN , Exploiting the value of OWSSVR.DLL in SharePoint 3.0

7 comments:

ADIL,  March 22, 2011 at 2:35 AM  

Nice one, very helpful in various scenarios.

Chu April 18, 2011 at 10:18 AM  

Thank you, its help my project.
I got the Linq XML from Visual Studio 2010/Solution Explorer when connected to SharePoint list view

Jack Myers,  September 22, 2011 at 2:53 PM  

One of the most useful SharePoint posts I've seen. Thank you, thank you, thank you!

James June 6, 2012 at 10:38 PM  

Can't see CAML from xml file! Just schema and query result set. Please help

Anonymous,  June 6, 2012 at 10:39 PM  

Can't see CAML!

Sandeep K Nahta June 6, 2012 at 11:30 PM  

Check out the links in the last section of the blog for more details

http://www.synergyonline.com/Blog/Lists/Posts/Post.aspx?ID=123

http://msdn.microsoft.com/en-us/library/ms478653.aspx

Ravi Chander Gunishetti August 25, 2015 at 2:58 PM  

This article no longer working and i suggest to use U2U CAML Query builder...i have tried all the links mentioned by Sandeep and none of them are working.