{"id":376,"date":"2011-09-02T05:32:47","date_gmt":"2011-09-02T05:32:47","guid":{"rendered":"http:\/\/www.stuartroberts.net\/?p=376"},"modified":"2011-10-04T05:01:29","modified_gmt":"2011-10-04T04:01:29","slug":"sharepoint_caml_helper","status":"publish","type":"post","link":"http:\/\/www.stuartroberts.net\/index.php\/2011\/09\/02\/sharepoint_caml_helper\/","title":{"rendered":"SharePoint CAML Helper Update"},"content":{"rendered":"<p>A couple of years ago I wrote a CAML helper project and uploaded the source code to CodePlex.<\/p>\n<p>Recently I updated the code with a few small bug fixes, after some feedback from a colleague who used it successfully on a large scale Portal project.<\/p>\n<p>As a result, I thought I&#8217;d write a short post to let those who weren&#8217;t aware of the code that it was there.<\/p>\n<p>To view the C# source for the tool, or download the latest assembly, please follow <a href=\"http:\/\/sharepointcamlhelper.codeplex.com\" target=\"_blank\">this link<\/a>.<\/p>\n<p>To create CAML queries with the assembly, work with the CAMLManager class to build your query.<\/p>\n<pre lang=\"csharp\">\r\nCAMLManager mgr = new CAMLManager();\r\n\r\nmgr.QueryGroups.Add(new QueryGroup(\"Title\", Types.FieldTypes.Text, Types.QueryTypes.Eq, \"A Title\"));\r\nmgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.Or, \"Title\", Types.FieldTypes.Text, Types.QueryTypes.Eq, \"Another Title\"));\r\n\r\nstring caml = mgr.GetCAML();\r\n<\/pre>\n<p>The GetCAML method will return the following string:<\/p>\n<pre lang=\"xml\">\r\n<Where>\r\n  <Or>\r\n    <Eq>\r\n      <FieldRef Name=\"Title\" \/>\r\n      <Value Type=\"Text\">A Title<\/Value>\r\n    <\/Eq>\r\n    <Eq>\r\n      <FieldRef Name=\"Title\" \/>\r\n      <Value Type=\"Text\">Another Title<\/Value>\r\n    <\/Eq>\r\n  <\/Or>\r\n<\/Where>\r\n<\/pre>\n<p><!--more--><br \/>\nAnother example, with a more nested query could be:<\/p>\n<pre lang=\"csharp\">\r\nCAMLManager mgr = new CAMLManager();\r\n\r\nmgr.QueryGroups.Add(new QueryGroup(\"Title\", Types.FieldTypes.Text, Types.QueryTypes.Eq, \"A Title\"));\r\nmgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.Or, \"Title\", Types.FieldTypes.Text, Types.QueryTypes.Eq, \"Another Title\"));\r\n\r\nmgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.And, QueryGroup.MergeTypes.BottomGroup, \"Total\", Types.FieldTypes.Number, Types.QueryTypes.Geq, \"1\"));\r\nmgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.Or, QueryGroup.MergeTypes.BottomGroup, \"Total\", Types.FieldTypes.Number, Types.QueryTypes.Eq, \"2\"));\r\n\r\nmgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.Or, QueryGroup.MergeTypes.BottomGroup, \"Total\", Types.FieldTypes.Number, Types.QueryTypes.Geq, \"3\"));\r\nmgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.And, QueryGroup.MergeTypes.BottomGroup, \"Total\", Types.FieldTypes.Number, Types.QueryTypes.Eq, \"4\"));\r\n\r\nstring caml = mgr.GetCAML();\r\n<\/pre>\n<p>This time, the result returned from the GetCAML method is:<\/p>\n<pre lang=\"xml\">\r\n<Where>\r\n  <And>\r\n    <Or>\r\n      <Eq>\r\n        <FieldRef Name=\"Title\" \/>\r\n        <Value Type=\"Text\">A Title<\/Value>\r\n      <\/Eq>\r\n      <Eq>\r\n        <FieldRef Name=\"Title\" \/>\r\n        <Value Type=\"Text\">Another Title<\/Value>\r\n      <\/Eq>\r\n    <\/Or>\r\n    <And>\r\n      <Or>\r\n        <Or>\r\n          <Geq>\r\n            <FieldRef Name=\"Total\" \/>\r\n            <Value Type=\"Number\">1<\/Value>\r\n          <\/Geq>\r\n          <Eq>\r\n            <FieldRef Name=\"Total\" \/>\r\n            <Value Type=\"Number\">2<\/Value>\r\n          <\/Eq>\r\n        <\/Or>\r\n        <Geq>\r\n          <FieldRef Name=\"Total\" \/>\r\n          <Value Type=\"Number\">3<\/Value>\r\n        <\/Geq>\r\n      <\/Or>\r\n      <Eq>\r\n        <FieldRef Name=\"Total\" \/>\r\n        <Value Type=\"Number\">4<\/Value>\r\n      <\/Eq>\r\n    <\/And>\r\n  <\/And>\r\n<\/Where>\r\n<\/pre>\n<p>You use <em>QueryGroup.MergeTypes<\/em> to control where the query is injected and the Types.JoinTypes to dictate the logical operator for that particular query.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A couple of years ago I wrote a CAML helper project and uploaded the source code to CodePlex. Recently I updated the code with a few small bug fixes, after some feedback from a colleague who used it successfully on &hellip; <a href=\"http:\/\/www.stuartroberts.net\/index.php\/2011\/09\/02\/sharepoint_caml_helper\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":[]},"categories":[3],"tags":[27,81],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/plx2I-64","_links":{"self":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/376"}],"collection":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/comments?post=376"}],"version-history":[{"count":8,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/376\/revisions"}],"predecessor-version":[{"id":696,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/376\/revisions\/696"}],"wp:attachment":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=376"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}