1
1
import { ISPService , ILibsOptions , LibsOrderBy } from "./ISPService" ;
2
- import { ISPLists } from "../common/SPEntities" ;
2
+ import { ISPField , ISPLists } from "../common/SPEntities" ;
3
3
import { WebPartContext } from "@microsoft/sp-webpart-base" ;
4
4
import { ExtensionContext } from "@microsoft/sp-extension-base" ;
5
5
import { SPHttpClient , ISPHttpClientOptions } from "@microsoft/sp-http" ;
@@ -10,7 +10,32 @@ export default class SPService implements ISPService {
10
10
11
11
constructor ( private _context : WebPartContext | ExtensionContext , webAbsoluteUrl ?: string ) {
12
12
this . _webAbsoluteUrl = webAbsoluteUrl ? webAbsoluteUrl : this . _context . pageContext . web . absoluteUrl ;
13
- }
13
+ }
14
+
15
+ public getField = async ( listId : string , internalColumnName : string , webUrl ?: string ) : Promise < ISPField | undefined > => {
16
+ try {
17
+ const webAbsoluteUrl = ! webUrl ? this . _webAbsoluteUrl : webUrl ;
18
+ const apiUrl = `${ webAbsoluteUrl } /_api/web/lists('${ listId } ')/fields/getByInternalNameOrTitle('${ internalColumnName } ')` ;
19
+ const data = await this . _context . spHttpClient . get ( apiUrl , SPHttpClient . configurations . v1 ) ;
20
+ if ( data . ok ) {
21
+ const results = await data . json ( ) ;
22
+ if ( results ) {
23
+ const field = results as ISPField ;
24
+
25
+ if ( field . TypeAsString === 'Calculated' ) {
26
+ const resultTypeRegEx = / R e s u l t T y p e = " ( \w + ) " / gmi;
27
+ const resultTypeMatch = resultTypeRegEx . exec ( field . SchemaXml ) ;
28
+
29
+ field . ResultType = resultTypeMatch [ 1 ] ;
30
+ }
31
+
32
+ return field ;
33
+ }
34
+ }
35
+ } catch ( error ) {
36
+ return Promise . reject ( error ) ;
37
+ }
38
+ }
14
39
15
40
/**
16
41
* Get lists or libraries
@@ -50,15 +75,27 @@ export default class SPService implements ISPService {
50
75
/**
51
76
* Get List Items
52
77
*/
53
- public async getListItems ( filterText : string , listId : string , internalColumnName : string , keyInternalColumnName ?: string , webUrl ?: string , filter ?: string , substringSearch : boolean = false ) : Promise < any [ ] > {
78
+ public async getListItems ( filterText : string , listId : string , internalColumnName : string , field : ISPField | undefined , keyInternalColumnName ?: string , webUrl ?: string , filter ?: string , substringSearch : boolean = false ) : Promise < any [ ] > {
54
79
let returnItems : any [ ] ;
55
- const filterStr = substringSearch ? // JJ - 20200613 - find by substring as an option
56
- `substringof('${ encodeURIComponent ( filterText . replace ( "'" , "''" ) ) } ',${ internalColumnName } )${ filter ? ' and ' + filter : '' } `
57
- : `startswith(${ internalColumnName } ,'${ encodeURIComponent ( filterText . replace ( "'" , "''" ) ) } ')${ filter ? ' and ' + filter : '' } ` ; //string = filterList ? `and ${filterList}` : '';
80
+ const webAbsoluteUrl = ! webUrl ? this . _webAbsoluteUrl : webUrl ;
81
+ let apiUrl = '' ;
82
+ let isPost = false ;
83
+
84
+ if ( field && field . TypeAsString === 'Calculated' ) { // for calculated fields we need to use CAML query
85
+ const camlQuery = `<View><Query><Where>${ substringSearch ? '<Contains>' : '<BeginsWith>' } <FieldRef Name="${ internalColumnName } "/><Value Type="${ field . ResultType } ">${ filterText } </Value>${ substringSearch ? '</Contains>' : '</BeginsWith>' } </Where></Query></View>` ;
86
+
87
+ apiUrl = `${ webAbsoluteUrl } /_api/web/lists('${ listId } ')/GetItems(query=@v1)?$select=${ keyInternalColumnName || 'Id' } ,${ internalColumnName } &@v1=${ JSON . stringify ( { ViewXml : camlQuery } ) } ` ;
88
+ isPost = true ;
89
+ }
90
+ else {
91
+ const filterStr = substringSearch ? // JJ - 20200613 - find by substring as an option
92
+ `substringof('${ encodeURIComponent ( filterText . replace ( "'" , "''" ) ) } ',${ internalColumnName } )${ filter ? ' and ' + filter : '' } `
93
+ : `startswith(${ internalColumnName } ,'${ encodeURIComponent ( filterText . replace ( "'" , "''" ) ) } ')${ filter ? ' and ' + filter : '' } ` ; //string = filterList ? `and ${filterList}` : '';
94
+ apiUrl = `${ webAbsoluteUrl } /_api/web/lists('${ listId } ')/items?$select=${ keyInternalColumnName || 'Id' } ,${ internalColumnName } &$filter=${ filterStr } ` ;
95
+ }
96
+
58
97
try {
59
- const webAbsoluteUrl = ! webUrl ? this . _webAbsoluteUrl : webUrl ;
60
- const apiUrl = `${ webAbsoluteUrl } /_api/web/lists('${ listId } ')/items?$select=${ keyInternalColumnName || 'Id' } ,${ internalColumnName } &$filter=${ filterStr } ` ;
61
- const data = await this . _context . spHttpClient . get ( apiUrl , SPHttpClient . configurations . v1 ) ;
98
+ const data = isPost ? await this . _context . spHttpClient . post ( apiUrl , SPHttpClient . configurations . v1 , { } ) : await this . _context . spHttpClient . get ( apiUrl , SPHttpClient . configurations . v1 ) ;
62
99
if ( data . ok ) {
63
100
const results = await data . json ( ) ;
64
101
if ( results && results . value && results . value . length > 0 ) {
@@ -74,16 +111,16 @@ export default class SPService implements ISPService {
74
111
75
112
76
113
77
- /**
78
- * Gets list items for list item picker
79
- * @param filterText
80
- * @param listId
81
- * @param internalColumnName
82
- * @param [keyInternalColumnName]
83
- * @param [webUrl]
84
- * @param [filterList]
85
- * @returns list items for list item picker
86
- */
114
+ /**
115
+ * Gets list items for list item picker
116
+ * @param filterText
117
+ * @param listId
118
+ * @param internalColumnName
119
+ * @param [keyInternalColumnName]
120
+ * @param [webUrl]
121
+ * @param [filterList]
122
+ * @returns list items for list item picker
123
+ */
87
124
public async getListItemsForListItemPicker (
88
125
filterText : string ,
89
126
listId : string ,
0 commit comments