Configure SharePoint Site Collection Audit Settings Programmatically Using C# Client-Side Object Model (CSOM)

Introduction

The audit feature of Microsoft SharePoint Server or SharePoint Online lets you track user actions on a site's content types, lists, libraries, list items, and library files within your site collections. Knowing who has done what with which information is critical for many requirements, such as regulatory compliance and records management. In this article, you will learn how to configure SharePoint Site Collection Audit settings programmatically, using C# Client-Side Object Model (CSOM). This will be applicable to all SharePoint On-Premise platforms.

Prerequisite 

  • Create a site collection in SharePoint central admin.
  • Should have site collection administrator permission.

Enable Audit Settings using CSOM

The following section explains the flow for enabling the SharePoint site collection audit settings.

  1. Add the references to your C# project. The necessary references are Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll.

  2. Initialize the client context object with the site URL.
    1. ClientContext ctx = new ClientContext("https://SiteURL.com");   
  3.  If you are trying to access the SharePoint site, then you need to setup the site credentials with credentials parameter and get it set to the client context. 
    1. ctx.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication; ctx.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo("ID67891""Password");  
  4. Initialize audit object with the site object.
  5. Load and execute the query.
  6. Enable the Audit by setting the AuditFlags to AuditMaskType.All
  7. Set the number of days to hold the audit log by AuditLogTrimmingRetention property.
  8. Trim the audit log by setting true to TrimAuditLog.
  9. Load and execute the query. 
    1. Site= ctx.Site;    
    2. Audit audit = site.Audit;    
    3. ctx.Load(site);    
    4. ctx.Load(audit);    
    5. ctx.ExecuteQuery();    
    6. // Enable all auditing is site collection level    
    7. audit.AuditFlags = Microsoft.SharePoint.Client.AuditMaskType.All;    
    8. audit.Update();    
    9. // Adjust retention time to be 30 days    
    10. site.AuditLogTrimmingRetention = 30;    
    11. site.TrimAuditLog = true;    
    12. ctx.ExecuteQuery();    

Disable Audit Settings using CSOM

The following section explains the flow for disabling the SharePoint site collection audit settings.

  1. Add the references to your C# project. The necessary references are Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll.

  2. Initialize the client context object with the site URL. 
    1. ClientContext ctx = new ClientContext("https://SiteURL.com");   
  3. If you are trying to access a SharePoint site, then you need to setup the site credentials with the credentials parameter and get it set to the client context.
    1. ctx.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication; ctx.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo("ID67891""Password");    
  4. Initialize audit object with the site object.
  5. Load and execute the query.
  6. Disable the Audit by setting the AuditFlags to AuditMaskType.None.
  7. Trim audit log setting TrimAuditLog to false.
  8. Load and execute the query.
    1. Site= ctx.Site;      
    2. Audit audit = site.Audit;      
    3. ctx.Load(site);      
    4. ctx.Load(audit);      
    5. ctx.ExecuteQuery();      
    6. // Set remove any auditing from site colelction level      
    7. audit.AuditFlags = Microsoft.SharePoint.Client.AuditMaskType.None;      
    8. audit.Update();      
    9. site.TrimAuditLog = false;      
    10. ctx.ExecuteQuery();      

To check whether the audit log settings are enabled or disabled, follow these steps.

  1. Go to Settings -> Site Settings.

  2. On the Site Settings page, under Site Collection Administration, select Site collection audit settings.

    settings 

To view the audit logs, go to the location where audit reports are stored, like Shared Library.

 Library
Summary

Thus, you have learned how to configure SharePoint Site Collection Audit settings programmatically, using C# Client-Side Object Model (CSOM).