To enumerate all the Audit entries of a Site, it is possible to use the method 'GetEntries' in this way:
SPSite mySite = new SPSite("http://ServerName");
SPAudit myAuditCol = mySite.Audit;
foreach (SPAuditEntry myEntry in myAuditCol)
{
Console.WriteLine(myEntry.DocLocation + " - " +
myEntry.Occurred.ToLongDateString()
" - " + myEntry.UserId.ToString() + " - " +
myEntry.Event.ToString());
}
In the code, after the creation of an object containing the Site Collection information and another object for the
Audit entries, a loop reads each entry and prints some properties.
In a similar way, after creating the Audit object, it is possible to eliminate all its entries:
myAuditCol.DeleteEntries(DateTime.Now.AddMilliseconds(1));
The delete method uses as input parameters a future date and time, and the code uses the actual time plus a mil-
lisecond. Remember that this code generates a new entry in the Audit to register the elimination and is impossible
to delete.
As explained earlier, SharePoint saves some default activities if it has been configured. The Object Model sanctions
the creation of new types of entries using the method 'WriteAuditEvent'. For example, to save a document modi-
fied by an unauthorized user, the exception can be trapped using an Event Handler or WorkFlow and registered in
the Audit:
myAuditCol.WriteAuditEvent(SPAuditEventType.Custom,
"NotAuthorized",
"<myXml>Modification Not
Authorized</myXml>");
The code to detect the exception is not shown in the example,
and the latest parameter may include all the information about
the user and exception time. The method accepts three parame-
ters: the event type ('Custom' in this case), the title, and the
description of the event. Note also that the description has been
written inside a '<Name>' XML tag.
What You Can Achieve with Programming
MOSS and WSS lack interfaces to configure and see Audits at the
document level, but the SharePoint Object Model provides the
tools to manipulate them programmatically. The following exam-
ple will show the creation of two Windows applications to config-
ure Audit at a Document level, and to view the created registers.
Note: The source code (Visual Studio 2008) of the examples can
be downloaded from:
http://www.developer.com/net/net/article.php/
AuditInSharePoint2007.zip
9
Putting SharePoint to Work for You, an Internet.com Developer eBook. Copyright 2008, Jupitermedia Corp.
Putting SharePoint to Work for You
[
]
Figure 3: Audit configuration program for
documents in a document library.