Wednesday, May 25, 2011

Determine auditing configuration details for all of your SharePoint site collections

I recently had a customer whose SharePoint audit tables had getting very large. They wanted to know which site collections had auditing enabled so they could meet with their end users to determine if auditing was really necessary. Here is a code snippet for finding out which SharePoint sites have auditing enabled. The code will loop through all site collections in the farm and then create a CSV file with auditing configuration details.
InstructionsCreate a c# Console Application in Visual Studio, add a reference to Microsoft.SharePoint, and then add the following code to Program.cs. This will work with SharePoint 2007 and 2010 as long as you reference the correct assembly. Don’t forget to make sure your Visual Studio project’s Platform Target is Any CPU or x64.



using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace SPCheckAudit
{
   class Program
   {
       [STAThread]
       static void Main(string[] args)
       {
           StringBuilder sb = new StringBuilder();
           sb.Append("SiteID,SiteURL,ContentDB,");
           foreach (int value in System.Enum.GetValues(typeof(SPAuditMaskType)))
           {
               sb.Append(System.Enum.GetName(typeof(SPAuditMaskType), value) + ",");
           }
           sb.Append("\r\n");
           try
           {
               SPServiceCollection services = SPFarm.Local.Services;
               foreach (SPService curService in services)
               {
                   if (curService is SPWebService)
                   {
                       SPWebService webService = (SPWebService)curService;
                       foreach (SPWebApplication spWebApplication in webService.WebApplications)
                       {
                           foreach (SPSite site in spWebApplication.Sites)
                           {
                               if (site.Audit.AuditFlags != SPAuditMaskType.None)
                               {
                                   sb.Append(site.ID + "," + site.Url + "," + site.ContentDatabase.Name + ",");
                                   int enumLength = System.Enum.GetValues(typeof(SPAuditMaskType)).Length - 1;
                                   int counter = 0;
                                   foreach (int value in System.Enum.GetValues(typeof(SPAuditMaskType)))
                                   {
                                       sb.Append(((site.Audit.AuditFlags & (SPAuditMaskType)value) == (SPAuditMaskType)value).ToString());
                                       if (enumLength != counter)
                                       {
                                           sb.Append(",");
                                       }
                                       counter++;
                                   }
                                   sb.Append("\r\n");
                               }
                           }
                       }
                   }
               }
           }
           catch (Exception ex)
           {
               sb.Append(ex.ToString());
           }
           SaveFileDialog sfd = new SaveFileDialog();
           sfd.Filter = "CSV File|*.csv";
           sfd.Title = "Save File";
           if (sfd.ShowDialog() == DialogResult.OK)
           {
               using (var writer = new StreamWriter(sfd.FileName, true))
               {
                   writer.WriteLine(sb.ToString());
               }
           }
       }
   }
}
image

No comments:

Post a Comment