-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventsExport.js
More file actions
385 lines (313 loc) · 13.8 KB
/
eventsExport.js
File metadata and controls
385 lines (313 loc) · 13.8 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// MCN EVENTS
//:::::::::::::::::::::: DOCUMENT READY STARTS ::::::::::::::::::::::
$(document).ready(function () {
console.log('eventExport.js is connected');
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', Getdata);
});
// added this function because sp.js wouldn't load
function Getdata() {
try {
clientContext = new SP.ClientContext.get_current();
} catch (err) {
alert(err);
}
}
// _-_-_-_-_-_-_ Export to Word _-_-_-_-_-_-_
function export2Word() {
alert('Export Word clicked!');
// grab values
getSearchValues();
// if no search is performed before pressing the button throw error
if (searchInputVal === "" && eventStartDateVal === "" && eventEndDateVal === "" && catDispVal === "" && regionDispVal === "" && countyDispVal === "" && countyDispVal === "" && venueDispVal === "" && RIDispVal === "" && CIDispVal === "") {
alert('Please perform a search before trying to export to PDF');
} else {
// if all OK, then do query and creates Excel doc
retrieveListItems();
}
}
// ::::::::::::::::::::::: Get all the current values of the search :::::::::::::::::::::::
function getSearchValues() {
//get the values of the search input and the refiners
try {
searchInputVal = $("input[placeholder='Enter your search terms...']").val();
} catch (error) {
searchInputVal = ''; // set it to empty string to avoid 'undefined' errors later
console.log('no value in the search box');
}
// Event date values
try {
eventStartDateVal = $('input[id*="DatePicker"]:first').val();
var date = new Date(eventStartDateVal);
eventStartDateFmt = date.toISOString().split("T")[0] + 'T00:01:00Z'; // convert it to a CAML friendly format
} catch (error) {
eventStartDateVal = '';
console.log('no start date');
}
try {
eventEndDateVal = $('input[id*="DatePicker"]:last').val();
var date2 = new Date(eventEndDateVal);
eventEndDateFmt = date2.toISOString();
} catch (error) {
eventEndDateVal = '';
console.log('no end date');
}
// Category value
try {
catDispVal = $("div.ellipsis_e61d9da1:contains('Category')").text().slice(11, -2);
} catch (error) {
catDispVal = '';
console.log('no category refiner value');
}
// Region value
try {
regionDispVal = $("div.ellipsis_e61d9da1:contains('Region')").text().slice(9, -2);
} catch (error) {
regionDispVal = '';
console.log('no region refiner value');
}
// County value
try {
countyDispVal = $("div.ellipsis_e61d9da1:contains('County')").text().slice(9, -2);
} catch (error) {
countyDispVal = '';
console.log('no county refiner value');
}
// Venue value
try {
venueDispVal = $("div.ellipsis_e61d9da1:contains('Venue')").text().slice(8, -2);
} catch (error) {
venueDispVal = '';
console.log('no venue refiner value');
}
// Recurring info
try {
RIDispVal = $("div.ellipsis_e61d9da1:contains('Recurring')").text().slice(17, -2);
} catch (error) {
RIDispVal = '';
console.log('no Recurring info refiner value');
}
//Contact info value
try {
CIDispVal = $("div.ellipsis_e61d9da1:contains('Contact')").text().slice(15, -2);
} catch (error) {
CIDispVal = '';
console.log('no Contact info value');
}
}
// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// :::::::::::::::::::: Query to retrieve the list items :::::::::::::::::::::
var clientContext;
var website;
var collListem;
function retrieveListItems() {
//returns the context information about the current web application
var clientContext = new SP.ClientContext('https://bauer.sharepoint.com/sites/UK-Article-Library');
oList = clientContext.get_web().get_lists().getByTitle('MCNEvents2');
var camlQuery = new SP.CamlQuery();
var filtersList = [];
var camlSyntax = '';
// Individual CAML queries for each filter, to add up
// will search the term in all the columns
if (searchInputVal !== '') {
filtersList.push(
"<Or><Contains> <FieldRef Name='Title' /> <Value Type='Text'>" + searchInputVal + "</Value> </Contains>" +
//"<Or> <Eq> <FieldRef Name='Category' /> <Value Type='Choice'>" + searchInputVal + "</Value> </Eq>" +
"<Or> <Contains> <FieldRef Name='Region' /> <Value Type='Text'>" + searchInputVal + "</Value> </Contains>" +
"<Or> <Contains> <FieldRef Name='Venue' /> <Value Type='Text'>" + searchInputVal + "</Value> </Contains>" +
"<Or> <Contains> <FieldRef Name='County' /> <Value Type='Text'>" + searchInputVal + "</Value> </Contains>" +
"<Or> <Contains> <FieldRef Name='RecurringInfo' /> <Value Type='Text'>" + searchInputVal + "</Value> </Contains>" +
"<Contains> <FieldRef Name='ContactInfo' /> <Value Type='Text'>" + searchInputVal + "</Value> </Contains>" +
"</Or> </Or> </Or> </Or> </Or>"
//</Or>
)
}
// search in Event date column
if (eventStartDateVal !== "") {
filtersList.push("<Geq><FieldRef Name='EventDate'/><Value Type='DateTime' IncludeTimeValue='TRUE'>" + eventStartDateFmt + "</Value> </Geq>")
}
if (eventEndDateVal !== "") {
filtersList.push("<Leq> <FieldRef Name='EventDate'/><Value Type='DateTime' IncludeTimeValue='TRUE'>" + eventEndDateFmt + "</Value> </Leq>")
}
// search in Category
//if(catDispVal !== '') {
// filtersList.push("<Eq><FieldRef Name='Category'/> <Value Type='Choice'>" + catDispVal + "</Value></Eq>")
//}
// search in Region
if (regionDispVal !== '') {
filtersList.push("<Contains><FieldRef Name='Region'/> <Value Type='Text'>" + regionDispVal + "</Value></Contains>")
}
// search in County
if (countyDispVal !== '') {
filtersList.push("<Contains><FieldRef Name='County'/> <Value Type='Text'>" + countyDispVal + "</Value></Contains>")
}
// search in Venue
if (venueDispVal !== '') {
filtersList.push("<Contains><FieldRef Name='Venue'/> <Value Type='Text'>" + venueDispVal + "</Value></Contains>")
}
// search in Recurring Info
if (RIDispVal !== '') {
filtersList.push("<Contains><FieldRef Name='RecurringInfo'/> <Value Type='Text'>" + RIDispVal + "</Value></Contains>")
}
// search in Contact Info
if (CIDispVal !== '') {
filtersList.push("<Contains><FieldRef Name='ContactInfo'/> <Value Type='Text'>" + CIDispVal + "</Value></Contains>")
}
console.log('filtersList contains: ', filtersList);
// if CAML query has 1 search term
if (filtersList.length === 1) {
camlSyntax = "<Where>" + filtersList[0] + "</Where>";
}
// if CAML query has 2 search terms
else if (filtersList.length === 2) {
camlSyntax = "<Where><And>" + filtersList[0] + filtersList[1] + "</And></Where>";
}
// if CAML query has 3 search terms
else if (filtersList.length === 3) {
camlSyntax = "<Where><And>" + filtersList[0] + "<And>" + filtersList[1] + filtersList[2] + "</And></And></Where>";
}
// if CAML query has 4 search terms
if (filtersList.length === 4) {
query.Query = "<Where><And>" + filtersList[0] + "<And>" + filtersList[1] + "<And>" + filtersList[2] + filtersList[3] + "</And></And></And></Where>";
}
// if CAML query has 5 search terms
if (filtersList.length === 5) {
query.Query = "<Where><And>" + filtersList[0] + "<And>" + filtersList[1] + "<And>" + filtersList[2] + "<And>" + filtersList[3] + filtersList[4] + "</And></And></And></And></Where>";
}
// if CAML query has 6 search terms
if (filtersList.length === 6) {
query.Query = "<Where><And>" + filtersList[0] + "<And>" + filtersList[1] + "<And>" + filtersList[2] + "<And>" + filtersList[3] + "<And>" + filtersList[4] + filtersList[5] + "</And></And></And></And></And></Where>";
}
// if CAML query has 7 search terms
if (filtersList.length === 7) {
query.Query = "<Where><And>" + filtersList[0] + "<And>" + filtersList[1] + "<And>" + filtersList[2] + "<And>" + filtersList[3] + "<And>" + filtersList[4] + "<And>" + filtersList[5] + filtersList[6] + "</And></And></And></And></And></And></Where>";
}
// if CAML query has 8 search terms
if (filtersList.length === 8) {
query.Query = "<Where><And>" + filtersList[0] + "<And>" + filtersList[1] + "<And>" + filtersList[2] + "<And>" + filtersList[3] + "<And>" + filtersList[4] + "<And>" + filtersList[5] + "<And>" + filtersList[6] + filtersList[7] + "</And></And></And></And></And></And></And></Where>";
}
// Set CAML query
camlQuery.set_viewXml("<View><Query>" + camlSyntax + "<OrderBy><FieldRef Name='EventDate' Ascending='TRUE'/><FieldRef Name='Category' Ascending='TRUE'/><FieldRef Name='Region' Ascending='TRUE'/></OrderBy><RowLimit>5000</RowLimit></Query></View>");
// retrieve the items from the CAML query
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onExportQuerySucceeded),
Function.createDelegate(this, this.onExportQueryFailed)
);
}
// if query succeeds
function onExportQuerySucceeded(sender, args) {
var listItemInfo = '';
var listItemEnumerator = collListItem.getEnumerator();
var currentCategory;
var prevCategory;
var currentDate;
var prevDate;
var pd;
var cd;
var count = 0;
var array = [];
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
count++;
currentCategory = oListItem.get_item('Category');
currentDate = oListItem.get_item('EventDate');
currentTime = oListItem.get_item('EventTime');
title = oListItem.get_item('Title');
venue = oListItem.get_item('Venue');
county = oListItem.get_item('County');
country = oListItem.get_item('Country');
price = oListItem.get_item('Price');
contact = oListItem.get_item('ContactInfo');
var region = oListItem.get_item('Region');
// if current date event is same as the previous event date, print nothing
try {
pd = prevDate.getDate();
} catch (ex) {
pd = 0;
}
try {
cd = currentDate.getDate();
} catch (ex) {
cd = 0;
}
if (cd === pd) {
listItemInfo += '\n';
}
// if new date, print
else {
var dateFormatted = oListItem.get_item('EventDate').format('dddd dd MMMM yyyy');
listItemInfo += '\n' + '\n' + '\n' + dateFormatted + '\n';
prevDate = currentDate;
}
// check if the category of the item is the same as the previous category, print nothing
if (currentCategory === prevCategory) {
listItemInfo += '\n';
}
// if new category, print
else {
listItemInfo += '\n' + '\n' + oListItem.get_item('Category') + '\n =============================================';
prevCategory = currentCategory;
}
//___________ print the rest of data________
listItemInfo += '\n' + oListItem.get_item('Title') + '\n';
// add different signs before REGION depending on which region the event is
if (region != null && region.toUpperCase() === 'SOUTH') {
listItemInfo += '\n$';
}
if (region != null && region.toUpperCase() === 'MIDLANDS') {
listItemInfo += '\n%';
}
if ((region != null && region.toUpperCase() === 'NORTH') || (region != null && region.toUpperCase() === 'SCOTLAND')) {
listItemInfo += '\n*';
}
if ((region != null && region.toUpperCase() === 'SOUTH EAST') || (region != null && region.toUpperCase() === 'SOUTH WEST') || (region != null && region.toUpperCase() === 'EAST')) {
listItemInfo += '\n^';
}
if (title != null) {
listItemInfo += ' ' + title;
}
if (venue != null) {
listItemInfo += ' ' + venue;
}
if (county != null) {
listItemInfo += ' ' + county;
}
if (country != null) {
listItemInfo += ' ' + country;
}
if (price != null) {
listItemInfo += ' ' + price;
}
if (currentTime != null) {
listItemInfo += ' ' + currentTime;
}
if (contact != null) {
listItemInfo += ' ' + contact;
}
// add a separator
listItemInfo += '\n--------------------------------';
};
listItemInfo = listItemInfo.replace(/�/g, '£'); // clean special unknown charac and replace with £ sign
console.log('retrieving ' + count + ' events...' + listItemInfo.toString());
createWordDoc(listItemInfo); // create the word document with the generated string
}
function onExportQueryFailed(sender, args) {
alert('Export query failed.' + args.get_message() + '\n' + args.get_stackTrace());
}
// Export the events list to a word document (will be called in the exportEvents function)
function createWordDoc(text) {
var link, blob, url;
blob = new Blob(['\ufeff', text], {
type: 'application/msword'
});
/* url = URL.createObjectURL(blob);
link = document.createElement('A');
link.href = url.replace('https://bauertest.sharepoint.com/sites/NewSandbox/', '');
link.download = 'MCN_Events_report'; //default name without extension
document.body.appendChild(link);
if (navigator.msSaveOrOpenBlob) navigator.msSaveOrOpenBlob(blob, 'MCN_Events_report.docx'); //IE10-11
else link.click(); //otherbrowsers
document.body.removeChild(link); */
saveAs(blob, "MCNEVENTS.doc");
};