public SPAuditEntryCollectionInstance(ObjectInstance prototype, SPAuditEntryCollection auditEntryCollection) : this(prototype) { if (auditEntryCollection == null) { throw new ArgumentNullException("auditEntryCollection"); } m_auditEntryCollection = auditEntryCollection; }
private void PrepareAuditReport(SPAuditEntryCollection auditCollection, SPWeb web) { // build a memory stream of our file contents MemoryStream exportStream = CreateAuditReport(auditCollection); // save our export file to our doc libary web.AllowUnsafeUpdates = true; WriteAuditReport(exportStream, web); web.AllowUnsafeUpdates = false; }
private MemoryStream CreateAuditReport(SPAuditEntryCollection auditCollection, SPWeb web) { String currentWebName = web.Name.Trim(); StringBuilder fileContents = new StringBuilder(); StringBuilder headerContents = new StringBuilder(); StringBuilder fieldContents = new StringBuilder(); string delimiter = ","; // build our header line and add our header line to our file headerContents.AppendLine("Audit Report" + delimiter); headerContents.AppendLine("Location,Item Type,Location Type,Date Occurred,Source Name,User ID,Event Type,Event Name,Event Source,Event Data"); fileContents.AppendLine(headerContents.ToString()); // build our file contents foreach (SPAuditEntry audit in auditCollection) { String docLocation = audit.DocLocation == null ? delimiter : audit.DocLocation.ToString() + delimiter; if (docLocation.Contains(currentWebName)) { String itemType = audit.ItemType == null ? delimiter : audit.ItemType.ToString().Replace(";", "").Replace(",", "") + delimiter; String locationType = audit.LocationType == null ? delimiter : audit.LocationType.ToString().Replace(";", "").Replace(",", "") + delimiter; String occurred = audit.Occurred == null ? delimiter : audit.Occurred.ToString().Replace(";", "").Replace(",", "") + delimiter; String sourceName = audit.SourceName == null ? delimiter : audit.SourceName.ToString().Replace(";", "").Replace(",", "") + delimiter; String userId = RetrieveUsernameById(audit.UserId, web).Replace(";", "").Replace(",", "") + delimiter; String eventType = audit.Event == null ? delimiter : audit.Event.ToString().Replace(";", "").Replace(",", "") + delimiter; String eventName = audit.EventName == null ? delimiter : audit.EventName.ToString().Replace(";", "").Replace(",", "") + delimiter; String eventSource = audit.EventSource == null ? delimiter : audit.EventSource.ToString().Replace(";", "").Replace(",", "") + delimiter; String eventData = audit.EventData == null ? delimiter : audit.EventData.ToString().Replace(";", "").Replace(",", "") + delimiter; String reportLine = docLocation + itemType + locationType + occurred + sourceName + userId + eventType + eventName + eventSource + eventData; //Append the information from the audit collection fieldContents.Append(reportLine); fileContents.AppendLine(fieldContents.ToString()); //Clear the audit row StringBuilder fieldContents.Length = 0; fieldContents.Capacity = 0; } } // create a file and return it to the caller - this object must be disposed by the caller MemoryStream output = new MemoryStream(); StreamWriter writer = new StreamWriter(output, Encoding.UTF8); writer.Write(fileContents); writer.Flush(); return(output); }
// CheckOut, CheckIn, View, Delete, Update, ProfileChange, ChildDelete, // SchemaChange, Undelete, Workflow, Copy, Move, AuditMaskChange, // Search, ChildMove, SecGroupCreate, SecGroupDelete, SecGroupMemberAdd, // SecGroupMemberDel, SecRoleDefCreate, SecRoleDefDelete, SecRoleDefModify, SecRoleDefBreakInherit, // SecRoleBindUpdate, SecRoleBindInherit, SecRoleBindBreakInherit, EventsDeleted, Custom protected void Page_Load(object sender, EventArgs e) { //string documentUrl = "http://spwfe03p-bro/sites/StaffPortal/TestWork1/Pages/Test.aspx"; string documentUrl = "http://staffportal.myselfserve.com.au/sites/StaffPortal/TestWork1/Pages/Test-Bendigo-TAFE%E2%80%99s-2018-Apprentice-and-Industry-Awards.aspx"; string siteUrl = "http://spwfe03p-bro/sites/staffportal/TestWork1"; //string listName = "Auditing Details";//"Pages"; string listName = "Pages"; SPList listObj; try { using (SPSite site = new SPSite(documentUrl))//siteUrl { using (SPWeb web = site.OpenWeb()) { string DocLibName = documentUrl.Substring(web.Url.Length + 1).Substring(0, documentUrl.Substring(web.Url.Length + 1).IndexOf("/")); string DocNameWithExtension = documentUrl.Substring(documentUrl.LastIndexOf("/") + 1); string DocName = DocNameWithExtension.Substring(0, DocNameWithExtension.LastIndexOf(".")); SPListItem itemObj = QueryListItem(web, DocLibName); //listObj = (SPDocumentLibrary)web.Lists[listName]; //string s = web.Url; //string a = documentUrl.Substring(web.Url.Length + 1, documentUrl.Length - web.Url.Length); // documentUrl.Substring(web.Url.Length + 1); SPAuditQuery query = new SPAuditQuery(site); //query.RestrictToList(listObj); //query.RestrictToListItem(itemObj); //query.AddEventRestriction(SPAuditEventType.View); //query.AddEventRestriction(SPAuditEventType.Search); query.SetRangeStart(DateTime.Now.AddDays(-15)); query.SetRangeEnd(DateTime.Now); SPAuditEntryCollection auditCol = web.Audit.GetEntries(query); foreach (SPAuditEntry audit in auditCol) { string docName = audit.DocLocation; // audit.DocLocation = documentUrl.Substring(documentUrl.LastIndexOf("/")+1) int userID = audit.UserId; string userEmail = (web.AllUsers.GetByID(audit.UserId)).Email; string userName = (web.AllUsers.GetByID(audit.UserId)).Name; string ItemID = Convert.ToString(audit.ItemId); string ItemType = Convert.ToString(audit.ItemType); string EventType = Convert.ToString(audit.Event); DateTime OccuredDate = audit.Occurred; } } } } catch (Exception ex) { //Catch error in to ULS log. } }
public List <Aduit> GetAuditEntries() { SPSite site = new SPSite("http://sp2016"); SPAudit audit = site.Audit; SPAuditEntryCollection collection = audit.GetEntries(); List <Aduit> Data = new List <Aduit>(); foreach (SPAuditEntry i in collection) { Data.Add(TransExpV2 <SPAuditEntry, Aduit> .Trans(i)); } return(Data); }
private void startIndexing() { ChangeToolStripTextInAnotherThread("Indexing..."); DateTime minDate = DateTime.Now; System.TimeSpan diffResult; SPSecurity.RunWithElevatedPrivileges(delegate { for (int i = 1; i <= lstSiteCollections.SelectedItems.Count; i++) { using (SPSite siteCollection = new SPSite(lstSiteCollections.SelectedItems[i - 1].ToString())) { SPAuditQuery wssQuery = new SPAuditQuery(siteCollection); wssQuery.RowLimit = 10000; SPAuditEntryCollection auditCol = siteCollection.Audit.GetEntries(wssQuery); foreach (SPAuditEntry entry in auditCol) { if (entry.Occurred < minDate) { minDate = entry.Occurred; diffResult = DateTime.Now.Subtract(minDate); ChangeDaysQtyTextInAnotherThread(diffResult.Days.ToString()); } } auditCol = null; wssQuery = null; } } }); //Calculate date difference between now and minDate and assign to label. diffResult = DateTime.Now.Subtract(minDate); lblDaysQty.Text = diffResult.Days.ToString(); lblDaysQty.ForeColor = Color.Red; ChangeToolStripTextInAnotherThread("Ready"); }
private void ProcessAuditLogReport() { SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite site = new SPSite(SPContext.Current.Site.ID)) { using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID)) { //limit query to a specific site SPAuditQuery query = new SPAuditQuery(site); //set the query date range query.SetRangeStart(this.StartDate); query.SetRangeEnd(this.EndDate); SPAuditEntryCollection auditCol = web.Audit.GetEntries(query); PrepareAuditReport(auditCol, web); } } }); }
protected void DocViewCounts1(string siteObj, string libName) { #region t1 SPSecurity.RunWithElevatedPrivileges(delegate() { try { using (SPSite site = new SPSite(siteObj)) { using (SPWeb web = site.OpenWeb()) { SPList list = web.Lists[libName]; SPListItemCollection coll = list.GetItems(); foreach (SPListItem item in coll) { SPAuditQuery spQuery = new SPAuditQuery(site); spQuery.RestrictToListItem(item); SPAuditEntryCollection auditCol = site.Audit.GetEntries(spQuery); string docName = ""; int counter = 0; foreach (SPAuditEntry entry in auditCol) { if (entry.ItemType == SPAuditItemType.Document && entry.Event == SPAuditEventType.View) { try { var1 = entry.DocLocation.Substring(entry.DocLocation.LastIndexOf("/")); var2 = var1.Substring(var1.LastIndexOf("/")); var3 = var2.Substring(1); var4 = var3.Substring(var3.LastIndexOf('.') + 1); if (var4 != "aspx") { if (entry.EventSource == SPAuditEventSource.SharePoint) { if (docName != var3) { docName = var3; counter = 1; DataRow drRow = dtAudit.NewRow(); drRow["DocName"] = var3; drRow["DocLocation"] = entry.DocLocation; drRow["DownloadCount"] = 1; dtAudit.Rows.Add(drRow); } else { DataRow[] drExists = dtAudit.Select("DocName = '" + var3 + "' AND DocLocation = '" + entry.DocLocation + "'"); if (drExists != null && drExists.Length > 0) { drExists[0]["DownloadCount"] = Convert.ToInt32(drExists[0]["DownloadCount"]) + 1; } counter = counter + 1; } } } } catch (Exception ee) { LogAction(ee.Message); } } } } } } using (SPSite docSite = new SPSite("http://win-njfp7te48bn/sites/HVEDev")) { using (SPWeb docWeb = docSite.OpenWeb()) { SPList docList = docWeb.Lists["TopViewedDocs"]; foreach (DataRow rr in dtAudit.Rows) { try { #region SQL Command for select from [AAES Home].[dbo].[TopViewedDocsTable11] HVEFiles.dbConnection conn = new HVEFiles.dbConnection(); sqlDocName = rr["DocName"].ToString(); sqlDocLocation = rr["DocLocation"].ToString(); string sclearsql = string.Concat("SELECT * FROM [AAES Home].[dbo].[TopViewedDocsTable11] " + "WHERE DocName = @DocName AND DocLocation = @DocLocation"); SqlParameter[] parameter = { new SqlParameter("@DocName", SqlDbType.VarChar) { Value = sqlDocName }, new SqlParameter("@DocLocation", SqlDbType.VarChar) { Value = sqlDocLocation } }; DataTable tempTable = null; tempTable = conn.executeSelectQuery(sclearsql, parameter); if ((tempTable == null) || (tempTable.Rows.Count == 0)) { //insert string sclearsqlIns = string.Concat("INSERT INTO [AAES Home].[dbo].[TopViewedDocsTable11] " + "(DocName, DocLocation, DownloadCount) " + "VALUES(@DocName, @DocLocation, @DownloadCount)"); SqlParameter[] parameterUpd = { new SqlParameter("@DocName", SqlDbType.VarChar) { Value = rr["DocName"].ToString() }, new SqlParameter("@DocLocation", SqlDbType.VarChar) { Value = rr["DocLocation"].ToString() }, new SqlParameter("@DownloadCount", SqlDbType.Int) { Value = Convert.ToInt32(rr["DownloadCount"]) } }; bool isInsert = conn.executeInsertQuery(sclearsqlIns, parameterUpd); } else { //update string sclearsqlUpd = string.Concat("UPDATE [AAES Home].[dbo].[TopViewedDocsTable11] " + "SET DownloadCount = @DownloadCount " + "WHERE DocName = @DocName AND DocLocation = @DocLocation"); SqlParameter[] parameterUpd = { new SqlParameter("@DocName", SqlDbType.VarChar) { Value = rr["DocName"].ToString() }, new SqlParameter("@DocLocation", SqlDbType.VarChar) { Value = rr["DocLocation"].ToString() }, new SqlParameter("@DownloadCount", SqlDbType.Int) { Value = Convert.ToInt32(rr["DownloadCount"]) } }; bool isInsert = conn.executeUpdateQuery(sclearsqlUpd, parameterUpd); } #endregion } catch (Exception Exception1) { LogAction(Exception1.Message); } } } } } catch (Exception eee) { LogAction(eee.Message); } }); #endregion }
protected void DocViewCounts(string siteObj, string libName) { #region t1 SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite siteLogList = new SPSite("http://win-njfp7te48bn/sites/HVEDev")) { using (SPWeb webLogList = siteLogList.OpenWeb()) { SPList logList = webLogList.Lists["TimerLogList"]; SPListItem logListItems = null; try { using (SPSite site = new SPSite(siteObj)) { using (SPWeb web = site.OpenWeb()) { //lbltest.Text += "<br/>" + web.Title.ToString(); SPList list = web.Lists[libName]; //Documents SPListItemCollection coll = list.GetItems(); //Dictionary<string, int> dictionary = new Dictionary<string, int>(); //dtAudit.Columns.Add("DocName"); //dtAudit.Columns.Add("DocLocation"); //dtAudit.Columns.Add("DownloadCount"); foreach (SPListItem item in coll) { SPAuditQuery spQuery = new SPAuditQuery(site); spQuery.RestrictToListItem(item); SPAuditEntryCollection auditCol = site.Audit.GetEntries(spQuery); string docName = ""; int counter = 0; foreach (SPAuditEntry entry in auditCol) { if (entry.ItemType == SPAuditItemType.Document && entry.Event == SPAuditEventType.View) { try { var1 = entry.DocLocation.Substring(entry.DocLocation.LastIndexOf("/")); var2 = var1.Substring(var1.LastIndexOf("/")); var3 = var2.Substring(1); var4 = var3.Substring(var3.LastIndexOf('.') + 1); if (var4 != "aspx") { if (entry.EventSource == SPAuditEventSource.SharePoint) { if (docName != var3) { docName = var3; counter = 1; DataRow drRow = dtAudit.NewRow(); drRow["DocName"] = var3; drRow["DocLocation"] = entry.DocLocation; drRow["DownloadCount"] = 1; //dictionary.Add(var3, 1); dtAudit.Rows.Add(drRow); } else { DataRow[] drExists = dtAudit.Select("DocName = '" + var3 + "' AND DocLocation = '" + entry.DocLocation + "'"); if (drExists != null && drExists.Length > 0) { //int cont = drExists[0]["DownloadCount"]; drExists[0]["DownloadCount"] = Convert.ToInt32(drExists[0]["DownloadCount"]) + 1; //drExists[0]["DownloadCount"]; } //if (dictionary.TryGetValue(var3, out counter)) //{ // dictionary[var3] = counter + 1; //} counter = counter + 1; } } } } catch (Exception ee) { // SPSecurity.RunWithElevatedPrivileges(delegate() // { //lbltest.Text += "<br/>" + web.Title.ToString(); //SPList logList = webLogList.Lists["TimerLogList"]; //Documents //SPListItem logListItems = logList.Items.Add(); logListItems["Title"] = "2 :- SPAuditEntry :- " + ee.Message; logListItems.Update(); // } // } //}); } } } } } } using (SPSite docSite = new SPSite("http://win-njfp7te48bn/sites/HVEDev")) { using (SPWeb docWeb = docSite.OpenWeb()) { SPList docList = docWeb.Lists["TopViewedDocs"]; //foreach (KeyValuePair<string, int> pair in dictionary) foreach (DataRow rr in dtAudit.Rows) { #region List Data Insert ////////htmlStr.Append(("Document Name: " + pair.Key.ToString() + " - " + "Views Count: " + pair.Value.ToString()) + "<br>"); //////SPListItem li = docList.Items.Add(); //////li["DocName"] = rr["DocName"].ToString();// pair.Value.ToString(); //////li["DocLocation"] = rr["DocLocation"].ToString();// pair.Value.ToString(); //////li["DocViewCount"] = Convert.ToInt32(rr["DownloadCount"]);// pair.Value.ToString(); //////li["Title"] = "Data Entered at :- " + DateTime.Now.ToString(); ////////li["DocViewCount"] = pair.Value.ToString(); ////////li["Title"] = "Data Entered at :- " + DateTime.Now.ToString(); //////li.Update(); #endregion try { #region SQL Command for select from [AAES Home].[dbo].[TopViewedDocsTable11] HVEFiles.dbConnection conn = new HVEFiles.dbConnection(); //CustCode conn = new CustCode(); sqlDocName = rr["DocName"].ToString(); sqlDocLocation = rr["DocLocation"].ToString(); string sclearsql = string.Concat("SELECT * FROM [AAES Home].[dbo].[TopViewedDocsTable11] " + "WHERE DocName = @DocName AND DocLocation = @DocLocation"); SqlParameter[] parameter = { new SqlParameter("@DocName", SqlDbType.VarChar) { Value = sqlDocName }, new SqlParameter("@DocLocation", SqlDbType.VarChar) { Value = sqlDocLocation } }; DataTable tempTable = null; //SPListItem logListItems = logList.Items.Add(); logList.Items.Add(); logListItems["Title"] = "3 :- SQL FUnctions :- " + "before executeSelectQuery"; logListItems.Update(); tempTable = conn.executeSelectQuery(sclearsql, parameter); //SPListItem logListItems = logList.Items.Add(); logList.Items.Add(); logListItems["Title"] = "3 :- SQL FUnctions :- " + "After executeSelectQuery"; logListItems.Update(); if ((tempTable == null) || (tempTable.Rows.Count == 0)) { //insert string sclearsqlIns = string.Concat("INSERT INTO [AAES Home].[dbo].[TopViewedDocsTable11] " + "(DocName, DocLocation, DownloadCount) " + "VALUES(@DocName, @DocLocation, @DownloadCount)"); SqlParameter[] parameterUpd = { new SqlParameter("@DocName", SqlDbType.VarChar) { Value = rr["DocName"].ToString() }, new SqlParameter("@DocLocation", SqlDbType.VarChar) { Value = rr["DocLocation"].ToString() }, new SqlParameter("@DownloadCount", SqlDbType.Int) { Value = Convert.ToInt32(rr["DownloadCount"]) } }; bool isInsert = conn.executeInsertQuery(sclearsqlIns, parameterUpd); } else { //update string sclearsqlUpd = string.Concat("UPDATE [AAES Home].[dbo].[TopViewedDocsTable11] " + "SET DownloadCount = @DownloadCount " + "WHERE DocName = @DocName AND DocLocation = @DocLocation"); SqlParameter[] parameterUpd = { new SqlParameter("@DocName", SqlDbType.VarChar) { Value = rr["DocName"].ToString() }, new SqlParameter("@DocLocation", SqlDbType.VarChar) { Value = rr["DocLocation"].ToString() }, new SqlParameter("@DownloadCount", SqlDbType.Int) { Value = Convert.ToInt32(rr["DownloadCount"]) } }; bool isInsert = conn.executeUpdateQuery(sclearsqlUpd, parameterUpd); } #endregion } catch (Exception Exception1) { //SPSecurity.RunWithElevatedPrivileges(delegate() //{ // using (SPSite siteLogList = new SPSite("http://win-njfp7te48bn/sites/HVEDev")) // { // using (SPWeb webLogList = siteLogList.OpenWeb()) // { //lbltest.Text += "<br/>" + web.Title.ToString(); // SPList logList = webLogList.Lists["TimerLogList"]; //Documents // SPListItem logListItems = logList.Items.Add(); logList.Items.Add(); logListItems["Title"] = "3 :- SQL FUnctions :- " + Exception1.Message; logListItems.Update(); // } // } //}); // Exception1Label.Text = Exception1.Message; //Label1 } #region SQL Command for insert into SP // string sclearsql = "sp_TopViewedDocs"; /*"INSERT INTO TopViewedDocsTable (TraineeName, LoginID,LearnerDSId, TraineeOnboardingDate,SupervisorLogin,FunctionalArea1,TraineeOrganization,RoleFamily1,Role1,FunctionalSpeciality1,ManagerRightsNeeded,ArchiveRole1,ArchiveRole2,ArchiveRole3,TraineeLocation,RoleFamily2,Role2,FunctionalArea2,FunctionalSpeciality2,RoleFamily3,Role3,FunctionalArea3,FunctionalSpeciality3) " + //"VALUES (@TraineeName, @LoginID,@LearnerDSId, @TraineeOnboardingDate,@SupervisorLogin, @FunctionalArea1, @TraineeOrganization,@RoleFamily1,@Role1,@FunctionalSpeciality1,@ManagerRightsNeeded,@ArchiveRole1,@ArchiveRole2,@ArchiveRole3,@TraineeLocation,@RoleFamily2, @Role2, @FunctionalArea2,@FunctionalSpeciality2,@RoleFamily3,@Role3,@FunctionalArea3,@FunctionalSpeciality3)";*/ // SqlParameter[] parameter = { // new SqlParameter("@DocName", SqlDbType.VarChar) { Value = rr["DocName"].ToString() }, // new SqlParameter("@DocLocation", SqlDbType.VarChar) { Value = rr["DocLocation"].ToString() }, // new SqlParameter("@DownloadCount", SqlDbType.Int) { Value = Convert.ToInt32(rr["DownloadCount"]) } // }; // HVE.Files.dbConnection conn = new HVE.Files.dbConnection(); // bool isInsert = conn.executeInsertQuery(sclearsql, parameter); #endregion } } } } catch (Exception eee) { //Console.WriteLine(eee.Message); //SPSecurity.RunWithElevatedPrivileges(delegate() //{ // using (SPSite siteLogList1 = new SPSite("http://win-njfp7te48bn/sites/HVEDev")) // { // using (SPWeb webLogList1 = siteLogList1.OpenWeb()) // { //lbltest.Text += "<br/>" + web.Title.ToString(); //SPList logList1 = webLogList1.Lists["TimerLogList"]; //Documents //SPListItem logListItems = logList.Items.Add(); logList.Items.Add(); logListItems["Title"] = eee.Message; logListItems.Update(); // } // } //}); } //LiteralText.Text = htmlStr.ToString(); } } }); #endregion }
// POST api/values public SPAuditEntryCollection Post([FromBody] auditParam param) { using (SPSite site = new SPSite(param.siteUrl)) { using (SPWeb web = site.OpenWeb()) { // query SPAuditQuery query = new SPAuditQuery(site); // filters if (param.rowLimit != 0) { query.RowLimit = param.rowLimit; } // list and item if (param.listTitle != null) { SPList list = web.Lists[param.listTitle]; query.RestrictToList(list); if (param.itemId != 0) { SPListItem item = list.GetItemById(param.itemId); query.RestrictToListItem(item); } } // single user if (param.userId != 0) { query.RestrictToUser(param.userId); } // start and end times if (param.RangeStart != null) { DateTime start = Convert.ToDateTime(param.RangeStart); query.SetRangeStart(start); } if (param.RangeEnd != null) { DateTime end = Convert.ToDateTime(param.RangeEnd); query.SetRangeEnd(end); } // filter event types if (param.SPAuditEventType != null) { foreach (string s in param.SPAuditEventType) { if (s == "AuditMaskChange") { SPAuditEventType type = SPAuditEventType.AuditMaskChange; query.AddEventRestriction(type); } if (s == "CheckIn") { SPAuditEventType type = SPAuditEventType.CheckIn; query.AddEventRestriction(type); } if (s == "CheckOut") { SPAuditEventType type = SPAuditEventType.CheckOut; query.AddEventRestriction(type); } if (s == "ChildDelete") { SPAuditEventType type = SPAuditEventType.ChildDelete; query.AddEventRestriction(type); } if (s == "ChildMove") { SPAuditEventType type = SPAuditEventType.ChildMove; query.AddEventRestriction(type); } if (s == "Copy") { SPAuditEventType type = SPAuditEventType.Copy; query.AddEventRestriction(type); } if (s == "Custom") { SPAuditEventType type = SPAuditEventType.Custom; query.AddEventRestriction(type); } if (s == "Delete") { SPAuditEventType type = SPAuditEventType.Delete; query.AddEventRestriction(type); } if (s == "EventsDeleted") { SPAuditEventType type = SPAuditEventType.EventsDeleted; query.AddEventRestriction(type); } if (s == "FileFragmentWrite") { SPAuditEventType type = SPAuditEventType.FileFragmentWrite; query.AddEventRestriction(type); } if (s == "Move") { SPAuditEventType type = SPAuditEventType.Move; query.AddEventRestriction(type); } if (s == "ProfileChange") { SPAuditEventType type = SPAuditEventType.ProfileChange; query.AddEventRestriction(type); } if (s == "SchemaChange") { SPAuditEventType type = SPAuditEventType.SchemaChange; query.AddEventRestriction(type); } if (s == "Search") { SPAuditEventType type = SPAuditEventType.Search; query.AddEventRestriction(type); } if (s == "SecGroupCreate") { SPAuditEventType type = SPAuditEventType.SecGroupCreate; query.AddEventRestriction(type); } if (s == "SecGroupDelete") { SPAuditEventType type = SPAuditEventType.SecGroupDelete; query.AddEventRestriction(type); } if (s == "SecGroupMemberAdd") { SPAuditEventType type = SPAuditEventType.SecGroupMemberAdd; query.AddEventRestriction(type); } if (s == "SecGroupMemberDel") { SPAuditEventType type = SPAuditEventType.SecGroupMemberDel; query.AddEventRestriction(type); } if (s == "SecRoleBindBreakInherit") { SPAuditEventType type = SPAuditEventType.SecRoleBindBreakInherit; query.AddEventRestriction(type); } if (s == "SecRoleBindInherit") { SPAuditEventType type = SPAuditEventType.SecRoleBindInherit; query.AddEventRestriction(type); } if (s == "SecRoleBindUpdate") { SPAuditEventType type = SPAuditEventType.SecRoleBindUpdate; query.AddEventRestriction(type); } if (s == "SecRoleDefBreakInherit") { SPAuditEventType type = SPAuditEventType.SecRoleDefBreakInherit; query.AddEventRestriction(type); } if (s == "SecRoleDefCreate") { SPAuditEventType type = SPAuditEventType.SecRoleDefCreate; query.AddEventRestriction(type); } if (s == "SecRoleDefDelete") { SPAuditEventType type = SPAuditEventType.SecRoleDefDelete; query.AddEventRestriction(type); } if (s == "SecRoleDefModify") { SPAuditEventType type = SPAuditEventType.SecRoleDefModify; query.AddEventRestriction(type); } if (s == "Undelete") { SPAuditEventType type = SPAuditEventType.Undelete; query.AddEventRestriction(type); } if (s == "Update") { SPAuditEventType type = SPAuditEventType.Update; query.AddEventRestriction(type); } if (s == "View") { SPAuditEventType type = SPAuditEventType.View; query.AddEventRestriction(type); } if (s == "Workflow") { SPAuditEventType type = SPAuditEventType.Workflow; query.AddEventRestriction(type); } } } // download events SPAuditEntryCollection coll = site.Audit.GetEntries(query); return(coll); } } }
protected void Page_Load1() { #region t1 try { SPSecurity.RunWithElevatedPrivileges(delegate() { SPSite site = new SPSite("http://win-njfp7te48bn/sites/HVEDev"); using (SPWeb web = site.OpenWeb()) { lbltest.Text += "<br/>" + web.Title.ToString(); SPList list = web.Lists["Documents"]; //Documents SPListItemCollection coll = list.GetItems(); Dictionary <string, int> dictionary = new Dictionary <string, int>(); foreach (SPListItem item in coll) { SPAuditQuery spQuery = new SPAuditQuery(site); spQuery.RestrictToListItem(item); SPAuditEntryCollection auditCol = site.Audit.GetEntries(spQuery); string docName = ""; int counter = 0; foreach (SPAuditEntry entry in auditCol) { if (entry.ItemType == SPAuditItemType.Document && entry.Event == SPAuditEventType.View) { try { var1 = entry.DocLocation.Substring(entry.DocLocation.LastIndexOf("/")); var2 = var1.Substring(var1.LastIndexOf("/")); var3 = var2.Substring(1); var4 = var3.Substring(var3.LastIndexOf('.') + 1); if (var4 != "aspx") { if (entry.EventSource == SPAuditEventSource.SharePoint) { if (docName != var3) { docName = var3; counter = 1; dictionary.Add(var3, 1); } else { if (dictionary.TryGetValue(var3, out counter)) { dictionary[var3] = counter + 1; } counter = counter + 1; } } } } catch (Exception ee) { Label1.Text = ee.Message; } } } } foreach (KeyValuePair <string, int> pair in dictionary) { htmlStr.Append(("Document Name: " + pair.Key.ToString() + " - " + "Views Count: " + pair.Value.ToString()) + "<br>"); } } }); } catch (Exception eee) { Console.WriteLine(eee.Message); } LiteralText.Text = htmlStr.ToString(); #endregion }