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