-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebUsageReceiver.cs
125 lines (102 loc) · 4.1 KB
/
WebUsageReceiver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Collections.Generic;
using CsvHelper.Configuration;
using Microsoft.SharePoint.Client;
using SharePointRunner.SDK;
namespace SharePointRunner.LauncherV1
{
/// <summary>
/// Site usage informations
/// </summary>
public class WebUsageInfo
{
public string SiteUrl { get; set; } = string.Empty;
public string SiteTitle { get; set; } = string.Empty;
public DateTime SiteCreationDate { get; set; }
public string SiteCreationDateString => SiteCreationDate.ToString("yyyy-MM-dd HH,mm,ss");
public int SiteItemCount { get; set; } = 0;
public string ListUrl { get; set; } = string.Empty;
public string ListTitle { get; set; } = string.Empty;
public DateTime LastItemUpdate { get; set; } = new DateTime(1900, 1, 1);
public string LastItemUpdateString => LastItemUpdate.ToString("yyyy-MM-dd HH,mm,ss");
public string LastEditor { get; set; }
}
/// <summary>
/// CSV mapping
/// </summary>
internal class WebUsageInfoMap : ClassMap<WebUsageInfo>
{
/// <summary>
/// Constructor
/// </summary>
public WebUsageInfoMap()
{
Map(m => m.SiteUrl);
Map(m => m.SiteTitle);
Map(m => m.SiteCreationDateString);
Map(m => m.SiteItemCount);
Map(m => m.ListTitle);
Map(m => m.LastItemUpdateString);
}
}
public class WebUsageReceiver : Receiver
{
private CsvWriterWrapper<WebUsageInfo, WebUsageInfoMap> webUsageFileWriter;
private WebUsageInfo webUsageInfo;
public override void OnStart()
{
string webUsageFileName = $"AuditWebUsage-{DateTime.Now.ToString("yyyy-MM-dd HH,mm,ss")}.csv";
webUsageFileWriter = new CsvWriterWrapper<WebUsageInfo, WebUsageInfoMap>(webUsageFileName);
}
public override void OnSiteRunningStart(Web web)
{
web.Context.Load(web,
w => w.Title,
w => w.Url,
w => w.Created);
web.Context.ExecuteQuery();
webUsageInfo = new WebUsageInfo()
{
SiteUrl = web.Url,
SiteTitle = web.Title,
SiteCreationDate = web.Created
};
}
public override void OnListRunningStart(List list)
{
list.Context.Load(list,
l => l.Title,
l => l.RootFolder.ServerRelativeUrl,
l => l.Hidden,
l => l.ItemCount);
list.Context.ExecuteQuery();
if (list.ItemCount < 5000 && !list.Hidden && !list.RootFolder.ServerRelativeUrl.Contains("/Catalogs/") && list.Title != "Long Running Operation Status")
{
CamlQuery query = new CamlQuery();
query.ViewXml = "<View Scope='Recursive'><RowLimit>1</RowLimit><Query><OrderBy><FieldRef Name='Modified' Ascending='False' /></OrderBy></Query></View>";
ListItemCollection items = list.GetItems(query);
list.Context.Load(items);
list.Context.ExecuteQuery();
webUsageInfo.SiteItemCount += list.ItemCount;
foreach (ListItem listItem in items)
{
list.Context.Load(listItem);
list.Context.ExecuteQuery();
if (DateTime.TryParse(listItem["Modified"]?.ToString(), out DateTime modified) && modified > webUsageInfo.LastItemUpdate)
{
webUsageInfo.LastItemUpdate = modified;
webUsageInfo.ListUrl = list.RootFolder.ServerRelativeUrl;
webUsageInfo.ListTitle = list.Title;
FieldUserValue userField = listItem["Editor"] as FieldUserValue;
webUsageInfo.LastEditor = userField.LookupValue;
}
}
}
}
public override void OnSiteRunningEnd(Web web)
{
// Write CSV
webUsageFileWriter.WriteRecord(webUsageInfo);
}
}
}