How to fetch SharePoint audits using Azure application in C#

Sakthi 1 Reputation point
2022-12-07T12:18:26.957+00:00

string TenantID = "";
string authString = "https://login.windows.net/" + TenantID;
string SPServiceUrl = "https://manage.office.com/api/v1.0/" + TenantID + "/activity/feed/subscriptions/content";
string resourceId = "https://manage.office.com";
string clientId = "";
string clientSecret = "";
#endregion

            try  
            {  
                var authenticationContext = new AuthenticationContext(authString, false);  
                ClientCredential clientCred = new ClientCredential(clientId, clientSecret);  
                AuthenticationResult authenticationResult = null;  
                Task runTask = Task.Run(async () => authenticationResult = await authenticationContext.AcquireTokenAsync(resourceId, clientCred));  
                runTask.Wait();  
                string token = authenticationResult.AccessToken;  

}

Microsoft 365 and Office SharePoint Development
Microsoft 365 and Office SharePoint For business Windows
{count} votes

1 answer

Sort by: Most helpful
  1. Tong Zhang_MSFT 9,251 Reputation points
    2022-12-08T05:38:27.937+00:00

    Hi @Sakthi ,

    According to my research and testing, you can try to use the following code to get SharePoint audit logs:

    try    
    {    
        using(SPSite site = new SPSite("https://SiteURL.com")) {    
            using(SPWeb web = site.OpenWeb()) {    
                SPAuditQuery query = new SPAuditQuery(site);    
                //query.RestrictToList(list);      
                //query.RestrictToListItem(ListItem);      
                query.AddEventRestriction(SPAuditEventType.View);    
                query.AddEventRestriction(SPAuditEventType.Search);    
                query.SetRangeStart(DateTime.Now.AddHours(-1));    
                query.SetRangeEnd(DateTime.Now);    
                SPAuditEntryCollection auditCol = web.Audit.GetEntries(query);    
                foreach(SPAuditEntry audit in auditCol) {    
                    string docName = audit.DocLocation;    
                    string userID = audit.UserId;    
                    string ItemID = Convert.ToString(audit.ItemId);    
                    string ItemType = Convert.ToString(audit.ItemType);    
                    string EventType = Convert.ToString(audit.Event);    
                    string OccuredDate = audit.Occurred;    
                }    
            }    
        }    
    } catch (Exception ex) {    
        //Catch error in to ULS log.      
    }      
    

    More information for reference:
    Retrieve Sharepoint Audit Logs Programmatically Using C# Server-Side Object Model
    SharePoint Online Audit Logs

    Hope it can help you. Thanks for your understanding.

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.