September 30, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Auditing in SharePoint 2007

  • June 27, 2008
  • 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);
}
else
{
   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 -
            intCounter;
         if (myAuditBin.ToString().Substring(intCounter, 1) == "1"
      )
      clbAudi.SetItemChecked(myInversor, true);
   }
}
else
   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;
else
{
   for (int intCounter = 1; intCounter < clbAuditar.Items.Count;
      intCounter++)
   {
      if (clbAudit.GetItemChecked(intCounter) == true)
         myAuditBin += "1";
      else
         myAuditBin += "0";
   }

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

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



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel