{"id":704,"date":"2008-03-18T02:58:00","date_gmt":"2008-03-18T02:58:00","guid":{"rendered":"\/blogs\/bobb\/post\/Programming-Policy-Based-Management-with-SMO-Part-6-Categories-Subscriptions-Wrapup.aspx"},"modified":"2013-01-04T01:49:55","modified_gmt":"2013-01-04T09:49:55","slug":"programming-policy-based-management-with-smo-part-6-categories-subscriptions-wrapup","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-6-categories-subscriptions-wrapup\/","title":{"rendered":"Programming Policy-Based Management with SMO &#8211; Part 6 &#8211; Categories, Subscriptions, Wrapup"},"content":{"rendered":"<p>\nThis is the last part 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>\nIn the previous installment, I created a policy that was constrained to a single database. To accomplish this, I used a Condition that called out the database by name, and tied it to the TargetSet using TargetSet&#39;s SetLevelCondition method.\n<\/p>\n<p>\nAn alternative consists of creating a policy as part of a PolicyCategory. Each Policy is a member of exactly one PolicyCategory. The default PolicyCategory is the only one that &quot;ships with the system&quot;, but you can define your own. If you don&#39;t specify otherwise in the code (SSMS has a Category dropdown on the Description page of the new Category dialog), your policy is a member of the default category.\n<\/p>\n<p>\nEach PolicyCategory has a property that indicates whether it&#39;s manditory that a database subscribe to that category. If this property is true, each database has an implicit subscription to the category. If not, a database must explicitly subscribe. The code to define a PolicyCategory is straightforward:\n<\/p>\n<p>\nstatic void CreateCategory(PolicyStore ps)<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp; PolicyCategory cat = new PolicyCategory(ps, &quot;MyNewCategory&quot;);<br \/>\n&nbsp;&nbsp;&nbsp; cat.MandateDatabaseSubscriptions = false;<br \/>\n&nbsp;&nbsp;&nbsp; cat.Create();<br \/>\n}\n<\/p>\n<p>\nTo create a Policy that&#39;s a member of the PolicyCategory, simply use the aptly-named PolicyCategory property. If you&#39;re using a named PolicyCategory you may not want to restrict that policy to a specific database. Here&#39;s the changes to the Policy definition.\n<\/p>\n<p>\n\/\/ No Condition On This One, applies to all databases, but must be subscribed to<br \/>\n\/\/ts1.SetLevelCondition(ts1.GetLevel(&quot;Server\/Database&quot;), &quot;FinanceDB&quot;);\n<\/p>\n<p>\n\/\/ Name the category<br \/>\np2.PolicyCategory = &quot;MyNewCategory&quot;;\n<\/p>\n<p>\nTo create a PolicyCategorySubscription (for those categories that are not Manditory), the only interesting part is that you need a SqlSmoObject. You can use a concrete subclass (like Database) or make up a SqlSmoObject by using a URN. Dan Sullivan&#39;s&nbsp;and my book &quot;<a href=\"http:\/\/www.pearsonhighered.com\/educator\/academic\/product\/0,3110,0321382188,00.html\">Developer&#39;s Guide to SQL Server 2005<\/a>&quot; covers both ways to make an SqlSmoObject. Here&#39;s the code for PolicyCategorySubscription.\n<\/p>\n<p>\nstatic void CreatePolicyCategorySubscription(PolicyStore ps)<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp; Server svr = new Server(); \/\/ open a connection to default instance, local server<br \/>\n&nbsp;&nbsp;&nbsp; Database db = new Database(svr, &quot;pubs&quot;);<br \/>\n&nbsp;&nbsp;&nbsp; PolicyCategorySubscription subs = new PolicyCategorySubscription(ps, db);<br \/>\n&nbsp;&nbsp;&nbsp; subs.PolicyCategory = &quot;MyNewCategory&quot;;<br \/>\n&nbsp;&nbsp;&nbsp; subs.Create();<br \/>\n}\n<\/p>\n<p>\nThis means that only the pubs database now follows the policies in &quot;MyNewCategory&quot;. BTW, there&#39;s currently the PowerShell provider in CTP6 throws an error when attempting list a PolicyCategorySubscription (its in the hierarchy at the same level as Policy). The PolicyCategorySubscription still works as advertised though.\n<\/p>\n<p>\nThis concludes the series on Programming Policy-Based Management with SMO. Hope it was useful.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the last part of a series on programming policy-based management. The series starts here. In the previous installment, I created a policy that was constrained to a single database. To accomplish this, I used a Condition that called out the database by name, and tied it to the TargetSet using TargetSet&#39;s SetLevelCondition method. [&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-704","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 6 - Categories, Subscriptions, Wrapup - 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-6-categories-subscriptions-wrapup\/\" \/>\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 6 - Categories, Subscriptions, Wrapup - Bob Beauchemin\" \/>\n<meta property=\"og:description\" content=\"This is the last part of a series on programming policy-based management. The series starts here. In the previous installment, I created a policy that was constrained to a single database. To accomplish this, I used a Condition that called out the database by name, and tied it to the TargetSet using TargetSet&#039;s SetLevelCondition method. [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-6-categories-subscriptions-wrapup\/\" \/>\n<meta property=\"og:site_name\" content=\"Bob Beauchemin\" \/>\n<meta property=\"article:published_time\" content=\"2008-03-18T02:58: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-6-categories-subscriptions-wrapup\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-6-categories-subscriptions-wrapup\/\",\"name\":\"Programming Policy-Based Management with SMO - Part 6 - Categories, Subscriptions, Wrapup - Bob Beauchemin\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\"},\"datePublished\":\"2008-03-18T02:58: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-6-categories-subscriptions-wrapup\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-6-categories-subscriptions-wrapup\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-6-categories-subscriptions-wrapup\/#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 6 &#8211; Categories, Subscriptions, Wrapup\"}]},{\"@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 6 - Categories, Subscriptions, Wrapup - 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-6-categories-subscriptions-wrapup\/","og_locale":"en_US","og_type":"article","og_title":"Programming Policy-Based Management with SMO - Part 6 - Categories, Subscriptions, Wrapup - Bob Beauchemin","og_description":"This is the last part of a series on programming policy-based management. The series starts here. In the previous installment, I created a policy that was constrained to a single database. To accomplish this, I used a Condition that called out the database by name, and tied it to the TargetSet using TargetSet&#39;s SetLevelCondition method. [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-6-categories-subscriptions-wrapup\/","og_site_name":"Bob Beauchemin","article_published_time":"2008-03-18T02:58: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-6-categories-subscriptions-wrapup\/","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-6-categories-subscriptions-wrapup\/","name":"Programming Policy-Based Management with SMO - Part 6 - Categories, Subscriptions, Wrapup - Bob Beauchemin","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website"},"datePublished":"2008-03-18T02:58: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-6-categories-subscriptions-wrapup\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-6-categories-subscriptions-wrapup\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/programming-policy-based-management-with-smo-part-6-categories-subscriptions-wrapup\/#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 6 &#8211; Categories, Subscriptions, Wrapup"}]},{"@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\/704","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=704"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/704\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/media?parent=704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/categories?post=704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/tags?post=704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}