{"id":1782,"date":"2015-03-31T19:19:17","date_gmt":"2015-03-31T18:19:17","guid":{"rendered":"http:\/\/www.stuartroberts.net\/?p=1782"},"modified":"2015-03-31T19:34:49","modified_gmt":"2015-03-31T18:34:49","slug":"alternate-to-caml-or","status":"publish","type":"post","link":"https:\/\/www.stuartroberts.net\/index.php\/2015\/03\/31\/alternate-to-caml-or\/","title":{"rendered":"Alternate to CAML Or"},"content":{"rendered":"<p><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/office\/ff625761.aspx\" title=\"In Element\" target=\"_blank\">In<\/a> is a lesser known CAML operator that I&#8217;d like to bring to more peoples attention.  When writing CAML and you want to restrict the results by a multi value field equaling two or more values, you&#8217;d write something like:<\/p>\n<pre lang=\"xml\">\r\n<Where>\r\n<Or>\r\n  <Or>\r\n    <Eq>\r\n      <FieldRef Name=\"Country\" \/><Values><Value Type=\"LookupMulti\">Scotland<\/Value><\/Values>\r\n    <Eq>\r\n    <Eq>\r\n      <FieldRef Name=\"Country\" \/><Values><Value Type=\"LookupMulti\">England<\/Value><\/Values>\r\n    <Eq>\r\n  <\/Or>\r\n  <Or>\r\n    <Eq>\r\n      <FieldRef Name=\"Country\" \/><Values><Value Type=\"LookupMulti\">Wales<\/Value><\/Values>\r\n    <Eq>\r\n    <Eq>\r\n      <FieldRef Name=\"Country\" \/><Values><Value Type=\"LookupMulti\">Ireland<\/Value><\/Values>\r\n    <Eq>\r\n  <\/Or>\r\n<\/Or>\r\n<\/Where>\r\n<\/pre>\n<p>This will return any item that has the value Scotland, England, Wales or Ireland in the Country field.  As the number of values you want to match against grows, imagine the state of the CAML query!<br \/>\n<!--more--><br \/>\nUsing the <em>In<\/em> condition instead, the above CAML can be simplified to the following:<\/p>\n<pre lang=\"xml\">\r\n<Where>\r\n  <In>\r\n    <FieldRef Name=\"Country\" \/>\r\n    <Values>\r\n      <Value Type=\"LookupMulti\">Scotland<\/Value>\r\n      <Value Type=\"LookupMulti\">England<\/Value>\r\n      <Value Type=\"LookupMulti\">Wales<\/Value>\r\n      <Value Type=\"LookupMulti\">Ireland<\/Value>\r\n    <\/Values>\r\n  <\/In>\r\n<\/Where>\r\n<\/pre>\n<p>A lot smaller and easier to read, don&#8217;t you agree?<\/p>\n<p>The <em>In<\/em> condition can be used with any field type, doesn&#8217;t have to be multi valued types like LookupMulti or MultiChoice. When used against a Text field, for example, it simply replaces the <em>Eq<\/em> condition.  Not that much benefit to using it this way though as it&#8217;s not making your life any easier.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In is a lesser known CAML operator that I&#8217;d like to bring to more peoples attention. When writing CAML and you want to restrict the results by a multi value field equaling two or more values, you&#8217;d write something like: &hellip; <a href=\"https:\/\/www.stuartroberts.net\/index.php\/2015\/03\/31\/alternate-to-caml-or\/\">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":true,"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-sK","_links":{"self":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1782"}],"collection":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/comments?post=1782"}],"version-history":[{"count":4,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1782\/revisions"}],"predecessor-version":[{"id":1786,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1782\/revisions\/1786"}],"wp:attachment":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=1782"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=1782"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=1782"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}