Friday, January 14, 2011

Get/View Audit Entries for Document Library in sharepoint 2010

SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite ElevatedSiteCollection = new SPSite("yoursite"))
            {
                using (SPWeb ElevatedSite = ElevatedSiteCollection.OpenWeb())
                {
                    Console.Write(ElevatedSite.Site.Url); SPList list = ElevatedSite.Lists["TestDocuments"];
                    // ElevatedSite.Lists[new Guid(ListId)];
                    Console.Write(list.Audit.AuditFlags + "\n");
                    SPListItem item = list.Items.GetItemById(list.Items[0].ID);
                    Console.Write(item.Title); SPAuditQuery wssQuery;
                    SPAuditEntryCollection auditCol; wssQuery = new SPAuditQuery(ElevatedSiteCollection);
                    wssQuery.RestrictToListItem(item); auditCol = ElevatedSite.Audit.GetEntries(wssQuery);
                    foreach (SPAuditEntry entry in auditCol)
                    {
                        // Query audit log with elevated privileges.
                        Console.Write(entry.Occurred.ToLocalTime() + " : " + entry.Event + " by " + GetUserNameById(entry.UserId, ElevatedSite) + "\n");
                    }
                }
            }
        });

1 comment:

  1. Thanks for sharing the helpful script, it helps to see the most viewed items in document library. I found this SharePoint auditing tool ( http://www.lepide.com/sharepoint-audit/ ) that helps to show the most viewed audit entries in library and generate report on-demand.

    ReplyDelete

Followers