1
|
|
|
var spauth = require('node-sp-auth'); |
2
|
|
|
var request = require('request-promise'); |
3
|
|
|
var config = require('./config.json'); |
4
|
|
|
var targetSharePoint = require('./targetSharePoint.json'); |
5
|
|
|
var MyCustomFunctions = require('./custom_functions.js'); |
6
|
|
|
var fs = require('fs'); |
7
|
|
|
|
8
|
|
|
spauth |
9
|
|
|
.getAuth(targetSharePoint.URL, MyCustomFunctions.buildAuthenticationHeader(targetSharePoint.authentication)) |
10
|
|
|
.then(function (data) { |
11
|
|
|
var fsOptions = { |
12
|
|
|
encoding: 'utf8' |
13
|
|
|
}; |
14
|
|
|
var headerOptions = data.headers; |
15
|
|
|
headerOptions['Accept'] = 'application/json;odata=verbose'; |
16
|
|
|
var ListNameArray = []; |
17
|
|
|
request.get(MyCustomFunctions.buildRequestQuery(targetSharePoint.URL, '', 'Lists', headerOptions)).then(function (response) { |
18
|
|
|
var dataObjectLists = response.d.results; |
19
|
|
|
if (Object.keys(dataObjectLists).length > 0) { |
20
|
|
|
var dataListLight = []; |
21
|
|
|
var counter = 0; |
22
|
|
|
dataObjectLists.forEach(function (item) { |
23
|
|
|
dataListLight[counter] = MyCustomFunctions.buildCurrentListAttributeValues(config.SharePoint.MetaDataOutput.Lists, item); |
24
|
|
|
ListNameArray[counter] = item.Title; |
25
|
|
|
counter++; |
26
|
|
|
}); |
27
|
|
|
var wStreamList = fs.createWriteStream(config.General.PathForExtracts + config.General.MetaDataFileName.Lists + '.csv', fsOptions); // initiate MetaData for Lists |
28
|
|
|
wStreamList.write('"' + Object.keys(dataListLight[0]).join('"' + config.General.ListSeparator + '"') + '"\n'); // headers of MetaData for Lists |
29
|
|
|
var wStreamListFields = fs.createWriteStream(config.General.PathForExtracts + config.General.MetaDataFileName.Fields + '.csv', fsOptions); // initiate MetaData for Fields |
30
|
|
|
wStreamListFields.write('"List"' + config.General.ListSeparator + '"' + Object.keys(config.SharePoint.MetaDataOutput.Fields).join('"' + config.General.ListSeparator + '"') + '"\n'); // headers of MetaData for Fields |
31
|
|
|
dataListLight.forEach(function (crtListParameters) { // parse each List |
32
|
|
|
// check current List against configured BlackList and WhiteList besides considering user defined Lists |
33
|
|
|
if (MyCustomFunctions.decideBlackListWhiteList(crtListParameters.Hidden, false, config.SharePoint.Filters.Lists.NotHidden.BlackList, true, config.SharePoint.Filters.Lists.Hidden.WhiteList, crtListParameters.Title)) { |
34
|
|
|
wStreamList.write('"' + Object.keys(crtListParameters).map(function (x) { // records detail of current List |
35
|
|
|
return crtListParameters[x]; |
36
|
|
|
}).join('"' + config.General.ListSeparator + '"') + '"\n'); |
37
|
|
|
// Dynamically detect structure of the list, extracting the Field names and their text to display |
38
|
|
|
request.get(MyCustomFunctions.buildRequestQuery(targetSharePoint.URL, crtListParameters.Title, 'Fields', headerOptions)).then(function (response) { |
39
|
|
|
var dataObject = response.d.results; |
40
|
|
|
if (Object.keys(dataObject).length > 0) { |
41
|
|
|
var fieldAttributes = []; |
42
|
|
|
var counter = 0; |
43
|
|
|
dataObject.forEach(function (item) { |
44
|
|
|
var crtRecordFieldWillBeExtracted = MyCustomFunctions.decideBlackListWhiteList(item.CanBeDeleted, true, config.SharePoint.Filters.Fields.CanBeDeleted.BlackList, false, config.SharePoint.Filters.Fields.CannotBeDeleted.WhiteList, item.InternalName); // check current Field against configured BlackList and WhiteList besides considering user defined Fields |
45
|
|
|
if (config.SharePoint.Filters.Lists.Hidden.WhiteList.indexOf(crtListParameters.Title) > -1) { // for certain Lists all existing fields should be retrieved |
46
|
|
|
crtRecordFieldWillBeExtracted = true; |
47
|
|
|
} |
48
|
|
|
if (crtRecordFieldWillBeExtracted) { |
49
|
|
|
fieldAttributes[item.Title] = { |
50
|
|
|
'Technical Name': item.StaticName, |
51
|
|
|
'Type': item.TypeAsString |
52
|
|
|
}; |
53
|
|
|
counter++; |
54
|
|
|
var crtListField = []; |
55
|
|
|
var counterF = 0; |
56
|
|
|
Object.keys(config.SharePoint.MetaDataOutput.Fields).forEach(function (itemF) { |
57
|
|
|
crtListField[counterF] = item[config.SharePoint.MetaDataOutput.Fields[itemF]]; |
58
|
|
|
counterF++; |
59
|
|
|
}); |
60
|
|
|
wStreamListFields.write('"' + crtListParameters.Title + '"' + config.General.ListSeparator + '"' + crtListField.join('"' + config.General.ListSeparator + '"') + '"\n'); |
61
|
|
|
} |
62
|
|
|
}); |
63
|
|
|
// Get the actual values from current list |
64
|
|
|
request.get(MyCustomFunctions.buildRequestQuery(targetSharePoint.URL, crtListParameters.Title, 'Items', headerOptions, crtListParameters.Records)).then(function (response) { |
65
|
|
|
var wstream = fs.createWriteStream(config.General.PathForExtracts + crtListParameters.Title + '.csv', fsOptions); |
66
|
|
|
wstream.write('"' + Object.keys(fieldAttributes).join('"' + config.General.ListSeparator + '"') + (crtListParameters['Versioning Enabled'] ? '"' + config.General.ListSeparator + '"Version' : '') + '"\n'); // writing headers for records within current list |
67
|
|
|
var dataObjectValues = response.d.results; |
68
|
|
|
if (Object.keys(dataObjectValues).length > 0) { |
69
|
|
|
dataObjectValues.forEach(function (item) { |
70
|
|
|
var crtRecord = []; |
71
|
|
|
var counterF = 0; |
72
|
|
|
Object.keys(fieldAttributes).map(function (itemF) { |
73
|
|
|
switch (fieldAttributes[itemF]['Type']) { |
74
|
|
|
case 'DateTime': |
75
|
|
|
if (item[fieldAttributes[itemF]['Technical Name']] === null) { |
76
|
|
|
crtRecord[counterF] = ''; |
77
|
|
|
} else { |
78
|
|
|
crtRecord[counterF] = item[fieldAttributes[itemF]['Technical Name']].replace('T', ' ').replace('Z', ''); |
79
|
|
|
} |
80
|
|
|
break; |
81
|
|
|
case 'Lookup': |
82
|
|
|
case 'User': |
83
|
|
|
crtRecord[counterF] = item[fieldAttributes[itemF]['Technical Name'] + 'Id']; |
84
|
|
|
break; |
85
|
|
|
default: |
86
|
|
|
crtRecord[counterF] = item[fieldAttributes[itemF]['Technical Name']]; |
87
|
|
|
break; |
88
|
|
|
} |
89
|
|
|
counterF++; |
90
|
|
|
}); |
91
|
|
|
wstream.write('"' + crtRecord.join('"' + config.General.ListSeparator + '"') + (crtListParameters['Versioning Enabled'] ? '"' + config.General.ListSeparator + '"' + item.OData__UIVersionString : '') + '"\n'); // writing current record values |
92
|
|
|
}); |
93
|
|
|
} |
94
|
|
|
wstream.end(); |
95
|
|
|
}); |
96
|
|
|
} |
97
|
|
|
}); |
98
|
|
|
} |
99
|
|
|
}); |
100
|
|
|
wStreamList.end(); |
101
|
|
|
} |
102
|
|
|
}); |
103
|
|
|
}); |
104
|
|
|
|