-
Notifications
You must be signed in to change notification settings - Fork 1
/
examples.js
executable file
·138 lines (113 loc) · 4.02 KB
/
examples.js
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
var SharePoint = require('./lib/SharePoint');
var config = require('./config');
var fs = require('fs');
var uuid = require('node-uuid');
var parse = require('jsonml').parse;
var buildSharePoint = function(){
//our sharepoint config
var sp = new SharePoint({
site: config.site,
user: config.user,
pass: config.password
});
return sp;
}
/*
The following shows how to get all the lists from the sharepoint site and print the title to the command prompt
*/
var getAllLists = function(){
var sp = buildSharePoint();
sp.getAllLists(['Title', 'ItemCount', 'Id']).then(function(result){
console.log(result);
}, function(err){
console.log(err);
});
};
/*
The following shows how to get information for a list. Returns all information available for the list.
*/
var getListInfo = function(list){
var sp = buildSharePoint();
sp.getListInfo(list).then(function(result){
console.log(result.ListItemEntityTypeFullName);
}, function(err){
console.log(err);
});
};
/*
The following shows how to get all items in a list. Returns all information available for each item.
*/
var getListItems = function(list){
var sp = buildSharePoint();
sp.getListItems(list]).then(function(result){
console.log(result);
}, function(err){
console.log(err);
});
};
/*
The following shows how to get all the content types from a specifiec list
*/
var getContentTypes = function(list){
var sp = buildSharePoint();
sp.getContentTypes(list, ['Name', 'StringId']).then(function(result){
console.log(result);
}, function(err){
console.log(err);
});
}
/*
The following shows how to get a content types columns by getting the content type id using a list name
*/
var getContentTypeColumns = function(list, id){
var sp = buildSharePoint();
sp.getContentTypeColumns(list, id).then(function(result){
console.log(result);
}, function(err){
console.log(err);
});
}
/*
The following example shows how to upload a file to a sharepoint document library with metadata attached.
The process must be done backwards by uploading the file first then retrieving it's item properties and
merging the new item metadata with the current item.
***Gotchas***
You must first get the ListItemEntityTypeFullName and add it to your new items __metadata type field. This tells
SharePoint what content type to use.
When creating the item metadata include what fields you want to modify outside of the __metadata property.
All fields with a space in the name need to be reformatted to use _x0020_ for the space.
Eq 'Some Column' = Some_x0020_Column
*/
var addItemWithFile = function () {
var sp = buildSharePoint();
//file to upload
var file = 'path to your file';
//list title
var list = 'my list title';
//We start by reading our file stream
fs.readFile(file, function (err, data) {
//We get the ListItemEntityTypeFullName for the list - All methods return a promise
sp.getListInfo(list).then(function (result) {
var entityType = result.ListItemEntityTypeFullName;
//create our new item metadata
var item = {
__metadata: {
type: entityType
},//add column information below
};
//pass the file stream into the uploadDocumentAttach method
//uploadDocumentAttach(list, fileName, stream, overwrite, newItem) - If you want to rename the file,
//enter the new name for the fileName arg
sp.uploadDocumentAttach(list, file, data, true, item).then(function (result) {
//do something with the results
console.log(result);
}, function (err) {
//do something with the err
console.log(err);
});
}, function (err) {
//do something with the err
console.log(err);
});
});
};