Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Server Technologies
Visual How Tos
 Activating Auditing Programmaticall...
Community Content
In this section
Statistics Annotations (0)
Activating Auditing Programmatically for a Site Collection in Windows SharePoint Services 3.0
Visual How To

Applies to: Microsoft Windows SharePoint Services 3.0, Microsoft Office SharePoint Server 2007, Microsoft Visual Studio 2005

Ted Pattison, Ted Pattison Group

May 2007

Overview

Microsoft Windows SharePoint Services 3.0 includes a powerful new infrastructure for auditing user access to list items, and documents and pages. In Windows SharePoint Services, auditing is configured on a site collection-by-site collection basis. It just takes a few lines of code to fully enable the auditing for an entire site collection. When you do this, Windows SharePoint Services writes an audit entry to its audit log each time a user views or modifies a list item, document, or site page.

Code It

Many object types in the Windows SharePoint Services object model are auditable. Each of these auditable objects exposes an Audit property. You can configure an Audit property programmatically by using a set of audit flags that is provided by the SPAuditMaskType enumeration. Start by configuring the current site collection to enable all types of auditing.

C#
SPSite siteCollection = SPContext.Current.Site;
// Turn on auditing flags.
siteCollection.Audit.AuditFlags = SPAuditMaskType.All;
siteCollection.Audit.Update();
Visual Basic
Dim siteCollection As SPSite = SPContext.Current.Site
'*** Turn on auditing flags.
siteCollection.Audit.AuditFlags = SPAuditMaskType.All
siteCollection.Audit.Update()

Using More Granular Auditing

In some cases, you might not want to audit every type of auditable event supported by Windows SharePoint Services. The SPAuditMaskType enumeration provides a value for each type of auditable event. You can use bitwise OR operations to combine these enumeration values to get the exact auditing support that you need for a particular scenario.

C#
SPSite siteCollection = SPContext.Current.Site;
// Turn on auditing flags.
siteCollection.Audit.AuditFlags = SPAuditMaskType.View |
                                  SPAuditMaskType.Update | 
                                  SPAuditMaskType.Delete;
siteCollection.Audit.Update();
Visual Basic
Dim siteCollection As SPSite = SPContext.Current.Site
'*** Turn on auditing flags.
siteCollection.Audit.AuditFlags = SPAuditMaskType.View Or _
                                  SPAuditMaskType.Update Or _
                                  SPAuditMaskType.Delete
siteCollection.Audit.Update()

Auditing Options

While the previous example demonstrates how to enable auditing for just view, update, and delete activity, you have even more options. Following is the complete set of SPAuditMaskType values that provide control over different types of auditing.

C#
SPAuditMaskType.CheckIn
SPAuditMaskType.CheckOut
SPAuditMaskType.ChildDelete
SPAuditMaskType.Copy
SPAuditMaskType.Delete
SPAuditMaskType.Move
SPAuditMaskType.ProfileChange
SPAuditMaskType.SchemaChange
SPAuditMaskType.Search
SPAuditMaskType.SecurityChange
SPAuditMaskType.Undelete
SPAuditMaskType.Update
SPAuditMaskType.View
SPAuditMaskType.Workflow
Visual Basic
SPAuditMaskType.CheckIn
SPAuditMaskType.CheckOut
SPAuditMaskType.ChildDelete
SPAuditMaskType.Copy
SPAuditMaskType.Delete
SPAuditMaskType.Move
SPAuditMaskType.ProfileChange
SPAuditMaskType.SchemaChange
SPAuditMaskType.Search
SPAuditMaskType.SecurityChange
SPAuditMaskType.Undelete
SPAuditMaskType.Update
SPAuditMaskType.View
SPAuditMaskType.Workflow
Read It

Many different types of objects in the Windows SharePoint Services object model are auditable, including SPSite, SPWeb, SPList, and SPListItem. Each of these object types exposes an Audit property that can be configured using values of the SPAuditMaskType enumeration types, as you have just seen. You can use a value of SPAuditMaskType.All to enable auditing for every type of event that is supported by Windows SharePoint Services. You can also use bitwise OR operations to combine these enumeration values from the SPAuditMaskType enumeration to get exactly the auditing support that you need for the scenario at hand. Also, remember that after you set the Audit flags on an auditable object, you must call the Update method to store your changes into the content database.

See It Activating Auditing For Site Collection

Watch the Video

Video Length: 00:03:07

File Size: 5.92 MB WMV

Explore It
Tags What's this?: audit (x) wss (x) Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker