情景:如何查看某个文档库中那个文档被浏览的次数最多?
解决方案:对该文档库开启文档审核日志,然后查看审核就可以轻松找到,那么通过代码的方式是怎样实现的呢?如下:
1: static void ReadAuditLog()
2: {
3: using (SPSite site = new SPSite("http://web.fly.com:2010"))
4: {
5: using(SPWeb web=site.OpenWeb())
6: {
7: SPList list=web.Lists["DocLibA"];
8: SPUser user = web.CurrentUser;
9: SPAuditQuery query = new SPAuditQuery(site);
10: query.AddEventRestriction(SPAuditEventType.View);
11: query.SetRangeEnd(DateTime.Now);
12: query.SetRangeStart(DateTime.Now.AddMonths(-1));
13: query.RestrictToList(list);
14: SPAuditEntryCollection auditCol = web.Audit.GetEntries(query);
15: IDictionaryitemCol = new Dictionary ();
16: foreach (SPAuditEntry audit in auditCol)
17: {
18: if (string.IsNullOrEmpty(audit.EventData)||
19: (audit.DocLocation.EndsWith(".aspx")))
20: {
21: continue;
22: }
23: if (!itemCol.Keys.Contains(audit.DocLocation))
24: {
25: itemCol.Add(audit.DocLocation, 1);
26: }
27: else
28: {
29: itemCol[audit.DocLocation] += 1;
30: }
31: }
32:
33: int maxCount = -1;
34: string itemLocation = string.Empty;
35: foreach (string id in itemCol.Keys)
36: {
37: if (itemCol[id] > maxCount)
38: {
39: maxCount = itemCol[id];
40: itemLocation = id;
41: }
42: }
43:
44: Console.WriteLine("Item:{0},Count:{1}", itemLocation, maxCount);
45: }
46: }
47: }