public void RestrictToListItem(SPListItemInstance listItem)
        {
            if (listItem == null)
            {
                throw new JavaScriptException(this.Engine, "Error", "ListItem must be specified.");
            }

            m_auditQuery.RestrictToListItem(listItem.ListItem);
        }
        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
        }
Пример #4
0
        /// <summary>
        /// 查询数据绑定(使用sharepoint的API方法,放弃)
        /// </summary>
        protected void BindGrid()
        {
            if (String.IsNullOrEmpty(ListId))
            {
                return;
            }
            DateTime dtS = DateTime.MinValue;
            if (!dateStart.IsDateEmpty)
            {
                dtS = dateStart.SelectedDate;
            }
            DateTime dtE = DateTime.MaxValue;
            if (!dateEnd.IsDateEmpty)
            {
                dtE = dateEnd.SelectedDate;
            }
            //获取当前选择用户的ID
            int userId=0;
            if (userPicker.ResolvedEntities.Count > 0)
            {
                PickerEntity pe = (PickerEntity)userPicker.ResolvedEntities[0];
                userId = String.IsNullOrEmpty(pe.EntityData["SPUserID"]+"") ? Convert.ToInt32(pe.EntityData["SPUserID"] + "") : 0;
                //GetUserId(pe.Key);
            }
            //foreach (PickerEntity pe in userPicker.ResolvedEntities)
            //{
            //    string principalType = pe.EntityData["PrincipalType"].ToString();

            //    string loginName = pe.Key;

            //}
            SPSite siteColl = SPContext.Current.Site;
            SPWeb site = SPContext.Current.Web;

            SPSecurity.RunWithElevatedPrivileges(
              delegate()
              {
              using (SPSite ElevatedSiteCollection = new SPSite(siteColl.ID))
              {
                  using (SPWeb ElevatedSite = ElevatedSiteCollection.OpenWeb(site.ID))
                  {

                      //取得列表
                      SPList list = ElevatedSite.Lists[new Guid(ListId)];

                      SPAuditQuery wssQuery = new SPAuditQuery(ElevatedSiteCollection);

                      SPAuditEntryCollection auditCol;
                      //SPList list = siteCollection.OpenWeb().Lists["公司共享文档"];
                      wssQuery.RestrictToList(list);

                      //当是选择了某个文档库的某个文件的时候
                      if (!String.IsNullOrEmpty(ItemId))
                      {
                          //获取该文档
                          SPListItem item = list.Items.GetItemById(Convert.ToInt32(ItemId));
                          wssQuery.RestrictToListItem(item);
                      }

                      #region 页面事件的查询条件
                      //判断页面的事件查询条件是否选择
                      bool isChecked = false;
                      //添加选择的判断条件
                      foreach (ListItem item in cbHandle.Items)
                      {
                          if (item.Selected)
                          {
                              isChecked = true;
                              if (item.Value.Equals("3"))
                              {
                                  //查看
                                  wssQuery.AddEventRestriction(SPAuditEventType.View);
                              }
                              else if (item.Value.Equals("5"))
                              {
                                  //更新
                                  wssQuery.AddEventRestriction(SPAuditEventType.Update);
                              }
                              else if (item.Value.Equals("4"))
                              {
                                  //删除
                                  wssQuery.AddEventRestriction(SPAuditEventType.Delete);
                              }
                              else if (item.Value.Equals("2"))
                              {
                                  //签入
                                  wssQuery.AddEventRestriction(SPAuditEventType.CheckIn);
                              }
                              else if (item.Value.Equals("1"))
                              {
                                  //签出
                                  wssQuery.AddEventRestriction(SPAuditEventType.CheckOut);
                              }
                              else if (item.Value.Equals("10"))
                              {
                                  //从回收站还原
                                  wssQuery.AddEventRestriction(SPAuditEventType.Undelete);
                              }
                          }
                      }
                      //当没选择事件的时候,默认添加以下条件
                      if (!isChecked)
                      {
                          //查看
                          wssQuery.AddEventRestriction(SPAuditEventType.View);
                          //更新
                          wssQuery.AddEventRestriction(SPAuditEventType.Update);
                          //删除
                          wssQuery.AddEventRestriction(SPAuditEventType.Delete);
                          //签入
                          wssQuery.AddEventRestriction(SPAuditEventType.CheckIn);
                          //签出
                          wssQuery.AddEventRestriction(SPAuditEventType.CheckOut);
                          //从回收站还原
                          wssQuery.AddEventRestriction(SPAuditEventType.Undelete);
                      }
                      #endregion

                      wssQuery.SetRangeStart(dtS);
                      wssQuery.SetRangeEnd(dtE);

                      if (userId != 0)
                      {
                          wssQuery.RestrictToUser(userId);
                      }
                      auditCol = ElevatedSiteCollection.Audit.GetEntries(wssQuery);
                      //List<SPAuditEntry> data = new List<SPAuditEntry>();
                      //foreach (SPAuditEntry item in auditCol)
                      //{
                      //    item.ItemType=SPAuditItemType.Document
                      //    data.Add(item);
                      //}

                      this.AspNetPager1.PageIndex = PageIndex;
                      AspNetPager1.PageSize = PageSize;
                      AspNetPager1.RecordCount = auditCol.Count;
                      this.gvData.DataSource = auditCol;
                      this.gvData.DataBind();

                      // enumerate through audit log and read entries
                  }
              }

              });
        }
Пример #5
0
        // 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
        }