Tag Archive for Information Management

Coding the Creation of Information Management Audit Policy

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:

public void EnsureAuditPolicy(SPContentType ct)
{
    const string AUDIT_POLICY_NAME = "Custom Audit Policy";
    // Policy XML for enabling all out of the box auditing
    const string AUDIT_POLICY = "<audit><update></update><view></view><checkinout></checkinout><movecopy></movecopy><deleterestore></deleterestore></audit>";
 
    Policy policy = Policy.GetPolicy(ct);
    if (policy == null)
    {
        // Create new custom policy
        Policy.CreatePolicy(ct, null);
        // Retrieve newly created policy
        policy = Policy.GetPolicy(ct);
 
        if (policy == null)
        {
            throw new NullReferenceException("policy");
        }
 
        // Update policy information
        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);
        policy.Name = string.Format("{0} - {1}", policy.Name, AUDIT_POLICY_NAME);
        policy.Description = "Custom Audit Policy";
    }
 
    if (policy != null)
    {
        // The PolicyId property provides the policyFeatureId parameter for the PolicyItemCollection indexer
        PolicyItem auditItem = policy.Items[PolicyAudit.PolicyId];
        if (auditItem == null)
        {
            // Update policy with custom audit requirements
            policy.Items.Add(PolicyAudit.PolicyId, AUDIT_POLICY);
        }
        else
        {
            // Modify existing policy with the custom audit requirements
            auditItem.CustomData = AUDIT_POLICY;
        }
    }
}

Read more

Follow

Get every new post delivered to your Inbox

Join other followers: