www.codeguru.com/csharp/.net/net_data/sortinganditerating/article.php/c15335/

Back to Article

Home >> .NET / C# >> .NET >> Data & Databases >> Sorting and Iterating


Auditing in SharePoint 2007
Rating: none

Gustavo Velez (view profile)
June 27, 2008

Go to page: Prev  1  2  3  Next

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 download from this site.

(continued)



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));
}

'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.

Go to page: Prev  1  2  3  Next

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed






internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers