January 18, 2021
Hot Topics:

Auditing in SharePoint 2007

  • By Gustavo Velez
  • Send Email »
  • More Articles »

Similar to the first program, the Site Collection and Library names are used to fill the ComboBox with documents, and the change event from the ComboBox runs the code to view the Audit:

lblMensage.Text = string.Empty;
dgvAudit.DataSource = null;

SPSite mySite = new SPSite(txtMySiteURL.Text);
SPWeb myWeb = mySite.OpenWeb();
SPList myList = myWeb.Lists[txtLibraryName.Text];

if (ddlDocs.SelectedIndex >= 0)
   SPListItem myDoc = myList.Items.GetItemById
      (Convert.ToInt32(ddlDocs.SelectedIndex + 1));
   SPAuditQuery myAuditQuery = new SPAuditQuery(mySite);
   SPAuditEntryCollection myAuditColeccion =

   DataTable myTable = CrearDataTable();

   if (myAuditColeccion.Count > 0)
      foreach (SPAuditEntry myEntry in myAuditColeccion)
         DataRow myLinea = myTabla.NewRow();
         myRow["Date"] = myEntry.Occurred.ToString();
         SPUser myUser = myWeb.SiteUsers.GetByID(myEntry.UserId);
         myLinea["User"] = myUser.Name;
         myLinea["Type Action"] = myEntry.Event.ToString();

      dgvAudit.DataSource = myTable;
      lblMessage.Text = "No audit information for this document";

DataGrid 'dgvAudit' and DataTable 'myTable' show the data onscreen. After creating the objects in the normal way, the code uses a query to find the specific data of the document. All the Audit entries of the Portal are saved in one table of the Content DataBase, and the 'GetEntries' method returns the entire Table content. By using the queries of the type 'SPAuditQuery' and the method 'RestricToListItem', it is possible to filter the data of the selected document.

The pertinent data can be read by using a loop to create each row in the DataTable. Finally, the DataTable is connected to the DataGrid, and parameters are configured to display it onscreen. A Label is used to indicate whether any events are available for the document.


Careful registration of information in a Portal can be vitally important to a company, not only internally but also for possible juridical consequences. SharePoint 2007 has the necessary infrastructure to gather and conserve the entries in a secure way. MOSS can configure Audits for some parts of the system and is able to generate reports in Excel spreadsheets. WSS has no interface for configuration or reporting, but the SharePoint Object Model provides the necessary tools to create, configure, and review Audits.

Download the Code

You can download the code for this article here.

About the Author

Gustavo Velez is a MCSD Senior Application Developer for Winvision (http://www.winvision.nl), a Microsoft Gold Partner in the Netherlands. He has many years of experience developing Windows and Office applications, and more than five years of daily programming experience with SharePoint. The author's articles can be found in many of the leading trade magazines in English, Dutch, and Spanish. He is also pleased to be Webmaster of http://www.gavd.net/servers, the only Spanish-language site dedicated to SharePoint. Spanish-language readers may want to consult Velez's new book, Programación con SharePoint 2007 (http://www.dotnetmania.com/Libros/index.html).

Page 4 of 4

This article was originally published on June 27, 2008

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date