{"id":707,"date":"2008-03-17T21:15:00","date_gmt":"2008-03-17T21:15:00","guid":{"rendered":"\/blogs\/bobb\/post\/Programming-Policy-Based-Management-with-SMO-Part-3-ExpressionNode-and-Policy.aspx"},"modified":"2013-01-04T01:49:56","modified_gmt":"2013-01-04T09:49:56","slug":"programming-policy-based-management-with-smo-part-3-expressionnode-and-policy","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-3-expressionnode-and-policy\/","title":{"rendered":"Programming Policy-Based Management with SMO &#8211; Part 3 &#8211; ExpressionNode and Policy"},"content":{"rendered":"<p>\nThis is part three of a series on programming policy-based management. The series starts <a href=\"http:\/\/3.209.169.194\/blogs\/bobb\/programming-policy-based-management-with-smo-part-1-intro\/\" class=\"broken_link\">here<\/a>.\n<\/p>\n<p>\nSo, to initialize my Condition&#39;s ExpressionNode I need more than just a string. It doesn&#39;t look like, at this time, every ExpressionNode CAN initialized with a string. But we can use the subclasses. The ones I need here are Operator, Attribute, and Function. I need: DatabaseMailEnabled (Attribute), Equals (Operator) and false (Function). BTW, ExpressionNodeFunction appears to be what you are programming when you use the &quot;Advanced Functions&quot; dialog in the SSMS Condition dialog.\n<\/p>\n<p>\nPutting one expression together first looks like this:\n<\/p>\n<p>\nExpressionNode exp1 = new ExpressionNodeOperator(OperatorType.EQ,<br \/>\n&nbsp;&nbsp;&nbsp; new ExpressionNodeAttribute(&quot;DatabaseMailEnabled&quot;),<br \/>\n&nbsp;&nbsp;&nbsp; new ExpressionNodeFunction(ExpressionNodeFunction.Function.False)<br \/>\n&nbsp;&nbsp;&nbsp; );\n<\/p>\n<p>\nNote that, unlike in SSMS, you don&#39;t need <a href=\"mailto:'@'\">&#39;@&#39;<\/a> before the attribute name. But I need to specify both DatabaseMail and SqlMail. That&#39;s just a little more complex.\n<\/p>\n<p>\nExpressionNode exp1 = new ExpressionNodeOperator(OperatorType.EQ,<br \/>\n&nbsp;&nbsp;&nbsp; new ExpressionNodeAttribute(&quot;DatabaseMailEnabled&quot;),<br \/>\n&nbsp;&nbsp;&nbsp; new ExpressionNodeFunction(ExpressionNodeFunction.Function.False)<br \/>\n&nbsp;&nbsp;&nbsp; );<br \/>\nExpressionNode exp2 = new ExpressionNodeOperator(OperatorType.EQ,<br \/>\n&nbsp;&nbsp;&nbsp; new ExpressionNodeAttribute(&quot;SqlMailEnabled&quot;),<br \/>\n&nbsp;&nbsp;&nbsp; new ExpressionNodeFunction(ExpressionNodeFunction.Function.False)<br \/>\n&nbsp;&nbsp;&nbsp; );<br \/>\nExpressionNode exp_both = new ExpressionNodeOperator(OperatorType.AND, exp1, exp2);\n<\/p>\n<p>\ncon1.ExpressionNode = exp_both;<br \/>\ncon1.Create();\n<\/p>\n<p>\nAnd, nice as you please. Looks just like the one defined in SSMS using the GUI. Make sure that you remove (or comment out) the original ExpressionNode.Parse statement.\n<\/p>\n<p>\nNow, on to the policy.\n<\/p>\n<p>\n\/\/ Create a policy that uses the condition we just created<br \/>\nPolicy p1 = new Policy(ps, &quot;OffByDefaultSMO&quot;);<br \/>\np1.Condition = &quot;MailOffSMO&quot;;<br \/>\np1.AutomatedPolicyExecutionMode = AutomatedPolicyExecutionMode.None;<br \/>\np1.Enabled = false;<br \/>\np1.Create();\n<\/p>\n<p>\nLooks like the one created by the GUI and works like it too.\n<\/p>\n<p>\nThis is a pretty simple policy, because it can only be applied at the instance level. Next, we&#39;ll look at a policy that can be applied to a set of database objects, e.g. All tables in a particular database. This requires that we investigate ObjectSets.\n<\/p>\n<p>\nCavaet. It&#39;s pretty easy to make a mistake, sans docs. Some mistakes produce a generic &quot;I can&#39;t do this&quot;-type message. So ALWAYS drill into the INNER exception if you get an error. That is:\n<\/p>\n<p>\ntry<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp; con1.ExpressionNode = exp_both;<br \/>\n&nbsp;&nbsp;&nbsp; con1.Create();<br \/>\n}<br \/>\ncatch (Exception ex)<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp; Console.WriteLine(ex.Message);<br \/>\n&nbsp;&nbsp;&nbsp; if (ex.InnerException != null)<br \/>\n&nbsp;&nbsp;&nbsp; {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(ex.InnerException.Message);<br \/>\n&nbsp;&nbsp;&nbsp; }<br \/>\n}\n<\/p>\n<p>\nStack trace might even be helpful.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is part three of a series on programming policy-based management. The series starts here. So, to initialize my Condition&#39;s ExpressionNode I need more than just a string. It doesn&#39;t look like, at this time, every ExpressionNode CAN initialized with a string. But we can use the subclasses. The ones I need here are Operator, [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,25,29],"tags":[],"class_list":["post-707","post","type-post","status-publish","format-standard","hentry","category-policy-based-management","category-smo","category-sql-server-2008"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.9.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Programming Policy-Based Management with SMO - Part 3 - ExpressionNode and Policy - Bob Beauchemin<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-3-expressionnode-and-policy\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Programming Policy-Based Management with SMO - Part 3 - ExpressionNode and Policy - Bob Beauchemin\" \/>\n<meta property=\"og:description\" content=\"This is part three of a series on programming policy-based management. The series starts here. So, to initialize my Condition&#039;s ExpressionNode I need more than just a string. It doesn&#039;t look like, at this time, every ExpressionNode CAN initialized with a string. But we can use the subclasses. The ones I need here are Operator, [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-3-expressionnode-and-policy\/\" \/>\n<meta property=\"og:site_name\" content=\"Bob Beauchemin\" \/>\n<meta property=\"article:published_time\" content=\"2008-03-17T21:15:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-01-04T09:49:56+00:00\" \/>\n<meta name=\"author\" content=\"Bob Beauchemin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bob Beauchemin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-3-expressionnode-and-policy\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-3-expressionnode-and-policy\/\",\"name\":\"Programming Policy-Based Management with SMO - Part 3 - ExpressionNode and Policy - Bob Beauchemin\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\"},\"datePublished\":\"2008-03-17T21:15:00+00:00\",\"dateModified\":\"2013-01-04T09:49:56+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-3-expressionnode-and-policy\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-3-expressionnode-and-policy\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-3-expressionnode-and-policy\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Policy-Based Management\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/policy-based-management\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Programming Policy-Based Management with SMO &#8211; Part 3 &#8211; ExpressionNode and Policy\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\",\"name\":\"Bob Beauchemin\",\"description\":\"SQL Server Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\",\"name\":\"Bob Beauchemin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"caption\":\"Bob Beauchemin\"},\"sameAs\":[\"http:\/www.sqlskills.com\/blogs\/bobb\/\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Programming Policy-Based Management with SMO - Part 3 - ExpressionNode and Policy - Bob Beauchemin","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-3-expressionnode-and-policy\/","og_locale":"en_US","og_type":"article","og_title":"Programming Policy-Based Management with SMO - Part 3 - ExpressionNode and Policy - Bob Beauchemin","og_description":"This is part three of a series on programming policy-based management. The series starts here. So, to initialize my Condition&#39;s ExpressionNode I need more than just a string. It doesn&#39;t look like, at this time, every ExpressionNode CAN initialized with a string. But we can use the subclasses. The ones I need here are Operator, [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-3-expressionnode-and-policy\/","og_site_name":"Bob Beauchemin","article_published_time":"2008-03-17T21:15:00+00:00","article_modified_time":"2013-01-04T09:49:56+00:00","author":"Bob Beauchemin","twitter_misc":{"Written by":"Bob Beauchemin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-3-expressionnode-and-policy\/","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-3-expressionnode-and-policy\/","name":"Programming Policy-Based Management with SMO - Part 3 - ExpressionNode and Policy - Bob Beauchemin","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website"},"datePublished":"2008-03-17T21:15:00+00:00","dateModified":"2013-01-04T09:49:56+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-3-expressionnode-and-policy\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-3-expressionnode-and-policy\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-3-expressionnode-and-policy\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/"},{"@type":"ListItem","position":2,"name":"Policy-Based Management","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/policy-based-management\/"},{"@type":"ListItem","position":3,"name":"Programming Policy-Based Management with SMO &#8211; Part 3 &#8211; ExpressionNode and Policy"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/","name":"Bob Beauchemin","description":"SQL Server Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e","name":"Bob Beauchemin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","caption":"Bob Beauchemin"},"sameAs":["http:\/www.sqlskills.com\/blogs\/bobb\/"],"url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/comments?post=707"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/707\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/media?parent=707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/categories?post=707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/tags?post=707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}