forked from vadimi/Backbone.SharePoint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone-sharepoint.soap.js
169 lines (128 loc) · 4.67 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
(function (Backbone, _, $) {
// SharePoint ListData service
var LIST_SERVICE = '_vti_bin/Lists.asmx',
url,
SoapClient;
// 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;
};
SoapClient = {
tpl: _.template(
'<?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>'
),
serializeParams: function (params) {
var key, value, xml = '';
params = params || {};
for (key in params) {
value = params[key];
if (value) {
xml += '<' + key + '>';
switch (key) {
case 'viewFields':
// for future use...
break;
default:
xml += params[key];
break;
}
xml += '</' + key + '>';
}
}
return xml;
},
success: function (data, status, xhr, callback) {
var nodes, node, rootnode, name,
NODE_ELEMENT = 1,
attributes, attribute,
results = [], result,
root = 'data',
i, j;
rootnode = data.querySelector(root);
nodes = rootnode.childNodes;
for (i = 0; i < nodes.length; i += 1) {
node = nodes[i];
// skip text nodes
if (node.nodeType === NODE_ELEMENT) {
attributes = node.attributes;
result = {};
for (j = 0; j < attributes.length; j += 1) {
attribute = attributes[j];
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);
}
}
}
// results now contains an Array of javascript objects.
// call the success handler inside Collection.fetch() to process the results.
if (callback) {
callback(results, status, xhr);
}
},
call: function (config) {
var me = this,
request;
config = config || {};
// prepare the Ajax request
request = {
type: 'POST',
url: url({ site: config.site }),
contentType: 'text/xml',
dataType: 'xml',
data: this.tpl({
method: config.method,
params: this.serializeParams(config.params)
}),
processData: false,
success: function (data, status, xhr) {
me.success(data, status, xhr, config.success)
},
error: config.error
};
// Make the request.
return $.ajax(request);
}
};
Backbone.SP = {};
Backbone.SP.Item = Backbone.Model.extend({
// to be implemented...
});
Backbone.SP.List = Backbone.Collection.extend({
url: function () {
// otherwise use site and list settings of this collection
return url({ site: this.site });
},
sync: function (method, collection, options) {
SoapClient.call({
site: collection.site,
service: 'Lists',
method: 'GetListItems',
success: options.success,
error: options.error,
params: {
listName: collection.list,
viewName: collection.view || ''
}
});
}
});
} (Backbone, _, $));