{"id":233,"date":"2011-02-04T20:57:42","date_gmt":"2011-02-04T20:57:42","guid":{"rendered":"http:\/\/www.stuartroberts.net\/?p=233"},"modified":"2011-02-04T20:57:42","modified_gmt":"2011-02-04T20:57:42","slug":"information-rights-audit-policy","status":"publish","type":"post","link":"https:\/\/www.stuartroberts.net\/index.php\/2011\/02\/04\/information-rights-audit-policy\/","title":{"rendered":"Coding the Creation of Information Management Audit Policy"},"content":{"rendered":"<p>This is a quick post to demonstrate how you would create or update the Information Rights audit policy for a specific content type in SharePoint.<\/p>\n<p>First things first, lets have a look at the method that implements this for us:<\/p>\n<pre lang=\"csharp\">\r\npublic void EnsureAuditPolicy(SPContentType ct)\r\n{\r\n    const string AUDIT_POLICY_NAME = \"Custom Audit Policy\";\r\n    \/\/ Policy XML for enabling all out of the box auditing\r\n    const string AUDIT_POLICY = \"<Audit><Update \/><View \/><CheckInOut \/><MoveCopy \/><DeleteRestore \/><\/Audit>\";\r\n\r\n    Policy policy = Policy.GetPolicy(ct);\r\n    if (policy == null)\r\n    {\r\n        \/\/ Create new custom policy\r\n        Policy.CreatePolicy(ct, null);\r\n        \/\/ Retrieve newly created policy\r\n        policy = Policy.GetPolicy(ct);\r\n\r\n        if (policy == null)\r\n        {\r\n            throw new NullReferenceException(\"policy\");\r\n        }\r\n\r\n        \/\/ Update policy information\r\n        policy.Statement = string.Format(\"Custom audit policy for viewing, editing, checking in or out, moving or copying and deleting or restoring \\\"{0}\\\" items\", policy.Name);\r\n        policy.Name = string.Format(\"{0} - {1}\", policy.Name, AUDIT_POLICY_NAME);\r\n        policy.Description = \"Custom Audit Policy\";\r\n    }\r\n\r\n    if (policy != null)\r\n    {\r\n        \/\/ The PolicyId property provides the policyFeatureId parameter for the PolicyItemCollection indexer\r\n        PolicyItem auditItem = policy.Items[PolicyAudit.PolicyId];\r\n        if (auditItem == null)\r\n        {\r\n            \/\/ Update policy with custom audit requirements\r\n            policy.Items.Add(PolicyAudit.PolicyId, AUDIT_POLICY);\r\n        }\r\n        else\r\n        {\r\n            \/\/ Modify existing policy with the custom audit requirements\r\n            auditItem.CustomData = AUDIT_POLICY;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p><!--more--><\/p>\n<p>For the code to compile you&#8217;ll need to add a reference to the <em>Microsoft.Office.Policy<\/em> assembly which is located in the ISAPI folder of the hive.<\/p>\n<p>%ProgramFiles%\\Common Files\\Microsoft Shared\\Web Server Extensions\\14\\ISAPI\\Microsoft.Office.Policy.dll<\/p>\n<p>The two namespaces that should be added to your code are:<\/p>\n<pre lang=\"csharp\">\r\nusing Microsoft.Office.RecordsManagement.InformationPolicy;\r\nusing Microsoft.Office.RecordsManagement.PolicyFeatures;\r\n<\/pre>\n<p>From looking at the code you can see it&#8217;s pretty straight forward.  You call the static method <em>GetPolicy<\/em> of Microsoft.Office.RecordsManagement.InformationPolicy.Policy to try and retrieve the policy object for the content type that was passed into the method.<\/p>\n<p>From here we check if there is already a policy for the content type.  Where there is no policy we create a new one via the <em>CreatePolicy<\/em> static method.  We then assign a name, statement and description &#8211; the text for this has no bearing on the type of audit that will be implemented.<\/p>\n<p>The last step is to check if the policy object has an existing policy item for auditing.  We check this by attempting to retrieve the PolicyItem that has the feature id of the audit class.  The feature id is provided for us by the policy object that we are creating &#8211; Microsoft.Office.RecordsManagement.PolicyFeatures.PolicyAudit.PolicyId<\/p>\n<p>The policy configuration in this example is provided by the AUDIT_POLICY constant.  This enables all out of the box auditing for the content type.<\/p>\n<p>An easy way to retrieve the custom data for a policy is to create the policy through the UI and then write a simple console application to return the policy object for your content type, something like:<\/p>\n<pre lang=\"csharp\">\r\npublic string GetAuditPolicyCustomData(SPContentType ct)\r\n{\r\n    string customData = string.Empty;\r\n    Policy policy = Policy.GetPolicy(ct);\r\n\r\n    if (policy != null)\r\n    {\r\n        PolicyItem auditItem = policy.Items[PolicyAudit.PolicyId];\r\n        if (auditItem != null)\r\n        {\r\n            customData = auditItem.CustomData;\r\n            Console.WriteLine(customData);\r\n        }\r\n    }\r\n\r\n    return customData;\r\n}\r\n<\/pre>\n<p>Here we are retrieving the audit policy configuration data for the manually configured content type, which for a fully enabled audit policy is: <\/p>\n<pre lang=\"xml\">\r\n<Audit><Update \/><View \/><CheckInOut \/><MoveCopy \/><DeleteRestore \/><\/Audit>\r\n<\/pre>\n<p>Examples of other audit objects that can be used are:<\/p>\n<pre lang=\"csharp\">\r\nMicrosoft.Office.RecordsManagement.PolicyFeatures.Barcode\r\nMicrosoft.Office.RecordsManagement.PolicyFeatures.Expiration\r\nMicrosoft.Office.RecordsManagement.PolicyFeatures.PolicyLabel\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is a quick post to demonstrate how you would create or update the Information Rights audit policy for a specific content type in SharePoint. First things first, lets have a look at the method that implements this for us: &hellip; <a href=\"https:\/\/www.stuartroberts.net\/index.php\/2011\/02\/04\/information-rights-audit-policy\/\">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":[12,81],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/plx2I-3L","_links":{"self":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/233"}],"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=233"}],"version-history":[{"count":10,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/233\/revisions"}],"predecessor-version":[{"id":244,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/233\/revisions\/244"}],"wp:attachment":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}