forked from lstak/Backbone.SharePoint
-
Notifications
You must be signed in to change notification settings - Fork 2
/
backbone-sharepoint.soap.js
301 lines (260 loc) · 10.5 KB
/
backbone-sharepoint.soap.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
(function (Backbone, _, $) {
// SharePoint ListData service
var LIST_SERVICE = '_vti_bin/Lists.asmx',
url,
getChanges,
getSuccessMethod,
SoapClient,
methodMap = {
'create': { name: 'UpdateListItems', updateCmd: 'New' },
'update': { name: 'UpdateListItems', updateCmd: 'Update' },
'delete': { name: 'UpdateListItems', updateCmd: 'Delete' },
'read': { name: 'GetListItems' }
},
templates = {
commandTemplate:
'<?xml version="1.0" encoding="utf-8"?>' +
'<soap:Envelope ' +
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
' xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +
' xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
'<soap:Body>' +
'<<%= method %> xmlns="http://schemas.microsoft.com/sharepoint/soap/">' +
'<%= params %>' +
'</<%= method %>>' +
'</soap:Body>' +
'</soap:Envelope>',
updateItemTemplate:
'<Batch OnError="Continue">' +
'<Method ID="1" Cmd="<%= command %>">' +
'<% _.each(fields, function(field, index) { %>' +
'<Field Name="<%= field.name %>"><%= field.value %></Field>' +
'<% }); %>' +
'</Method>' +
'</Batch>',
getItemById:
'<Query><Where><Eq><FieldRef Name="ID" /><Value Type="Counter">' +
'<%= id %>' +
'</Value></Eq></Where></Query>'
},
compiledTemplates = {};
// calculate url based on site
url = function (options) {
var site = options.site,
// remove leading and trailing forward slashes from the site path
path = site.replace(/^\/+|\/+$/g, ''),
url = (path ? '/' + path : '') + '/' + LIST_SERVICE;
return url;
};
getChanges = function (model, method) {
if (model.isNew()) {
return _.map(model.attributes, function(value, key) {
return { name: key, value: value };
});
}
var requiredFields = [{ name: 'ID', value: model.get('ID') }];
var changedFields = _.map(model.changedAttributes(), function (value, key) {
return { name: key, value: value };
});
// it's required to pass FileRef to delete items from document library
if (method === 'delete') {
var fileRef = model.get('FileRef');
if (fileRef) {
var tokens = fileRef.split(';#');
var fileRefValue = tokens.length > 1 ? tokens[1] : fileRef;
requiredFields.push({ name: 'FileRef', value: fileRefValue });
}
}
return _.flatten([requiredFields, changedFields]);
};
SoapClient = {
template: function( key) {
if (compiledTemplates[key])
return compiledTemplates[key];
// copy templateSettings in case someone modified them
var tmplSettings = _.templateSettings;
_.templateSettings = {
evaluate : /<%([\s\S]+?)%>/g,
interpolate : /<%=([\s\S]+?)%>/g,
escape : /<%-([\s\S]+?)%>/g
};
var templateString = templates[key];
if (templateString) {
compiledTemplates[key] = _.template(templateString);
}
// restore templateSettings
_.templateSettings = tmplSettings;
return compiledTemplates[key];
},
serializeParams: function (params) {
var key, value, xml = '';
params = params || {};
if (typeof params === 'string') {
return params;
}
for (key in params) {
value = params[key];
if (typeof value !== 'undefined' && value !== null) {
xml += '<' + key + '>' + value + '</' + key + '>';
}
}
return xml;
},
// string that contains information for the next page request
// if there is no ListItemCollectionPositionNext - it means that we reached last page
parsePagingInfo: function (data) {
var rootnode,
licp;
rootnode = $(data).find('*').filter(function () {
return this.nodeName === 'rs:data';
})[0];
if (!rootnode)
return null;
licp = _.findWhere(rootnode.attributes, { name: 'ListItemCollectionPositionNext' });
return licp ? licp.value : null;
},
// results will an Array of javascript objects.
parseResultsXml: function (data) {
var nodes, name,
NODE_ELEMENT = 1,
results = [], result;
nodes = $(data).find('*').filter(function () {
return this.nodeName === 'z:row';
});
if (nodes.length === 0)
return null;
nodes.each(function () {
// skip text nodes
if (this.nodeType === NODE_ELEMENT) {
result = {};
_.each(this.attributes, function(attribute) {
name = attribute.name.replace('ows_', '');
result[name] = attribute.value;
});
// only use the result if it is not hidden
if ((result.Hidden || '').toUpperCase() !== 'TRUE') {
results.push(result);
}
}
});
return results;
},
call: function (config) {
config = config || {};
var method = methodMap[config.method];
// prepare the Ajax request
var request = {
type: 'POST',
url: url({ site: config.site }),
contentType: 'text/xml',
dataType: 'xml',
data: this.template('commandTemplate')({
method: method.name,
params: this.serializeParams(config.params)
}),
processData: false,
success: config.options.success,
headers: {
'SOAPAction': 'http://schemas.microsoft.com/sharepoint/soap/' + method.name
},
error: config.options.error
};
// Make the request.
return $.ajax(request);
}
};
getSuccessMethod = function (model, options, getDataCallback) {
var oldSuccess = options.success,
success = function(resp, status, xhr) {
var data = null;
if (getDataCallback && _.isFunction(getDataCallback))
data = getDataCallback(resp, status, xhr);
if (oldSuccess) {
if (Backbone.VERSION === '0.9.9' || Backbone.VERSION === '0.9.10') {
oldSuccess(model, data, options);
} else {
oldSuccess(data, status, xhr);
}
}
};
return success;
};
Backbone.SP = Backbone.SP || {};
Backbone.SP.SoapItem = Backbone.Model.extend({
idAttribute: 'ID',
url: function () {
return url({ site: this.site });
},
sync: function (method, model, options) {
options = options ? _.clone(options) : {};
options.params = options.params || {};
if (method === 'read') {
// viewName is mandatory parameter
options.params.viewName = model.view || '';
options.params.query = SoapClient.template('getItemById')(this);
options.params.queryOptions = '<QueryOptions><ViewAttributes Scope="Recursive" /></QueryOptions>';
}
// otherwise we have create/update/delete
else {
var fields = getChanges(model, method);
var updates = SoapClient.template('updateItemTemplate')({
fields: fields,
command: methodMap[method].updateCmd
});
options.params.updates = updates;
}
options.success = getSuccessMethod(model, options, function (resp, status, xhr) {
var data = null;
if (method === 'read' || method === 'create') {
data = SoapClient.parseResultsXml(resp);
// get first item if we fetch single model
data = data.length > 0 ? data[0] : data;
}
return data;
});
SoapClient.call({
site: model.site,
method: method,
options: options,
params: _.extend({
listName: model.list
}, options.params)
});
}
});
Backbone.SP.SoapList = Backbone.Collection.extend({
url: function () {
if (this.model && this.model.prototype.site && this.model.prototype.url) {
return this.model.prototype.url();
}
// otherwise use site and list settings of this collection
return url({ site: this.site });
},
pagingInfo: null,
sync: function (method, collection, options) {
options = options ? _.clone(options) : {};
options.params = options.params || {};
if (method === 'read') {
// viewName is mandatory parameter
options.params.viewName = collection.view || '';
}
options.success = getSuccessMethod(collection, options, function (resp, status, xhr) {
var data = null;
options.params = options.params || {};
if (method === 'read') {
data = SoapClient.parseResultsXml(resp);
collection.pagingInfo = SoapClient.parsePagingInfo(resp);
}
return data;
});
SoapClient.call({
site: collection.site || collection.model.prototype.site,
method: method,
options: options,
params: _.extend({
listName: collection.list || collection.model.prototype.list
}, options.params)
});
}
});
} (Backbone, _, jQuery));