-
Notifications
You must be signed in to change notification settings - Fork 22
/
camljs.js
1294 lines (1294 loc) · 63.6 KB
/
camljs.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var CamlBuilder = (function () {
function CamlBuilder() {
}
/** Generate CAML Query, starting from <Where> tag */
CamlBuilder.prototype.Where = function () {
return CamlBuilder.Internal.createWhere();
};
/** Generate <View> tag for SP.CamlQuery
@param viewFields If omitted, default view fields are requested; otherwise, only values for the fields with the specified internal names are returned.
Specifying view fields is a good practice, as it decreases traffic between server and client. */
CamlBuilder.prototype.View = function (viewFields) {
return CamlBuilder.Internal.createView(viewFields);
};
/** Generate <ViewFields> tag for SPServices */
CamlBuilder.prototype.ViewFields = function (viewFields) {
return CamlBuilder.Internal.createViewFields(viewFields);
};
/** Use for:
1. SPServices CAMLQuery attribute
2. Creating partial expressions
3. In conjunction with Any & All clauses
*/
CamlBuilder.Expression = function () {
return CamlBuilder.Internal.createExpression();
};
CamlBuilder.FromXml = function (xml) {
return CamlBuilder.Internal.createRawQuery(xml);
};
return CamlBuilder;
}());
(function (CamlBuilder) {
var ViewScope;
(function (ViewScope) {
ViewScope[ViewScope["Recursive"] = 0] = "Recursive";
ViewScope[ViewScope["RecursiveAll"] = 1] = "RecursiveAll";
ViewScope[ViewScope["FilesOnly"] = 2] = "FilesOnly";
})(ViewScope = CamlBuilder.ViewScope || (CamlBuilder.ViewScope = {}));
var DateRangesOverlapType;
(function (DateRangesOverlapType) {
/** Returns events for today */
DateRangesOverlapType[DateRangesOverlapType["Now"] = 0] = "Now";
/** Returns events for one day, specified by CalendarDate in QueryOptions */
DateRangesOverlapType[DateRangesOverlapType["Day"] = 1] = "Day";
/** Returns events for one week, specified by CalendarDate in QueryOptions */
DateRangesOverlapType[DateRangesOverlapType["Week"] = 2] = "Week";
/** Returns events for one month, specified by CalendarDate in QueryOptions.
Caution: usually also returns few days from previous and next months */
DateRangesOverlapType[DateRangesOverlapType["Month"] = 3] = "Month";
/** Returns events for one year, specified by CalendarDate in QueryOptions */
DateRangesOverlapType[DateRangesOverlapType["Year"] = 4] = "Year";
})(DateRangesOverlapType = CamlBuilder.DateRangesOverlapType || (CamlBuilder.DateRangesOverlapType = {}));
var Internal = (function () {
function Internal() {
}
Internal.createView = function (viewFields) {
return new ViewInternal().View(viewFields);
};
Internal.createViewFields = function (viewFields) {
return new ViewInternal().CreateViewFields(viewFields);
};
Internal.createWhere = function () {
return new QueryInternal().Where();
};
Internal.createExpression = function () {
return new FieldExpression(new Builder());
};
Internal.createRawQuery = function (xml) {
return new RawQueryInternal(xml);
};
return Internal;
}());
CamlBuilder.Internal = Internal;
var ViewInternal = (function () {
function ViewInternal() {
this.builder = new Builder();
}
/** Adds View element. */
ViewInternal.prototype.View = function (viewFields) {
this.builder.WriteStart("View");
this.builder.unclosedTags++;
if (viewFields && viewFields.length > 0)
this.CreateViewFields(viewFields);
this.joinsManager = new JoinsManager(this.builder, this);
return this;
};
ViewInternal.prototype.CreateViewFields = function (viewFields) {
this.builder.WriteStart("ViewFields");
for (var i = 0; i < viewFields.length; i++) {
this.builder.WriteFieldRef(viewFields[i]);
}
this.builder.WriteEnd();
return this;
};
ViewInternal.prototype.RowLimit = function (limit, paged) {
this.builder.WriteRowLimit(paged || false, limit);
return this;
};
ViewInternal.prototype.Scope = function (scope) {
switch (scope) {
case ViewScope.FilesOnly:
this.builder.SetAttributeToLastElement("View", "Scope", "FilesOnly");
break;
case ViewScope.Recursive:
this.builder.SetAttributeToLastElement("View", "Scope", "Recursive");
break;
case ViewScope.RecursiveAll:
this.builder.SetAttributeToLastElement("View", "Scope", "RecursiveAll");
break;
default:
console.log('Incorrect view scope! Please use values from CamlBuilder.ViewScope enumeration.');
break;
}
return this;
};
ViewInternal.prototype.InnerJoin = function (lookupFieldInternalName, alias) {
return this.joinsManager.Join(lookupFieldInternalName, alias, "INNER");
};
ViewInternal.prototype.LeftJoin = function (lookupFieldInternalName, alias) {
return this.joinsManager.Join(lookupFieldInternalName, alias, "LEFT");
};
/** Select projected field for using in the main Query body
@param remoteFieldAlias By this alias, the field can be used in the main Query body. */
ViewInternal.prototype.Select = function (remoteFieldInternalName, remoteFieldAlias) {
return this.joinsManager.ProjectedField(remoteFieldInternalName, remoteFieldAlias);
};
ViewInternal.prototype.ToString = function () {
if (this.joinsManager != null)
this.joinsManager.Finalize();
return this.builder.Finalize();
};
ViewInternal.prototype.ToCamlQuery = function () {
this.joinsManager.Finalize();
return this.builder.FinalizeToSPQuery();
};
/** Adds Query clause to the View XML. */
ViewInternal.prototype.Query = function () {
this.joinsManager.Finalize();
this.builder.WriteStart("Query");
this.builder.unclosedTags++;
return new QueryInternal(this.builder);
};
return ViewInternal;
}());
/** Represents SharePoint CAML Query element */
var QueryInternal = (function () {
function QueryInternal(builder) {
this.builder = builder || new Builder();
}
/** Adds Where clause to the query, inside you can specify conditions for certain field values. */
QueryInternal.prototype.Where = function () {
this.builder.WriteStart("Where");
this.builder.unclosedTags++;
return new FieldExpression(this.builder);
};
/** Adds GroupBy clause to the query.
@param collapse If true, only information about the groups is retrieved, otherwise items are also retrieved. */
QueryInternal.prototype.GroupBy = function (groupFieldName, collapse) {
this.builder.WriteStartGroupBy(groupFieldName, collapse);
return new GroupedQuery(this.builder);
};
/** Adds OrderBy clause to the query
@param fieldInternalName Internal field of the first field by that the data will be sorted (ascending)
@param override This is only necessary for large lists. DON'T use it unless you know what it is for!
@param useIndexForOrderBy This is only necessary for large lists. DON'T use it unless you know what it is for!
*/
QueryInternal.prototype.OrderBy = function (fieldInternalName, override, useIndexForOrderBy) {
this.builder.WriteStartOrderBy(override, useIndexForOrderBy);
this.builder.WriteFieldRef(fieldInternalName);
return new SortedQuery(this.builder);
};
/** Adds OrderBy clause to the query (using descending order for the first field).
@param fieldInternalName Internal field of the first field by that the data will be sorted (descending)
@param override This is only necessary for large lists. DON'T use it unless you know what it is for!
@param useIndexForOrderBy This is only necessary for large lists. DON'T use it unless you know what it is for!
*/
QueryInternal.prototype.OrderByDesc = function (fieldInternalName, override, useIndexForOrderBy) {
this.builder.WriteStartOrderBy(override, useIndexForOrderBy);
this.builder.WriteFieldRef(fieldInternalName, { Descending: true });
return new SortedQuery(this.builder);
};
QueryInternal.prototype.ToString = function () {
return this.builder.Finalize();
};
QueryInternal.prototype.ToCamlQuery = function () {
return this.builder.FinalizeToSPQuery();
};
return QueryInternal;
}());
var JoinsManager = (function () {
function JoinsManager(builder, viewInternal) {
this.projectedFields = [];
this.joins = [];
this.originalView = viewInternal;
this.builder = builder;
}
JoinsManager.prototype.Finalize = function () {
if (this.joins.length > 0) {
this.builder.WriteStart("Joins");
for (var i = 0; i < this.joins.length; i++) {
var join = this.joins[i];
this.builder.WriteStart("Join", [
{ Name: "Type", Value: join.JoinType },
{ Name: "ListAlias", Value: join.Alias }
]);
this.builder.WriteStart("Eq");
var fieldAttrs = { RefType: "ID" };
if (join.FromList)
fieldAttrs["List"] = join.FromList;
this.builder.WriteFieldRef(join.RefFieldName, fieldAttrs);
this.builder.WriteFieldRef("ID", { List: join.Alias });
this.builder.WriteEnd();
this.builder.WriteEnd();
}
this.builder.WriteEnd();
this.builder.WriteStart("ProjectedFields");
for (var i = 0; i < this.projectedFields.length; i++) {
var projField = this.projectedFields[i];
this.builder.WriteStart("Field", [
{ Name: "ShowField", Value: projField.FieldName },
{ Name: "Type", Value: "Lookup" },
{ Name: "Name", Value: projField.Alias },
{ Name: "List", Value: projField.JoinAlias }
]);
this.builder.WriteEnd();
}
this.builder.WriteEnd();
}
};
JoinsManager.prototype.Join = function (lookupFieldInternalName, alias, joinType, fromList) {
this.joins.push({ RefFieldName: lookupFieldInternalName, Alias: alias, JoinType: joinType, FromList: fromList });
return new Join(this.builder, this);
};
JoinsManager.prototype.ProjectedField = function (remoteFieldInternalName, remoteFieldAlias) {
this.projectedFields.push({ FieldName: remoteFieldInternalName, Alias: remoteFieldAlias, JoinAlias: this.joins[this.joins.length - 1].Alias });
return this.originalView;
};
return JoinsManager;
}());
var Join = (function () {
function Join(builder, joinsManager) {
this.builder = builder;
this.joinsManager = joinsManager;
}
/** Select projected field for using in the main Query body
@param remoteFieldAlias By this alias, the field can be used in the main Query body. */
Join.prototype.Select = function (remoteFieldInternalName, remoteFieldAlias) {
return this.joinsManager.ProjectedField(remoteFieldInternalName, remoteFieldAlias);
};
Join.prototype.InnerJoin = function (lookupFieldInternalName, alias, fromList) {
return this.joinsManager.Join(lookupFieldInternalName, alias, "INNER", fromList);
};
Join.prototype.LeftJoin = function (lookupFieldInternalName, alias, fromList) {
return this.joinsManager.Join(lookupFieldInternalName, alias, "LEFT", fromList);
};
return Join;
}());
var QueryToken = (function () {
function QueryToken(builder, startIndex) {
this.builder = builder;
this.startIndex = startIndex;
}
/** Adds And clause to the query. */
QueryToken.prototype.And = function () {
this.builder.tree.splice(this.startIndex, 0, { Element: "Start", Name: "And" });
this.builder.unclosedTags++;
return new FieldExpression(this.builder);
};
/** Adds Or clause to the query. */
QueryToken.prototype.Or = function () {
this.builder.tree.splice(this.startIndex, 0, { Element: "Start", Name: "Or" });
this.builder.unclosedTags++;
return new FieldExpression(this.builder);
};
/** Adds GroupBy clause to the query.
@param collapse If true, only information about the groups is retrieved, otherwise items are also retrieved. */
QueryToken.prototype.GroupBy = function (groupFieldName, collapse) {
this.builder.WriteStartGroupBy(groupFieldName, collapse);
return new GroupedQuery(this.builder);
};
/** Adds OrderBy clause to the query
@param fieldInternalName Internal field of the first field by that the data will be sorted (ascending)
@param override This is only necessary for large lists. DON'T use it unless you know what it is for!
@param useIndexForOrderBy This is only necessary for large lists. DON'T use it unless you know what it is for!
*/
QueryToken.prototype.OrderBy = function (fieldInternalName, override, useIndexForOrderBy) {
this.builder.WriteStartOrderBy(override, useIndexForOrderBy);
this.builder.WriteFieldRef(fieldInternalName);
return new SortedQuery(this.builder);
};
/** Adds OrderBy clause to the query (using descending order for the first field).
@param fieldInternalName Internal field of the first field by that the data will be sorted (descending)
@param override This is only necessary for large lists. DON'T use it unless you know what it is for!
@param useIndexForOrderBy This is only necessary for large lists. DON'T use it unless you know what it is for!
*/
QueryToken.prototype.OrderByDesc = function (fieldInternalName, override, useIndexForOrderBy) {
this.builder.WriteStartOrderBy(override, useIndexForOrderBy);
this.builder.WriteFieldRef(fieldInternalName, { Descending: true });
return new SortedQuery(this.builder);
};
/** Returns the XML string representing the generated CAML
*/
QueryToken.prototype.ToString = function () {
return this.builder.Finalize();
};
/** Returns SP.CamlQuery object that represents the constructed query
*/
QueryToken.prototype.ToCamlQuery = function () {
return this.builder.FinalizeToSPQuery();
};
return QueryToken;
}());
var ModifyType;
(function (ModifyType) {
ModifyType[ModifyType["Replace"] = 0] = "Replace";
ModifyType[ModifyType["AppendOr"] = 1] = "AppendOr";
ModifyType[ModifyType["AppendAnd"] = 2] = "AppendAnd";
})(ModifyType || (ModifyType = {}));
var RawQueryInternal = (function () {
function RawQueryInternal(xml) {
this.xml = xml;
}
RawQueryInternal.prototype.ReplaceWhere = function () {
return this.modifyWhere(ModifyType.Replace);
};
RawQueryInternal.prototype.ModifyWhere = function () {
return this;
};
RawQueryInternal.prototype.AppendOr = function () {
return this.modifyWhere(ModifyType.AppendOr);
};
RawQueryInternal.prototype.AppendAnd = function () {
return this.modifyWhere(ModifyType.AppendAnd);
};
RawQueryInternal.prototype.modifyWhere = function (modifyType) {
var builder = new Builder();
var xmlDoc = this.getXmlDocument(this.xml);
var whereBuilder = this.parseRecursive(builder, xmlDoc.documentElement, modifyType);
if (whereBuilder == null)
console.log("CamlJs error: cannot find Query tag in provided XML");
builder.WriteStart("Where");
builder.unclosedTags++;
switch (modifyType) {
case ModifyType.Replace:
return new FieldExpression(builder);
case ModifyType.AppendAnd:
var pos = builder.tree.length;
builder.WriteStart("And");
builder.unclosedTags++;
builder.tree = builder.tree.concat(whereBuilder.tree);
return new FieldExpression(builder);
case ModifyType.AppendOr:
var pos = builder.tree.length;
builder.WriteStart("Or");
builder.unclosedTags++;
builder.tree = builder.tree.concat(whereBuilder.tree);
return new FieldExpression(builder);
default:
console.log("CamlJs error: unknown ModifyType " + modifyType);
return null;
}
};
RawQueryInternal.prototype.getXmlDocument = function (xml) {
var xmlDoc;
if (window["DOMParser"]) {
var parser = new DOMParser();
xmlDoc = parser.parseFromString(this.xml, "text/xml");
}
else {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc["async"] = false;
xmlDoc["loadXML"](this.xml);
}
return xmlDoc;
};
RawQueryInternal.prototype.parseRecursive = function (builder, node, modifyType) {
if (node.nodeName == "#text") {
builder.tree.push({ Element: "Raw", Xml: node.nodeValue });
return;
}
var attrs = [];
for (var i = 0, len = node.attributes.length; i < len; i++) {
attrs.push({ Name: node.attributes[i].name, Value: node.attributes[i].value });
}
builder.WriteStart(node.nodeName, attrs);
builder.unclosedTags++;
var found = node.nodeName == "Query" ? new Builder() : null;
for (var i = 0, len = node.childNodes.length; i < len; i++) {
if (node.nodeName == "Query" && node.childNodes[i].nodeName == "Where") {
var whereBuilder = new Builder();
var whereNode = node.childNodes[i];
for (var w = 0, wlen = whereNode.childNodes.length; w < wlen; w++) {
this.parseRecursive(whereBuilder, whereNode.childNodes[w], modifyType);
}
found = whereBuilder;
continue;
}
var result = this.parseRecursive(builder, node.childNodes[i], modifyType);
if (found == null)
found = result;
}
if (!found) {
builder.unclosedTags--;
builder.WriteEnd();
}
return found;
};
return RawQueryInternal;
}());
var FieldExpression = (function () {
function FieldExpression(builder) {
this.builder = builder;
}
/** Specifies that a condition will be tested against the field with the specified internal name, and the type of this field is Text */
FieldExpression.prototype.TextField = function (internalName) {
return new FieldExpressionToken(this.builder, internalName, "Text");
};
/** Specifies that a condition will be tested against the field with the specified internal name, and the type of this field is Choice */
FieldExpression.prototype.ChoiceField = function (internalName) {
return new FieldExpressionToken(this.builder, internalName, "Choice");
};
/** Specifies that a condition will be tested against the field with the specified internal name, and the type of this field is Computed */
FieldExpression.prototype.ComputedField = function (internalName) {
return new FieldExpressionToken(this.builder, internalName, "Computed");
};
;
/** Specifies that a condition will be tested against the field with the specified internal name, and the type of this field is Boolean */
FieldExpression.prototype.BooleanField = function (internalName) {
return new FieldExpressionToken(this.builder, internalName, "Integer");
};
/** Specifies that a condition will be tested against the field with the specified internal name, and the type of this field is URL */
FieldExpression.prototype.UrlField = function (internalName) {
return new FieldExpressionToken(this.builder, internalName, "URL");
};
/** Specifies that a condition will be tested against the field with the specified internal name, and the type of this field is Number */
FieldExpression.prototype.NumberField = function (internalName) {
return new FieldExpressionToken(this.builder, internalName, "Number");
};
/** Specifies that a condition will be tested against the field with the specified internal name, and the type of this field is Integer */
FieldExpression.prototype.IntegerField = function (internalName) {
return new FieldExpressionToken(this.builder, internalName, "Integer");
};
/** Specifies that a condition will be tested against the field with the specified internal name, and the type of this field is Counter (usually ID field) */
FieldExpression.prototype.CounterField = function (internalName) {
return new FieldExpressionToken(this.builder, internalName, "Counter");
};
/** Specifies that a condition will be tested against the field with the specified internal name, and the type of this field is User */
FieldExpression.prototype.UserField = function (internalName) {
return new UserFieldExpression(this.builder, internalName);
};
/** Specifies that a condition will be tested against the field with the specified internal name, and the type of this field is Lookup */
FieldExpression.prototype.LookupField = function (internalName) {
return new LookupFieldExpression(this.builder, internalName);
};
/** Specifies that a condition will be tested against the field with the specified internal name, and the type of this field is LookupMulti */
FieldExpression.prototype.LookupMultiField = function (internalName) {
return new LookupOrUserMultiFieldExpression(this.builder, internalName, FieldMultiExpressionType.LookupMulti);
};
/** Specifies that a condition will be tested against the field with the specified internal name, and the type of this field is UserMulti */
FieldExpression.prototype.UserMultiField = function (internalName) {
return new LookupOrUserMultiFieldExpression(this.builder, internalName, FieldMultiExpressionType.UserMulti);
};
/** Specifies that a condition will be tested against the field with the specified internal name, and the type of this field is Date */
FieldExpression.prototype.DateField = function (internalName) {
return new FieldExpressionToken(this.builder, internalName, "Date");
};
/** Specifies that a condition will be tested against the field with the specified internal name, and the type of this field is DateTime */
FieldExpression.prototype.DateTimeField = function (internalName) {
return new FieldExpressionToken(this.builder, internalName, "DateTime");
};
/** Specifies that a condition will be tested against the field with the specified internal name, and the type of this field is ModStat (moderation status) */
FieldExpression.prototype.ModStatField = function (internalName) {
return new ModStatFieldExpression(this.builder, internalName);
};
/** Used in queries for retrieving recurring calendar events.
@param overlapType Defines type of overlap: return all events for a day, for a week, for a month or for a year
@param calendarDate Defines date that will be used for determining events for which exactly day/week/month/year will be returned.
This value is ignored for overlapType=Now, but for the other overlap types it is mandatory.
This value will cause generation of QueryOptions/CalendarDate element.
@param eventDateField Internal name of "Start Time" field (default: "EventDate" - all OOTB Calendar lists use this name)
@param endDateField Internal name of "End Time" field (default: "EndDate" - all OOTB Calendar lists use this name)
@param recurrenceIDField Internal name of "Recurrence ID" field (default: "RecurrenceID" - all OOTB Calendar lists use this name)
*/
FieldExpression.prototype.DateRangesOverlap = function (overlapType, calendarDate, eventDateField, endDateField, recurrenceIDField) {
var pos = this.builder.tree.length;
this.builder.WriteStart("DateRangesOverlap");
this.builder.WriteFieldRef(eventDateField || "EventDate");
this.builder.WriteFieldRef(endDateField || "EndDate");
this.builder.WriteFieldRef(recurrenceIDField || "RecurrenceID");
var value;
switch (overlapType) {
case DateRangesOverlapType.Now:
value = CamlValues.Now;
break;
case DateRangesOverlapType.Day:
value = CamlValues.Today;
break;
case DateRangesOverlapType.Week:
value = "{Week}";
break;
case DateRangesOverlapType.Month:
value = "{Month}";
break;
case DateRangesOverlapType.Year:
value = "{Year}";
break;
}
this.builder.WriteValueElement("Date", value);
this.builder.WriteEnd();
// TODO: write CalendarDate to QueryOptions
return new QueryToken(this.builder, pos);
};
/** Adds And clauses to the query. Use for creating bracket-expressions in conjuction with CamlBuilder.Expression(). */
FieldExpression.prototype.All = function () {
var conditions = [];
for (var _i = 0; _i < arguments.length; _i++) {
conditions[_i] = arguments[_i];
}
var pos = this.builder.tree.length;
if (conditions.length == 1 && conditions[0] instanceof Array)
conditions = conditions[0];
var builders = [];
for (var i = 0; i < conditions.length; i++)
builders.push(conditions[i]["builder"]);
this.builder.WriteConditions(builders, "And");
return new QueryToken(this.builder, pos);
};
/** Adds Or clauses to the query. Use for creating bracket-expressions in conjuction with CamlBuilder.Expression(). */
FieldExpression.prototype.Any = function () {
var conditions = [];
for (var _i = 0; _i < arguments.length; _i++) {
conditions[_i] = arguments[_i];
}
var pos = this.builder.tree.length;
if (conditions.length == 1 && conditions[0] instanceof Array)
conditions = conditions[0];
var builders = [];
for (var i = 0; i < conditions.length; i++)
builders.push(conditions[i]["builder"]);
this.builder.WriteConditions(builders, "Or");
return new QueryToken(this.builder, pos);
};
return FieldExpression;
}());
var FieldMultiExpressionType;
(function (FieldMultiExpressionType) {
FieldMultiExpressionType[FieldMultiExpressionType["UserMulti"] = 0] = "UserMulti";
FieldMultiExpressionType[FieldMultiExpressionType["LookupMulti"] = 1] = "LookupMulti";
})(FieldMultiExpressionType || (FieldMultiExpressionType = {}));
var LookupOrUserMultiFieldExpression = (function () {
function LookupOrUserMultiFieldExpression(builder, name, type) {
this.builder = builder;
this.name = name;
this.type = type;
if (this.type == FieldMultiExpressionType.UserMulti)
this.typeAsString = "UserMulti";
else
this.typeAsString = "LookupMulti";
}
LookupOrUserMultiFieldExpression.prototype.IncludesSuchItemThat = function () {
if (this.type == FieldMultiExpressionType.LookupMulti)
return new LookupFieldExpression(this.builder, this.name);
else
return new UserFieldExpression(this.builder, this.name);
};
LookupOrUserMultiFieldExpression.prototype.IsNull = function () {
return new FieldExpressionToken(this.builder, this.name, this.typeAsString, false).IsNull();
};
LookupOrUserMultiFieldExpression.prototype.IsNotNull = function () {
return new FieldExpressionToken(this.builder, this.name, this.typeAsString, false).IsNotNull();
};
LookupOrUserMultiFieldExpression.prototype.Includes = function (value) {
return new FieldExpressionToken(this.builder, this.name, this.typeAsString, false).EqualTo(value);
};
LookupOrUserMultiFieldExpression.prototype.NotIncludes = function (value) {
return new FieldExpressionToken(this.builder, this.name, this.typeAsString, false).NotEqualTo(value);
};
LookupOrUserMultiFieldExpression.prototype.EqualTo = function (value) {
return new FieldExpressionToken(this.builder, this.name, this.typeAsString, false).EqualTo(value);
};
LookupOrUserMultiFieldExpression.prototype.NotEqualTo = function (value) {
return new FieldExpressionToken(this.builder, this.name, this.typeAsString, false).NotEqualTo(value);
};
return LookupOrUserMultiFieldExpression;
}());
var LookupFieldExpression = (function () {
function LookupFieldExpression(builder, name) {
this.builder = builder;
this.name = name;
}
LookupFieldExpression.prototype.Id = function () {
return new FieldExpressionToken(this.builder, this.name, "Integer", true);
};
LookupFieldExpression.prototype.Value = function () {
return new FieldExpressionToken(this.builder, this.name, "Lookup");
};
LookupFieldExpression.prototype.ValueAsText = function () {
return new FieldExpressionToken(this.builder, this.name, "Text");
};
LookupFieldExpression.prototype.ValueAsNumber = function () {
return new FieldExpressionToken(this.builder, this.name, "Number");
};
LookupFieldExpression.prototype.ValueAsDateTime = function () {
return new FieldExpressionToken(this.builder, this.name, "DateTime");
};
LookupFieldExpression.prototype.ValueAsDate = function () {
return new FieldExpressionToken(this.builder, this.name, "Date");
};
LookupFieldExpression.prototype.ValueAsBoolean = function () {
return new FieldExpressionToken(this.builder, this.name, "Integer");
};
return LookupFieldExpression;
}());
var UserFieldExpression = (function () {
function UserFieldExpression(builder, name) {
var self = this;
this.builder = builder;
this.name = name;
this.startIndex = builder.tree.length;
this.Membership = {
/** DEPRECATED. Please use UserField(...).IsInCurrentUserGroups() instead */
CurrentUserGroups: function () {
return self.IsInCurrentUserGroups();
},
/** DEPRECATED. Please use UserField(...).IsInSPGroup() instead */
SPGroup: function (groupId) {
return self.IsInSPGroup(groupId);
},
/** DEPRECATED. Please use UserField(...).IsInSPWeb* methods instead */
SPWeb: {
/** DEPRECATED. Please use UserField(...).IsInSPWebAllUsers() instead */
AllUsers: function () {
return self.IsInSPWebAllUsers();
},
/** DEPRECATED. Please use UserField(...).IsInSPWebUsers() instead */
Users: function () {
return self.IsInSPWebUsers();
},
/** DEPRECATED. Please use UserField(...).IsInSPWebGroups() instead */
Groups: function () {
return self.IsInSPWebGroups();
}
}
};
}
UserFieldExpression.prototype.Id = function () {
return new FieldExpressionToken(this.builder, this.name, "Integer", true);
};
UserFieldExpression.prototype.ValueAsText = function () {
return new FieldExpressionToken(this.builder, this.name, "Text");
};
UserFieldExpression.prototype.EqualToCurrentUser = function () {
this.builder.WriteFieldRef(this.name, { LookupId: true });
this.builder.WriteBinaryOperation(this.startIndex, "Eq", "Integer", "{UserID}");
return new QueryToken(this.builder, this.startIndex);
};
UserFieldExpression.prototype.IsInCurrentUserGroups = function () {
this.builder.WriteFieldRef(this.name);
this.builder.WriteMembership(this.startIndex, "CurrentUserGroups");
return new QueryToken(this.builder, this.startIndex);
};
UserFieldExpression.prototype.IsInSPGroup = function (groupId) {
this.builder.WriteFieldRef(this.name);
this.builder.WriteMembership(this.startIndex, "SPGroup", groupId);
return new QueryToken(this.builder, this.startIndex);
};
UserFieldExpression.prototype.IsInSPWebGroups = function () {
this.builder.WriteFieldRef(this.name);
this.builder.WriteMembership(this.startIndex, "SPWeb.Groups");
return new QueryToken(this.builder, this.startIndex);
};
UserFieldExpression.prototype.IsInSPWebAllUsers = function () {
this.builder.WriteFieldRef(this.name);
this.builder.WriteMembership(this.startIndex, "SPWeb.AllUsers");
return new QueryToken(this.builder, this.startIndex);
};
UserFieldExpression.prototype.IsInSPWebUsers = function () {
this.builder.WriteFieldRef(this.name);
this.builder.WriteMembership(this.startIndex, "SPWeb.Users");
return new QueryToken(this.builder, this.startIndex);
};
return UserFieldExpression;
}());
var ModStatFieldExpression = (function () {
function ModStatFieldExpression(builder, name) {
this.builder = builder;
this.name = name;
this.startIndex = builder.tree.length;
}
ModStatFieldExpression.prototype.ModStatId = function () {
return new FieldExpressionToken(this.builder, this.name, "ModStat");
};
ModStatFieldExpression.prototype.IsApproved = function () {
return new FieldExpressionToken(this.builder, this.name, "ModStat").EqualTo(0);
};
ModStatFieldExpression.prototype.IsRejected = function () {
return new FieldExpressionToken(this.builder, this.name, "ModStat").EqualTo(1);
};
ModStatFieldExpression.prototype.IsPending = function () {
return new FieldExpressionToken(this.builder, this.name, "ModStat").EqualTo(2);
};
ModStatFieldExpression.prototype.ValueAsText = function () {
return new FieldExpressionToken(this.builder, this.name, "Text");
};
return ModStatFieldExpression;
}());
var FieldExpressionToken = (function () {
function FieldExpressionToken(builder, name, valueType, isLookupId) {
this.builder = builder;
this.name = name;
this.startIndex = builder.tree.length;
this.valueType = valueType;
this.builder.WriteFieldRef(name, { LookupId: isLookupId });
}
FieldExpressionToken.prototype.IsTrue = function () {
this.builder.WriteBinaryOperation(this.startIndex, "Eq", "Integer", "1");
return new QueryToken(this.builder, this.startIndex);
};
FieldExpressionToken.prototype.IsFalse = function () {
this.builder.WriteBinaryOperation(this.startIndex, "Eq", "Integer", "0");
return new QueryToken(this.builder, this.startIndex);
};
FieldExpressionToken.prototype.IsNull = function () {
this.builder.WriteUnaryOperation(this.startIndex, "IsNull");
return new QueryToken(this.builder, this.startIndex);
};
FieldExpressionToken.prototype.IsNotNull = function () {
this.builder.WriteUnaryOperation(this.startIndex, "IsNotNull");
return new QueryToken(this.builder, this.startIndex);
};
FieldExpressionToken.prototype.EqualTo = function (value) {
if (value instanceof Date)
value = value.toISOString();
if (value === true)
value = 1;
if (value === false)
value = 0;
this.builder.WriteBinaryOperation(this.startIndex, "Eq", this.valueType, value);
return new QueryToken(this.builder, this.startIndex);
};
FieldExpressionToken.prototype.GreaterThan = function (value) {
if (value instanceof Date)
value = value.toISOString();
this.builder.WriteBinaryOperation(this.startIndex, "Gt", this.valueType, value);
return new QueryToken(this.builder, this.startIndex);
};
FieldExpressionToken.prototype.LessThan = function (value) {
if (value instanceof Date)
value = value.toISOString();
this.builder.WriteBinaryOperation(this.startIndex, "Lt", this.valueType, value);
return new QueryToken(this.builder, this.startIndex);
};
FieldExpressionToken.prototype.GreaterThanOrEqualTo = function (value) {
if (value instanceof Date)
value = value.toISOString();
this.builder.WriteBinaryOperation(this.startIndex, "Geq", this.valueType, value);
return new QueryToken(this.builder, this.startIndex);
};
FieldExpressionToken.prototype.LessThanOrEqualTo = function (value) {
if (value instanceof Date)
value = value.toISOString();
this.builder.WriteBinaryOperation(this.startIndex, "Leq", this.valueType, value);
return new QueryToken(this.builder, this.startIndex);
};
FieldExpressionToken.prototype.NotEqualTo = function (value) {
if (value instanceof Date)
value = value.toISOString();
this.builder.WriteBinaryOperation(this.startIndex, "Neq", this.valueType, value);
return new QueryToken(this.builder, this.startIndex);
};
FieldExpressionToken.prototype.Contains = function (value) {
this.builder.WriteBinaryOperation(this.startIndex, "Contains", this.valueType, value);
return new QueryToken(this.builder, this.startIndex);
};
FieldExpressionToken.prototype.BeginsWith = function (value) {
this.builder.WriteBinaryOperation(this.startIndex, "BeginsWith", this.valueType, value);
return new QueryToken(this.builder, this.startIndex);
};
FieldExpressionToken.prototype.In = function (arrayOfValues) {
this.builder.tree.splice(this.startIndex, 0, { Element: "Start", Name: "In" });
this.builder.WriteStart("Values");
for (var i = 0; i < arrayOfValues.length; i++) {
var value = arrayOfValues[i];
if (value instanceof Date)
value = value.toISOString();
this.builder.WriteValueElement(this.valueType, value);
}
this.builder.WriteEnd();
this.builder.WriteEnd();
return new QueryToken(this.builder, this.startIndex);
};
return FieldExpressionToken;
}());
var GroupedQuery = (function () {
function GroupedQuery(builder) {
this.builder = builder;
}
GroupedQuery.prototype.OrderBy = function (fieldInternalName, override, useIndexForOrderBy) {
this.builder.WriteStartOrderBy(override, useIndexForOrderBy);
this.builder.WriteFieldRef(fieldInternalName);
return new SortedQuery(this.builder);
};
GroupedQuery.prototype.OrderByDesc = function (fieldInternalName, override, useIndexForOrderBy) {
this.builder.WriteStartOrderBy(override, useIndexForOrderBy);
this.builder.WriteFieldRef(fieldInternalName, { Descending: true });
return new SortedQuery(this.builder);
};
GroupedQuery.prototype.ToString = function () {
return this.builder.Finalize();
};
GroupedQuery.prototype.ToCamlQuery = function () {
return this.builder.FinalizeToSPQuery();
};
return GroupedQuery;
}());
var SortedQuery = (function () {
function SortedQuery(builder) {
this.builder = builder;
}
SortedQuery.prototype.ThenBy = function (fieldInternalName, override, useIndexForOrderBy) {
this.builder.WriteFieldRef(fieldInternalName);
return new SortedQuery(this.builder);
};
SortedQuery.prototype.ThenByDesc = function (fieldInternalName, override, useIndexForOrderBy) {
this.builder.WriteFieldRef(fieldInternalName, { Descending: true });
return new SortedQuery(this.builder);
};
SortedQuery.prototype.ToString = function () {
return this.builder.Finalize();
};
SortedQuery.prototype.ToCamlQuery = function () {
return this.builder.FinalizeToSPQuery();
};
return SortedQuery;
}());
var Builder = (function () {
function Builder() {
this.tree = new Array();
this.unclosedTags = 0;
}
Builder.prototype.SetAttributeToLastElement = function (tagName, attributeName, attributeValue) {
for (var i = this.tree.length - 1; i >= 0; i--) {
if (this.tree[i].Name == tagName) {
this.tree[i].Attributes = this.tree[i].Attributes || [];
this.tree[i].Attributes.push({ Name: attributeName, Value: attributeValue });
return;
}
}
console.log("CamlJs ERROR: can't find element '" + tagName + "' in the tree while setting attribute " + attributeName + " to '" + attributeValue + "'!");
};
Builder.prototype.WriteRowLimit = function (paged, limit) {
if (paged)
this.tree.push({ Element: "Start", Name: "RowLimit", Attributes: [{ Name: "Paged", Value: "TRUE" }] });
else
this.tree.push({ Element: "Start", Name: "RowLimit" });
this.tree.push({ Element: "Raw", Xml: limit });
this.tree.push({ Element: "End" });
};
Builder.prototype.WriteStart = function (tagName, attributes) {
if (attributes)
this.tree.push({ Element: "Start", Name: tagName, Attributes: attributes });
else
this.tree.push({ Element: "Start", Name: tagName });
};
Builder.prototype.WriteEnd = function (count) {
if (count > 0)
this.tree.push({ Element: "End", Count: count });
else
this.tree.push({ Element: "End" });
};
Builder.prototype.WriteFieldRef = function (fieldInternalName, options) {
var fieldRef = { Element: 'FieldRef', Name: fieldInternalName };
for (var name in options || {}) {
fieldRef[name] = options[name];
}
this.tree.push(fieldRef);
};
Builder.prototype.WriteValueElement = function (valueType, value) {
if (valueType == "Date")
this.tree.push({ Element: "Value", ValueType: "DateTime", Value: value });
else if (valueType == "DateTime")
this.tree.push({ Element: "Value", ValueType: "DateTime", Value: value, IncludeTimeValue: true });
else
this.tree.push({ Element: "Value", ValueType: valueType, Value: value });
};
Builder.prototype.WriteMembership = function (startIndex, type, groupId) {
var attributes = [{ Name: "Type", Value: type }];
if (groupId) {
attributes.push({ Name: "ID", Value: groupId });
}
this.tree.splice(startIndex, 0, { Element: "Start", Name: "Membership", Attributes: attributes });
this.WriteEnd();
};
Builder.prototype.WriteUnaryOperation = function (startIndex, operation) {
this.tree.splice(startIndex, 0, { Element: "Start", Name: operation });
this.WriteEnd();
};
Builder.prototype.WriteBinaryOperation = function (startIndex, operation, valueType, value) {
this.tree.splice(startIndex, 0, { Element: "Start", Name: operation });
this.WriteValueElement(valueType, value);
this.WriteEnd();
};
Builder.prototype.WriteStartGroupBy = function (groupFieldName, collapse) {
if (this.unclosedTags > 0) {
var tagsToClose = this.unclosedTags;
if (this.tree[0].Name == "Query")
tagsToClose--;
else if (this.tree[0].Name == "View")
tagsToClose -= 2;
if (tagsToClose > 0)
this.tree.push({ Element: "End", Count: tagsToClose });
this.unclosedTags -= tagsToClose;
}
if (collapse)
this.tree.push({ Element: "Start", Name: "GroupBy", Attributes: [{ Name: "Collapse", Value: "TRUE" }] });
else
this.tree.push({ Element: "Start", Name: "GroupBy" });
this.tree.push({ Element: "FieldRef", Name: groupFieldName });
this.WriteEnd();
};
Builder.prototype.WriteStartOrderBy = function (override, useIndexForOrderBy) {
if (this.unclosedTags > 0) {
var tagsToClose = this.unclosedTags;
if (this.tree[0].Name == "Query")
tagsToClose--;
else if (this.tree[0].Name == "View")
tagsToClose -= 2;
if (tagsToClose > 0)
this.tree.push({ Element: "End", Count: tagsToClose });
this.unclosedTags -= tagsToClose;
}
var attributes = new Array();
if (override)
attributes.push({ Name: "Override", Value: "TRUE" });
if (useIndexForOrderBy)
attributes.push({ Name: "UseIndexForOrderBy", Value: "TRUE" });
if (attributes.length > 0)
this.tree.push({ Element: "Start", Name: "OrderBy", Attributes: attributes });
else
this.tree.push({ Element: "Start", Name: "OrderBy" });
this.unclosedTags++;
};
Builder.prototype.WriteConditions = function (builders, elementName) {
var pos = this.tree.length;
builders.reverse();
for (var i = 0; i < builders.length; i++) {
var conditionBuilder = builders[i];
if (conditionBuilder.unclosedTags > 0)
conditionBuilder.WriteEnd(conditionBuilder.unclosedTags);
if (i > 0) {
conditionBuilder.tree.splice(0, 0, { Element: "Start", Name: elementName });
this.WriteEnd();
}
Array.prototype.splice.apply(this.tree, [pos, 0].concat(conditionBuilder.tree));
}
};
Builder.prototype.Finalize = function () {
var sb = new window["Sys"].StringBuilder();
var writer = window["SP"].XmlWriter.create(sb);
for (var i = 0; i < this.tree.length; i++) {
if (this.tree[i].Element == "FieldRef") {
writer.writeStartElement("FieldRef");
writer.writeAttributeString("Name", this.tree[i].Name);
if (this.tree[i].LookupId)
writer.writeAttributeString("LookupId", "True");
if (this.tree[i].Descending)
writer.writeAttributeString("Ascending", "False");
for (var attr in this.tree[i]) {
if (attr == "Element" || attr == "Name" || attr == "LookupId" || attr == "Descending")
continue;
writer.writeAttributeString(attr, this.tree[i][attr]);
}
writer.writeEndElement();
}
else if (this.tree[i].Element == "Start") {
writer.writeStartElement(this.tree[i].Name);
if (this.tree[i].Attributes) {
for (var a = 0; a < this.tree[i].Attributes.length; a++) {
writer.writeAttributeString(this.tree[i].Attributes[a].Name, this.tree[i].Attributes[a].Value);
}
}
}
else if (this.tree[i].Element == "Raw") {
writer.writeRaw(this.tree[i].Xml);
}
else if (this.tree[i].Element == "Value") {
writer.writeStartElement("Value");
if (this.tree[i].IncludeTimeValue === true)
writer.writeAttributeString("IncludeTimeValue", "True");
writer.writeAttributeString("Type", this.tree[i].ValueType);
var value = this.tree[i].Value.toString();
if (value.slice(0, 1) == "{" && value.slice(-1) == "}")
writer.writeRaw("<" + value.slice(1, value.length - 1) + " />");
else
writer.writeString(value);
writer.writeEndElement();
}
else if (this.tree[i].Element == "End") {
var count = this.tree[i].Count;
if (count) {
while (count > 0) {