{"id":705,"date":"2008-03-17T22:44:00","date_gmt":"2008-03-17T22:44:00","guid":{"rendered":"\/blogs\/bobb\/post\/Programming-Policy-Based-Management-with-SMO-Part-5-TargetSets-and-TargetSetLevels.aspx"},"modified":"2013-01-04T01:49:55","modified_gmt":"2013-01-04T09:49:55","slug":"programming-policy-based-management-with-smo-part-5-targetsets-and-targetsetlevels","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-5-targetsets-and-targetsetlevels\/","title":{"rendered":"Programming Policy-Based Management with SMO &#8211; Part 5 &#8211; TargetSets and TargetSetLevels"},"content":{"rendered":"<p>\nThis post is part of a series on programming policy-based management. The series begins <a href=\"http:\/\/3.209.169.194\/blogs\/bobb\/programming-policy-based-management-with-smo-part-1-intro\/\">here<\/a>.\n<\/p>\n<p>\nSo, we were working with a policy that required an ObjectSet. ObjectSets contain TargetSets. For example, the ObjectSet for the naming policy (IMultipartName) we were working on needs a TargetSet for Procedure, Synonym, Table, Function, Type, View, and XmlSchemaCollection.\n<\/p>\n<p>\nNote that this collection is similar to what you&#39;d see for a MultipartName policy in the SSMS designer dialog. The title for it in SSMS is &quot;Against Targets:&quot;. We want our policy to apply only to Tables. After fumbling around for a while attempting to define TargetSets and add them to the ObjectSet&#39;s collection of them, I found that the seven TargetSets I needed were *already* defined. This reduces enabling only the table&#39;s TargetSet to two lines of additional code. Notice that you can reference a specific TargetSet in an ObjectSet by using an indexer. The indexer is an SMO URL.\n<\/p>\n<p>\nTargetSet ts1 = os1.TargetSets[&quot;Server\/Database\/Table&quot;];<br \/>\nts1.Enabled = true;\n<\/p>\n<p>\nBut how to restrict this policy to a single database? For this we need a Condition to name the database. Because this condition is simple we can use ExpressionNode.Parse(). Here&#39;s the Condition code.\n<\/p>\n<p>\n\/\/ Create a condition to enforce <br \/>\nCondition con = new Condition(ps, &quot;FinanceDB&quot;);<br \/>\ncon.Facet = &quot;Database&quot;;<br \/>\n\/\/ Note: Using Parse() treats the string as an SMO URL. Only works for simplest cases<br \/>\nstring s = &quot;@Name = &#39;finance&#39;&quot;;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>\n\/\/ try-catch code omitted for brevity<br \/>\ncon.ExpressionNode = ExpressionNode.Parse(s);<br \/>\ncon.Create();\n<\/p>\n<p>\nBack to our TargetSet. We restrict the TargetSet to a specific database by using TargetSet.SetLevelCondition. SetLevelCondition takes a TargetSetLevel, and we get the appropriate TargetSetLevel (which is prepopulated) by&#8230;. you guessed it&#8230;using a SMO URL. After ts1.Enabled, this limits the policy to a single database defined by our Condition.\n<\/p>\n<p>\n\/\/ FinanceDB is the name of our Condition that &quot;limit\/defines&quot; this policy only to Finance<br \/>\nts1.SetLevelCondition(ts1.GetLevel(&quot;Server\/Database&quot;), &quot;FinanceDB&quot;);\n<\/p>\n<p>\nHaving initialized the ObjectSet correctly, we now create the ObjectSet, tie it to the Policy and voila&#8230;\n<\/p>\n<p>\nos1.Create();<br \/>\np2.ObjectSet = &quot;CheckFinanceTab_ObjectSet&quot;;<br \/>\np2.Create();\n<\/p>\n<p>\nCheck this policy by using the following code in a query window:\n<\/p>\n<p>\nuse finance<br \/>\ngo<br \/>\ncreate table dbo.foo (id int);<br \/>\ngo\n<\/p>\n<p>\nYou get the expected error:<br \/>\nPolicy &#39;CheckFinanceTab&#39; has been violated by &#39;\/Server\/(local)\/Database\/finance\/Table\/dbo.foo&#39;.<br \/>\nThis transaction will be rolled back.<br \/>\nPolicy description: &#39;&#39;<br \/>\nAdditional help: &#39;&#39; : &#39;&#39;.<br \/>\nMsg 3609, Level 16, State 1, Procedure sp_syspolicy_dispatch_event, Line 50<br \/>\nThe transaction ended in the trigger. The batch has been aborted.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post is part of a series on programming policy-based management. The series begins here. So, we were working with a policy that required an ObjectSet. ObjectSets contain TargetSets. For example, the ObjectSet for the naming policy (IMultipartName) we were working on needs a TargetSet for Procedure, Synonym, Table, Function, Type, View, and XmlSchemaCollection. Note [&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-705","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 5 - TargetSets and TargetSetLevels - 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-5-targetsets-and-targetsetlevels\/\" \/>\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 5 - TargetSets and TargetSetLevels - Bob Beauchemin\" \/>\n<meta property=\"og:description\" content=\"This post is part of a series on programming policy-based management. The series begins here. So, we were working with a policy that required an ObjectSet. ObjectSets contain TargetSets. For example, the ObjectSet for the naming policy (IMultipartName) we were working on needs a TargetSet for Procedure, Synonym, Table, Function, Type, View, and XmlSchemaCollection. Note [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-5-targetsets-and-targetsetlevels\/\" \/>\n<meta property=\"og:site_name\" content=\"Bob Beauchemin\" \/>\n<meta property=\"article:published_time\" content=\"2008-03-17T22:44:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-01-04T09:49:55+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-5-targetsets-and-targetsetlevels\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-5-targetsets-and-targetsetlevels\/\",\"name\":\"Programming Policy-Based Management with SMO - Part 5 - TargetSets and TargetSetLevels - Bob Beauchemin\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\"},\"datePublished\":\"2008-03-17T22:44:00+00:00\",\"dateModified\":\"2013-01-04T09:49:55+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-5-targetsets-and-targetsetlevels\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-5-targetsets-and-targetsetlevels\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-5-targetsets-and-targetsetlevels\/#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 5 &#8211; TargetSets and TargetSetLevels\"}]},{\"@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 5 - TargetSets and TargetSetLevels - 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-5-targetsets-and-targetsetlevels\/","og_locale":"en_US","og_type":"article","og_title":"Programming Policy-Based Management with SMO - Part 5 - TargetSets and TargetSetLevels - Bob Beauchemin","og_description":"This post is part of a series on programming policy-based management. The series begins here. So, we were working with a policy that required an ObjectSet. ObjectSets contain TargetSets. For example, the ObjectSet for the naming policy (IMultipartName) we were working on needs a TargetSet for Procedure, Synonym, Table, Function, Type, View, and XmlSchemaCollection. Note [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-5-targetsets-and-targetsetlevels\/","og_site_name":"Bob Beauchemin","article_published_time":"2008-03-17T22:44:00+00:00","article_modified_time":"2013-01-04T09:49:55+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-5-targetsets-and-targetsetlevels\/","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-5-targetsets-and-targetsetlevels\/","name":"Programming Policy-Based Management with SMO - Part 5 - TargetSets and TargetSetLevels - Bob Beauchemin","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website"},"datePublished":"2008-03-17T22:44:00+00:00","dateModified":"2013-01-04T09:49:55+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-5-targetsets-and-targetsetlevels\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-5-targetsets-and-targetsetlevels\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-5-targetsets-and-targetsetlevels\/#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 5 &#8211; TargetSets and TargetSetLevels"}]},{"@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\/705","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=705"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/705\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/media?parent=705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/categories?post=705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/tags?post=705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}