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:

Followers