October 24, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Auditing in SharePoint 2007

  • June 27, 2008
  • By Gustavo Velez
  • Send Email »
  • More Articles »

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 millisecond. 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 modified 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 parameters: 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 example will show the creation of two Windows applications to configure 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 this site.

The configuration program uses the names of the Site Collection and Document Library as input parameters and gives you the ability to configure the Audit level of each individual document in the Library.

Figure 3: Audit configuration program for documents in a Document Library

The OnLoad event in the program calls a routine that uses the names of Site Collection and Library to fill the Document ComboBox. Bear in mind that the code is only to explain the concepts of Audit programming and it is not sufficient for production code (doesn't have any type of validation of try/catch statements, for example).

The possible event types can be found in the SPAuditMaskType enumeration, and using a loop can be displayed onscreen:

for (int
   intCounter = 0; intCounter <
      Enum.GetNames(typeof(SPAuditMaskType)).Count();
   intCounter++)
{
   clbAudit.Items.Add(Enum.GetNames(typeof(SPAuditMaskType))
   .ElementAt(intCounter));
}




Page 2 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel