Answered by:
Is there a CAML query builder that works?

Question
-
It is simple to build a caml query with one or two conditions but if there are five or more things become a little bit more difficult. Microsoft P&P put CamlQueryBuilder classin there guidance library but it only works with one or two conditions. I wonder if somebody wrote CAML query builder that really works (I am talking about code that generates CAML, not a utilty like U2U, etc)Wednesday, August 25, 2010 1:01 PM
Answers
-
LINQ to SharePoint actually generates CAML under the covers for you. If you create a LINQ query and then use the Log() method, it will spit out the CAML query that was generated for you:
http://msdn.microsoft.com/en-us/library/ee538250.aspx
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.linq.datacontext.log.aspx
blog.beckybertram.com | RSS | @beckybertram- Proposed as answer by Joe Capka Thursday, August 26, 2010 1:37 PM
- Marked as answer by Tobias ZimmergrenMVP Wednesday, September 1, 2010 9:00 PM
Wednesday, August 25, 2010 1:36 PM
All replies
-
LINQ to SharePoint actually generates CAML under the covers for you. If you create a LINQ query and then use the Log() method, it will spit out the CAML query that was generated for you:
http://msdn.microsoft.com/en-us/library/ee538250.aspx
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.linq.datacontext.log.aspx
blog.beckybertram.com | RSS | @beckybertram- Proposed as answer by Joe Capka Thursday, August 26, 2010 1:37 PM
- Marked as answer by Tobias ZimmergrenMVP Wednesday, September 1, 2010 9:00 PM
Wednesday, August 25, 2010 1:36 PM -
Hi Stan have you tried the stramit camlquerybuilder?
And if it is 2010 why don't you use LINQ to SharePoint.
kind regards,
Paul Keijzers
Check my website http://www.kbworks.nl or follow me on @KbWorks be sure to Check my KbWorks blogWednesday, August 25, 2010 1:49 PM -
Hi Stan have you tried the stramit camlquerybuilder?
Paul:
The reason is I am not using LINQ to SharePoint is because query is dynamic. It is possible to build a dynamic query with LINQ, but I think it will be less efficient than the old way. Problem, though, I need to build CAML, which I was hoping not to if I there is caml query builder class that works.
-Stan
Wednesday, August 25, 2010 3:12 PM -
Stan,
What I do is this:
- In the browser, define a view that implements the query I want, i.e. define the view fields, sort, filter, etc.
- Open Server Explorer in Visual Studio 2010 and drill down to the view.
- Copy the XML from the Query property or the HtmlSchemaXml property
Nice and easy and I don't have to use any other tools than the standard kit.
--Doug
Wednesday, August 25, 2010 3:39 PM -
Stan,
What I do is this:
- In the browser, define a view that implements the query I want, i.e. define the view fields, sort, filter, etc.
- Open Server Explorer in Visual Studio 2010 and drill down to the view.
- Copy the XML from the Query property or the HtmlSchemaXml property
Nice and easy and I don't have to use any other tools than the standard kit.
--Doug
Doug:I understand that. I need the CAML build dynamically, meaning that there are a number of text boxes, dropdowns, date time pickers on the page, and depending on what user selects, the caml needs to be genereated.
-Stan
Thursday, August 26, 2010 12:01 PM -
Did you try U2U CamlQueryBuilder? I think you need to end up using tools like this. The SharePoint 2007 version still works with SharePoint 2010. I have used it quite a few times with SharePoint 2010.
If you want to create your own query, there is no Caml Query Editor out of the box.
I usually have a StringBuilder class where I keep appending the query to the string depending on conditions/values. Even better, sometimes I do something like this:
<Contains> <FieldRef Name='Title'/> <Value Type='text'>{0}</Value> </Contains>
And now I can use String.Format method to insert my {0} argument with the value.
Regards,
Chakkaradeep || SharePoint Solutions Specialist - MCTS SharePoint Dev, WSS Dev, MCPD SharePoint Developer 2010 || http://www.intergen.co.nz || Twitter: http://twitter.com/chakkaradeep || http://www.chakkaradeep.comThursday, August 26, 2010 12:35 PM -
u2u camlquerybuilder works for me too...Thursday, August 26, 2010 1:31 PM
-
If you are not restricted by the limitations of LINQ to SharePoint (anonymous users, etc), use it. It is EXACTLY what you are looking for, all it does is generate CAML queries under the hood. The performance is really quite good so unless you really suffer from performance issues, there is no need to look elsewhere.
http://jcapka.blogspot.comThursday, August 26, 2010 1:36 PM -
I made a JS camlbuilder. I don't know if you van rebuild it to your needs in C#
http://kbworksjscamlbuilder.codeplex.com/
Kind regards,
Paul Keijzers
Check my website http://www.kbworks.nl or follow me on @KbWorks be sure to Check my KbWorks blogThursday, August 26, 2010 2:56 PM -
If you are not restricted by the limitations of LINQ to SharePoint (anonymous users, etc), use it. It is EXACTLY what you are looking for, all it does is generate CAML queries under the hood. The performance is really quite good so unless you really suffer from performance issues, there is no need to look elsewhere.
http://jcapka.blogspot.com
Joe:Let's say you are building a dashboard with selection criterias consisting of five text boxes and one dropdown (status, date start, date end, etc). Not all of these control can be populated, there can be one or two or all six of them. Can you tell how will build LINQ to SharePoint query in this case?
-Stan
Friday, August 27, 2010 3:10 PM -
If I understand, you will be fetching list items based on the filter criteria provided in some or perhaps all the text boxes and/or drop down.
LINQ is actually great for such scenarios since you can build up your query in steps, and then execute only once you are ready. So your query would look something like:
// Get DataContext from page context DataContext data = new DataContext(SPContext.Current.Web.Url); // Get the SharePoint list EntityList<Customer> Customers = data.GetList<Customer>("Customers");
// add filter 1 if(!string.IsNullOrEmpty(myFilter1.Text)){ Customers = Customers.Where(c=>c.Name == myFilter1.Text); }
// add filter 2 if(!string.IsNullOrEmpty(myFilter2.Text)){ Customers = Customers.Where(c=>c.City == myFilter2.Text); }
// execute query by calling ToList() IList<Customer> filteredCusts = Customers.ToList();
I don't have a place to test this code right now, so it's all out of memory, but the concept should be there even if I messed up some syntax.
Mainly notice how I am conditionally adding Where clauses to the LINQ query depending on whether or not the filter value is present.
Hope that helps
http://jcapka.blogspot.com- Proposed as answer by Joe Capka Friday, August 27, 2010 5:22 PM
Friday, August 27, 2010 3:35 PM -
If I understand, you will be fetching list items based on the filter criteria provided in some or perhaps all the text boxes and/or drop down.
LINQ is actually great for such scenarios since you can build up your query in steps, and then execute only once you are ready. So your query would look something like:
Friday, August 27, 2010 4:47 PM -
Hi Stan B,
Thanks for your post.
May be the “SharePoint Friendly Query” is what you want. It is a query builder library which release you from the complex CAML. Just use T-SQL liked gramma as "SELECT * from Announcement WHERE Title CONTAINS 'Erucy' ORDER BY ID DESC".
You can download it from: http://fquery.codeplex.com/
Sunday, August 29, 2010 3:00 AM