1
- var csomapi = require ( 'csom-node' ) ,
2
- settings = require ( './settings.js' ) . settings ;
3
-
4
- var webAbsoluteUrl = settings . tenantUrl + settings . webUrl ;
5
-
6
- csomapi . setLoaderOptions ( { url : webAbsoluteUrl } ) ;
7
-
8
-
9
- var authCtx = new AuthenticationContext ( webAbsoluteUrl ) ;
10
- authCtx . acquireTokenForUser ( settings . username , settings . password , function ( err , data ) {
11
-
12
- var ctx = new SP . ClientContext ( settings . webUrl ) ;
13
- authCtx . setAuthenticationCookie ( ctx ) ; //authenticate
14
-
15
- var web = ctx . get_web ( ) ;
16
- console . log ( "1. Read list items" ) ;
17
- readListItems ( web , "Tasks" , function ( items ) {
18
- items . get_data ( ) . forEach ( function ( item ) {
19
- console . log ( item . get_fieldValues ( ) . Title ) ;
20
- } ) ;
21
- console . log ( 'Tasks have been read successfully' ) ;
22
- console . log ( "2. Create list item" ) ;
23
- createListItem ( web , "Tasks" , function ( item ) {
24
-
25
- console . log ( String . format ( 'Task {0} has been created successfully' , item . get_item ( 'Title' ) ) ) ;
26
- console . log ( "3. Update list item" ) ;
27
- updateListItem ( item , function ( item ) {
28
- console . log ( String . format ( 'Task {0} has been updated successfully' , item . get_item ( 'Title' ) ) ) ;
29
- console . log ( "4. Delete list item" ) ;
30
- deleteListItem ( item , function ( ) {
31
- console . log ( 'Task has been deleted successfully' ) ;
32
- } , logError ) ;
33
-
34
- } , logError ) ;
35
-
36
- } , logError ) ;
37
-
38
- } , logError ) ;
39
-
40
- } ) ;
41
-
42
-
43
- function logError ( sender , args ) {
44
- console . log ( 'An error occured: ' + args . get_message ( ) ) ;
45
- }
46
-
47
-
48
- function readListItems ( web , listTitle , success , error ) {
49
- var ctx = web . get_context ( ) ;
50
- var list = web . get_lists ( ) . getByTitle ( listTitle ) ;
51
- var items = list . getItems ( SP . CamlQuery . createAllItemsQuery ( ) ) ;
52
- ctx . load ( items ) ;
53
- ctx . executeQueryAsync ( function ( ) {
54
- success ( items ) ;
55
- } ,
56
- error ) ;
57
- }
58
-
59
- function createListItem ( web , listTitle , success , error ) {
60
- var ctx = web . get_context ( ) ;
61
- var list = web . get_lists ( ) . getByTitle ( listTitle ) ;
62
- var creationInfo = new SP . ListItemCreationInformation ( ) ;
63
- var listItem = list . addItem ( creationInfo ) ;
64
- listItem . set_item ( 'Title' , 'New Task' ) ;
65
- listItem . update ( ) ;
66
- ctx . load ( listItem ) ;
67
- ctx . executeQueryAsync ( function ( ) {
68
- success ( listItem ) ;
69
- } ,
70
- error ) ;
71
- }
72
-
73
-
74
- function updateListItem ( listItem , success , error ) {
75
- var ctx = listItem . get_context ( ) ;
76
- listItem . set_item ( 'Title' , 'New Task (updated)' ) ;
77
- listItem . update ( ) ;
78
- ctx . load ( listItem ) ;
79
- ctx . executeQueryAsync ( function ( ) {
80
- success ( listItem ) ;
81
- } ,
82
- error ) ;
83
- }
84
-
85
- function deleteListItem ( listItem , success , error ) {
86
- var ctx = listItem . get_context ( ) ;
87
- listItem . deleteObject ( ) ;
88
- ctx . executeQueryAsync ( function ( ) {
89
- success ( ) ;
90
- } ,
91
- error ) ;
1
+ var csomapi = require ( 'csom-node' ) ,
2
+ settings = require ( '../settings.js' ) . settings ;
3
+
4
+ csomapi . setLoaderOptions ( { url : settings . siteUrl } ) ;
5
+ var authCtx = new AuthenticationContext ( settings . siteUrl ) ;
6
+ authCtx . acquireTokenForUser ( settings . username , settings . password , function ( err , data ) {
7
+
8
+ var ctx = new SP . ClientContext ( authCtx . path ) ;
9
+ authCtx . setAuthenticationCookie ( ctx ) ; //authenticate
10
+
11
+ var web = ctx . get_web ( ) ;
12
+ console . log ( "1. Read list items" ) ;
13
+ readListItems ( web , "Tasks" , function ( items ) {
14
+ items . get_data ( ) . forEach ( function ( item ) {
15
+ console . log ( item . get_fieldValues ( ) . Title ) ;
16
+ } ) ;
17
+ console . log ( 'Tasks have been read successfully' ) ;
18
+ console . log ( "2. Create list item" ) ;
19
+ createListItem ( web , "Tasks" , function ( item ) {
20
+
21
+ console . log ( String . format ( 'Task {0} has been created successfully' , item . get_item ( 'Title' ) ) ) ;
22
+ console . log ( "3. Update list item" ) ;
23
+ updateListItem ( item , function ( item ) {
24
+ console . log ( String . format ( 'Task {0} has been updated successfully' , item . get_item ( 'Title' ) ) ) ;
25
+ console . log ( "4. Delete list item" ) ;
26
+ deleteListItem ( item , function ( ) {
27
+ console . log ( 'Task has been deleted successfully' ) ;
28
+ } , logError ) ;
29
+
30
+ } , logError ) ;
31
+
32
+ } , logError ) ;
33
+
34
+ } , logError ) ;
35
+
36
+ } ) ;
37
+
38
+
39
+ function logError ( sender , args ) {
40
+ console . log ( 'An error occured: ' + args . get_message ( ) ) ;
41
+ }
42
+
43
+
44
+ function readListItems ( web , listTitle , success , error ) {
45
+ var ctx = web . get_context ( ) ;
46
+ var list = web . get_lists ( ) . getByTitle ( listTitle ) ;
47
+ var items = list . getItems ( SP . CamlQuery . createAllItemsQuery ( ) ) ;
48
+ ctx . load ( items ) ;
49
+ ctx . executeQueryAsync ( function ( ) {
50
+ success ( items ) ;
51
+ } ,
52
+ error ) ;
53
+ }
54
+
55
+ function createListItem ( web , listTitle , success , error ) {
56
+ var ctx = web . get_context ( ) ;
57
+ var list = web . get_lists ( ) . getByTitle ( listTitle ) ;
58
+ var creationInfo = new SP . ListItemCreationInformation ( ) ;
59
+ var listItem = list . addItem ( creationInfo ) ;
60
+ listItem . set_item ( 'Title' , 'New Task' ) ;
61
+ listItem . update ( ) ;
62
+ ctx . load ( listItem ) ;
63
+ ctx . executeQueryAsync ( function ( ) {
64
+ success ( listItem ) ;
65
+ } ,
66
+ error ) ;
67
+ }
68
+
69
+
70
+ function updateListItem ( listItem , success , error ) {
71
+ var ctx = listItem . get_context ( ) ;
72
+ listItem . set_item ( 'Title' , 'New Task (updated)' ) ;
73
+ listItem . update ( ) ;
74
+ ctx . load ( listItem ) ;
75
+ ctx . executeQueryAsync ( function ( ) {
76
+ success ( listItem ) ;
77
+ } ,
78
+ error ) ;
79
+ }
80
+
81
+ function deleteListItem ( listItem , success , error ) {
82
+ var ctx = listItem . get_context ( ) ;
83
+ listItem . deleteObject ( ) ;
84
+ ctx . executeQueryAsync ( function ( ) {
85
+ success ( ) ;
86
+ } ,
87
+ error ) ;
92
88
}
0 commit comments