Monday, March 16, 2009

A Feature to Setup Site Auditing

Some of our SharePoint sites need auditing turned on to monitor changes to content. This is a fairly simple thing to do through the user interface (http://siteurl/_layouts/AuditSettings.aspx).

When using site definitions it is best to remove as many manual steps as you can. The following is a handy feature to automatically setup the Audit Settings.

The feature:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

using Microsoft.SharePoint;

 

using MaskedSharePointer.Logging;

 

namespace MaskedSharePointer.Features.Receivers

{

    public class SiteAuditingReceiver : SPFeatureReceiver

    {

        public override void FeatureActivated(SPFeatureReceiverProperties properties)

        {

            if (!(properties.Feature.Parent is SPSite))

                throw new ApplicationException("Site Auditing Receiver is a site feature.");

 

            SPSite site = properties.Feature.Parent as SPSite;

 

            int auditFlags = 0;

 

            foreach (SPFeatureProperty featureProperty in properties.Feature.Properties)

            {

                try

                {

                    auditFlags += Convert.ToInt32(Enum.Parse(typeof(SPAuditMaskType), featureProperty.Value, true));

                }

                catch (Exception ex)

                {

                    Logger.LogException(ex);

                }

            }

 

            site.Audit.AuditFlags = (SPAuditMaskType)Enum.ToObject(typeof(SPAuditMaskType), auditFlags);

            site.Audit.Update();

        }

 

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)

        {

            if (!(properties.Feature.Parent is SPSite))

                throw new ApplicationException("Site Auditing Receiver is a site feature.");

 

            SPSite site = properties.Feature.Parent as SPSite;

            site.Audit.AuditFlags = SPAuditMaskType.None;

            site.Audit.Update();

        }

 

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)

        {

        }

 

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)

        {

        }

    }

}



And the feature to activate the audit settings. Its customisable by changing the properties to the appropriate level of auditing:

<?xml version="1.0" encoding="utf-8" ?>

<Feature

    Title="Site Auditing"

    Description="Sets up site collection audit settings"

    Id="{GUID}"

    Scope="Site"

    Version="1.0.0.0"

    Hidden="FALSE"

    xmlns="http://schemas.microsoft.com/sharepoint/"

    ReceiverAssembly="MaskedSharePointer.Demo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx"

    ReceiverClass="MaskedSharePointer.Features.Receivers.SiteAuditingReceiver">

    <Properties>

        <!-- One of these -->

        <Property Key="All" Value="All"/>

        <Property Key="None" Value="None"/>

 

        <!-- OR a combination of any of the following-->

        <Property Key="View" Value="View"/>

        <Property Key="CheckIn" Value="CheckIn"/>

        <Property Key="CheckOut" Value="CheckOut"/>

        <Property Key="ChildDelete" Value="ChildDelete"/>

        <Property Key="Copy" Value="Copy"/>

        <Property Key="Delete" Value="Delete"/>

        <Property Key="Move" Value="Move"/>

        <Property Key="ProfileChange" Value="ProfileChange"/>

        <Property Key="SchemaChange" Value="SchemaChange"/>

        <Property Key="SecurityChange" Value="SecurityChange"/>

        <Property Key="Undelete" Value="Undelete"/>

        <Property Key="Update" Value="Update"/>

        <Property Key="Workflow" Value="Workflow"/>

        <Property Key="Search" Value="Search"/>

    </Properties>

</Feature>

No comments: