-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlists.js
138 lines (126 loc) · 5.24 KB
/
lists.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
/* list operations with rest api */
function getSiteListNames(onSuccess, onError, async) {
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists",
async: async ? async : true,
method: 'GET',
headers: { 'Accept': 'application/json;odata=verbose' },
success: function (data) { onSuccess(data.d.results); },
error: function (data) { onError(data); }
});
}
function getListItemById(listTitle, itemId, select, onSuccess, onError, async) {
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items(" + itemId + ")" + (select.trim() != '' ? ('?$select=' + select) : ''),
async: async ? async : true,
method: 'GET',
headers: { 'Accept': 'application/json;odata=verbose' },
success: function (data) { onSuccess(data.d); },
error: function (data) { onError(data); }
});
}
function getListItemsByFilter(listTitle, select, filter, top, orderby, onSuccess, onError, async) {
var slc = select.trim() != '' ? ('$select=' + select) : '';
var flt = filter.trim() != '' ? ('&$filter=' + filter) : '';
var tp = top.trim() != '' ? ('&$top=' + top) : '';
var ord = orderby.trim() != '' ? ('&$orderby=' + orderby) : ''; // example: Employee asc or Employee desc
var f = slc + flt + tp + ord;
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items" + (f != '' ? ('?' + f) : ''),
async: async ? async : true,
method: 'GET',
headers: { 'Accept': 'application/json;odata=verbose' },
success: function (data) { onSuccess(data.d.results); },
error: function (data) { onError(data); }
});
}
function getItemTypeForListName(listName) {
return "SP.Data." + listName.charAt(0).toUpperCase() + listName.slice(1) + "ListItem";
}
function createListItem(listTitle, newdata, onSuccess, onError, async) {
// sample of new data
// var newdata = {
// __metadata: {
// // also for getting 'type' you can use getItemTypeForListName(listName)
// 'type': 'SP.Data.[ListName]ListItem', // [ListName] is for list name (first char must be capital)
// },
// Title: 'title',
// x: 45 + '', // don't forget to convert numbers to string for text columns (it may cause 400 error)
// y: 90,
// };
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/lists/getbytitle('" + listTitle + "')/items",
type: "POST",
async: async ? async : true,
contentType: "application/json;odata=verbose",
data: JSON.stringify(newdata),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data) { onSuccess(data); },
error: function (data) { onError(data); }
});
}
function updateListItemById(listTitle, itemId, newdata, onSuccess, onError, async) {
// var newdata = {
// __metadata: {
// 'type': 'SP.Data.[ListName]Item', // [ListName] is for list name (first char must be capital)
// },
// Title: "new title",
// x: 'x',
// y: 65 + '', // don't forget to convert numbers to string for text columns (it may cause 400 error)
// };
$.ajax({
type: 'POST',
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items(" + itemId + ")",
async: async ? async : true,
method: 'PATCH',
data: JSON.stringify(newdata),
contentType: 'application/json;odata=verbose',
headers: {
'Accept': 'application/json;odata=verbose',
'X-RequestDigest': $('#__REQUESTDIGEST').val(),
'X-HTTP-Method': 'MERGE',
'If-Match': '*'
},
success: function (data) { onSuccess(data); },
error: function (data) { onError(data) }
});
}
function deleteListItemById(listTitle, itemId, onSuccess, onError, async) {
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items(" + itemId + ")",
async: async ? async : true,
type: 'POST',
contentType: 'application/json;odata=verbose',
headers: {
'Accept': 'application/json;odata=verbose',
'X-RequestDigest': $('#__REQUESTDIGEST').val(),
'IF-MATCH': '*',
'X-HTTP-Method': 'DELETE',
},
success: function (data) { onSuccess(data); },
error: function (data) { onError(data); }
});
}
function createFolder(libraryEntityName, folderName, onSuccess, onError) {
$.ajax({
url: `${_spPageContextInfo.webAbsoluteUrl}/_api/web/folders`,
type: 'POST',
data: JSON.stringify({
'__metadata': { 'type': 'SP.Folder' },
'ServerRelativeUrl': `${libraryEntityName}/${folderName}`
}),
headers: {
'accept': 'application/json;odata=verbose',
'content-type': 'application/json;odata=verbose',
'X-RequestDigest': $('#__REQUESTDIGEST').val()
},
success: onSuccess,
error: onError
});
}
// future functions
function createList() {}
function deleteList() {}