|
1
|
|
|
var spauth = require('node-sp-auth'); |
|
2
|
|
|
var request = require('request-promise'); |
|
3
|
|
|
var config = require('./config.json'); |
|
4
|
|
|
var MyCustomFunctions = require('./custom_functions.js'); |
|
5
|
|
|
var targetSharePoint = require('./targetSharePoint.json'); |
|
6
|
|
|
var fs = require('fs'); |
|
7
|
|
|
|
|
8
|
|
|
spauth |
|
9
|
|
|
.getAuth(targetSharePoint.URL, { |
|
10
|
|
|
username: targetSharePoint.credentials.username, |
|
11
|
|
|
password: targetSharePoint.credentials.password |
|
12
|
|
|
}) |
|
13
|
|
|
.then(function (data) { |
|
14
|
|
|
var fsOptions = { |
|
15
|
|
|
encoding: 'utf8' |
|
16
|
|
|
}; |
|
17
|
|
|
var headerOptions = data.headers; |
|
18
|
|
|
headerOptions['Accept'] = 'application/json;odata=verbose'; |
|
19
|
|
|
var ListNameArray = []; |
|
20
|
|
|
request.get({ |
|
21
|
|
|
url: targetSharePoint.URL + '_api/web/Lists', |
|
22
|
|
|
headers: headerOptions, |
|
23
|
|
|
json: true |
|
24
|
|
|
}).then(function (response) { |
|
25
|
|
|
var dataObjectLists = response.d.results; |
|
26
|
|
|
if (Object.keys(dataObjectLists).length > 0) { |
|
27
|
|
|
var dataListLight = []; |
|
28
|
|
|
var counter = 0; |
|
29
|
|
|
dataObjectLists.forEach(function (item) { |
|
30
|
|
|
dataListLight[counter] = { |
|
31
|
|
|
'Created': item.Created.replace('T', ' ').replace('Z', ''), |
|
32
|
|
|
'Description': item.Description, |
|
33
|
|
|
'EnableAttachments': item.EnableAttachments, |
|
34
|
|
|
'EnableFolderCreation': item.EnableFolderCreation, |
|
35
|
|
|
'EnableVersioning': item.EnableVersioning, |
|
36
|
|
|
'Hidden': item.Hidden, |
|
37
|
|
|
'Id': item.Id, |
|
38
|
|
|
'IsPrivate': item.IsPrivate, |
|
39
|
|
|
'ItemCount': item.ItemCount, |
|
40
|
|
|
'LastItemDeletedDate': item.LastItemDeletedDate.replace('T', ' ').replace('Z', ''), |
|
41
|
|
|
'LastItemModifiedDate': item.LastItemModifiedDate.replace('T', ' ').replace('Z', ''), |
|
42
|
|
|
'LastItemUserModifiedDate': item.LastItemUserModifiedDate.replace('T', ' ').replace('Z', ''), |
|
43
|
|
|
'MajorVersionLimit': item.MajorVersionLimit, |
|
44
|
|
|
'NoCrawl': item.NoCrawl, |
|
45
|
|
|
'ParserDisabled': item.ParserDisabled, |
|
46
|
|
|
'Title': item.Title |
|
47
|
|
|
}; |
|
48
|
|
|
ListNameArray[counter] = item.Title; |
|
49
|
|
|
counter++; |
|
50
|
|
|
}); |
|
51
|
|
|
var wStreamList = fs.createWriteStream(config.General.PathForExtracts + config.General.MetaDataFileName.Lists + '.csv', fsOptions); |
|
52
|
|
|
var wStreamListFields = fs.createWriteStream(config.General.PathForExtracts + config.General.MetaDataFileName.Fields + '.csv', fsOptions); |
|
53
|
|
|
var inMetaDataFields = config.SharePoint.MetaDataOutput.Fields; |
|
54
|
|
|
wStreamListFields.write('"' + Object.keys(inMetaDataFields).join('"' + config.General.ListSeparator + '"') + '"\n'); |
|
55
|
|
|
wStreamList.write('"' + Object.keys(dataListLight[0]).join('"' + config.General.ListSeparator + '"') + '"\n'); |
|
56
|
|
|
dataListLight.forEach(function (crtListParameters) { |
|
57
|
|
|
var crtListName = crtListParameters.Title; |
|
58
|
|
|
var myFunctions = new MyCustomFunctions(); |
|
59
|
|
|
var crtListWillBeExtracted = myFunctions.decideBlackListWhiteList(crtListParameters.Hidden, false, config.SharePoint.Filters.Lists.NotHidden.BlackList, true, config.SharePoint.Filters.Lists.Hidden.WhiteList, crtListName); |
|
60
|
|
|
if (crtListWillBeExtracted) { |
|
61
|
|
|
wStreamList.write('"' + Object.keys(crtListParameters).map(function (x) { |
|
62
|
|
|
return crtListParameters[x]; |
|
63
|
|
|
}).join('"' + config.General.ListSeparator + '"') + '"\n'); |
|
64
|
|
|
// Dynamically detect structure of the list, extracting the Field names and their text to display |
|
65
|
|
|
request.get({ |
|
66
|
|
|
url: targetSharePoint.URL + '_api/web/lists/GetByTitle(\'' + crtListName + '\')/Fields', |
|
67
|
|
|
headers: headerOptions, |
|
68
|
|
|
json: true |
|
69
|
|
|
}).then(function (response) { |
|
70
|
|
|
var dataObject = response.d.results; |
|
71
|
|
|
if (Object.keys(dataObject).length > 0) { |
|
72
|
|
|
var headersArray = []; |
|
73
|
|
|
var fieldsArray = []; |
|
74
|
|
|
var fieldsTypeArray = []; |
|
75
|
|
|
var counter = 0; |
|
76
|
|
|
dataObject.forEach(function (item) { |
|
77
|
|
|
var crtRecordFieldWillBeExtracted = myFunctions.decideBlackListWhiteList(item.CanBeDeleted, true, config.SharePoint.Filters.Fields.CanBeDeleted.BlackList, false, config.SharePoint.Filters.Fields.CannotBeDeleted.WhiteList, item.InternalName); |
|
78
|
|
|
// for certain Lists all existing fields should be retrieved |
|
79
|
|
|
if (config.SharePoint.Filters.Lists.Hidden.WhiteList.indexOf(crtListName) > -1) { |
|
80
|
|
|
crtRecordFieldWillBeExtracted = true; |
|
81
|
|
|
} |
|
82
|
|
|
if (crtRecordFieldWillBeExtracted) { |
|
83
|
|
|
headersArray[counter] = item.Title; |
|
84
|
|
|
fieldsArray[counter] = item.StaticName; |
|
85
|
|
|
fieldsTypeArray[counter] = item.TypeAsString; |
|
86
|
|
|
counter++; |
|
87
|
|
|
var crtListField = []; |
|
88
|
|
|
var counterF = 0 |
|
89
|
|
|
Object.keys(inMetaDataFields).forEach(function (itemF) { |
|
90
|
|
|
crtListField[counterF] = item[inMetaDataFields[itemF]]; |
|
91
|
|
|
counterF++; |
|
92
|
|
|
}); |
|
93
|
|
|
wStreamListFields.write('"' + crtListName + '"' + config.General.ListSeparator + '"' + crtListField.join('"' + config.General.ListSeparator + '"') + '"\n'); |
|
94
|
|
|
} |
|
95
|
|
|
}); |
|
96
|
|
|
// Get the actual values from current list |
|
97
|
|
|
request.get({ |
|
98
|
|
|
url: targetSharePoint.URL + '_api/web/lists/GetByTitle(\'' + crtListName + '\')/Items', |
|
99
|
|
|
headers: headerOptions, |
|
100
|
|
|
json: true |
|
101
|
|
|
}).then(function (response) { |
|
102
|
|
|
var fieldsLength = fieldsArray.length; |
|
103
|
|
|
// output to file only if detectable fields are in scope |
|
104
|
|
|
if (fieldsLength > 0) { |
|
105
|
|
|
var wstream = fs.createWriteStream('./results/' + crtListName + '.csv', fsOptions); |
|
106
|
|
|
if (crtListParameters.EnableVersioning) { |
|
107
|
|
|
headersArray.push('Version'); |
|
108
|
|
|
} |
|
109
|
|
|
// writing headers for records within current list |
|
110
|
|
|
wstream.write('"' + headersArray.join('"' + config.General.ListSeparator + '"') + '"\n'); |
|
111
|
|
|
var dataObjectValues = response.d.results; |
|
112
|
|
|
if (Object.keys(dataObjectValues).length > 0) { |
|
113
|
|
|
dataObjectValues.forEach(function (item) { |
|
114
|
|
|
var crtRecord = []; |
|
115
|
|
|
for (var counterF = 0; counterF < fieldsLength; counterF++) { |
|
116
|
|
|
switch (fieldsTypeArray[counterF]) { |
|
117
|
|
|
case 'DateTime': |
|
118
|
|
|
crtRecord[counterF] = item[fieldsArray[counterF]].replace('T', ' ').replace('Z', ''); |
|
119
|
|
|
break; |
|
120
|
|
|
case 'Lookup': |
|
121
|
|
|
case 'User': |
|
122
|
|
|
crtRecord[counterF] = item[fieldsArray[counterF] + 'Id']; |
|
123
|
|
|
break; |
|
124
|
|
|
default: |
|
125
|
|
|
crtRecord[counterF] = item[fieldsArray[counterF]]; |
|
126
|
|
|
break; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
if (crtListParameters.EnableVersioning) { |
|
130
|
|
|
crtRecord[counterF] = item.OData__UIVersionString; |
|
131
|
|
|
} |
|
132
|
|
|
// writing current record values |
|
133
|
|
|
wstream.write('"' + crtRecord.join('"' + config.General.ListSeparator + '"') + '"\n'); |
|
134
|
|
|
}); |
|
135
|
|
|
} |
|
136
|
|
|
wstream.end(function () { |
|
137
|
|
|
if (config.General.Feedback.FileCompletion.OtherLists) { |
|
138
|
|
|
console.log(crtListName + '.csv has been completed!\n' + (config.General.Feedback.ContentAsJSON.OtherLists ? JSON.stringify(dataObjectValues) : '')); |
|
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
}); |
|
141
|
|
|
} |
|
142
|
|
|
}); |
|
143
|
|
|
} |
|
144
|
|
|
}); |
|
145
|
|
|
} |
|
146
|
|
|
}); |
|
147
|
|
|
wStreamList.end(function () { |
|
148
|
|
|
if (config.General.Feedback.FileCompletion.ListOfLists) { |
|
149
|
|
|
console.log(config.General.MetaDataFileName.Lists + '.csv has been completed!\n' + (config.General.Feedback.ContentAsJSON.ListOfLists ? JSON.stringify(dataListLight) : '')); |
|
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
}); |
|
152
|
|
|
} |
|
153
|
|
|
}); |
|
154
|
|
|
}); |
|
155
|
|
|
|