February 27, 2021
Hot Topics:

Auditing in SharePoint 2007

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

'clbAudt' is a control of the type 'System.Windows.Forms.CheckedListBox'. When one document has been chosen, the change event of the ComboBox calls the routine to read all the selected Audits and to show them onscreen:

SPSite mySite = new SPSite(txtMySiteURL.Text);
SPWeb myWeb = mySite.OpenWeb();
SPList myList = myWeb.Lists[txtLibraryName.Text];
SPListItem myItem = myList.Items.GetItemById(Convert.ToInt32
   (ddlDocs.SelectedIndex + 1));

if ((int)myItem.Audit.AuditFlags == -1)
   clbAudit.SetItemChecked(0, true);
   string myAuditBin = DecimalToBin((int)myItem.Audit.AuditFlags);
   if (string.IsNullOrEmpty(myAuditBin) == false)
      for (int intCounter = myAuditBin.ToString().Length - 1;
         intCounter >= 0; intCounter--)
         int myInversor = myAuditBin.ToString().Length -
         if (myAuditBin.ToString().Substring(intCounter, 1) == "1"
      clbAudi.SetItemChecked(myInversor, true);
   clbAudit.SetItemChecked(15, true);

After creating the objects to contain the Site, Web, Listm and Document information, the 'AuditFlags' property generates the necessary information. The value '-1' indicates that all the event types have been chosen for auditing, and if the value is null, no audit will occur.

The 'AuditFlags' property conserves the Audit values encrypted in the form of a binary 'OR' operation of each of the values of the 'SPAuditMaskType' enumeration converted to a decimal value. The variable 'myAuditBin' reads the decimal value, converts it to a binary value using the routine 'DecimalToBin' and then reads each bit one by one. If the bit is '1', the correspondent element in the list is checked. Note that the binary value is read from the highest value using a loop in this direction.

The event of the save button keeps the data in a similar way, but reads the selected elements from the screen, encrypting them in the correct way and saving them in the property:

SPAudit myAuditItem = myItem.Audit;
if(clbAudit.GetItemChecked(0) == true)
   myAuditItem.AuditFlags = SPAuditMaskType.All;
   for (int intCounter = 1; intCounter < clbAuditar.Items.Count;
      if (clbAudit.GetItemChecked(intCounter) == true)
         myAuditBin += "1";
         myAuditBin += "0";

   int myAuditDecimal = BinToDecimal(myAuditBin);
   myAuditItem.AuditFlags = (SPAuditMaskType)miAuditDecimal;

By using the statement 'For', it is possible to read each check box onscreen, and build the string variable 'myAuditBin' again. The binary string is converted to the decimal equivalent using the routine 'BinToDecimal' and stores it in the 'AuditFlags' property. Finally, the 'Update' method persists the data in SharePoint. There are many algorithms to make the binary-to-decimal and decimal-to-binary conversions (a concatenation of OR operations, for example); the two routines presented are only for demonstration, and not to be used as an example of algorithms theory.

After the document has been configured to Audit the events, it also is necessary to review what has occurred. The second program displays the Audit registry of the chosen document:

Figure 4: Program to read the Audit registry of a document

Page 3 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