Thursday, March 19, 2009

Activating Auditing Programmatically for a Document Library in SharePoint

In the SharePoint (MOSS) object model, SPList objects and SPListItem objects expose an Audit property that makes it possible to configure auditing. The following example shows how to enable auditing in a single document library.

// Open site and Web objects
SPSite siteCollection = new SPSite(http://localhost);
SPWeb site = site.OpenWeb("testweb");

// Open the document library to enable auditing
SPList docLib = site.Lists("documents");

// Turn on auditing flags.
docLib.Audit.AuditFlags = SPAuditMaskType.View
SPAuditMaskType.ChildDelete;
docLib.Audit.Update();


To enable auditing at site collection level
// Open site object
SPSite siteCollection = new SPSite(http://localhost/);
// Enable auditing for the site
siteCollection.Audit.AuditFlags = SPAuditMaskType.All;
siteCollection.Audit.Update();

No comments: