@@ -10,10 +10,11 @@ import { Environment, EnvironmentType } from '@microsoft/sp-core-library';
10
10
import { IWebPartContext } from '@microsoft/sp-webpart-base' ;
11
11
import { ITaxonomyPickerProps } from '../controls/taxonomyPicker/ITaxonomyPicker' ;
12
12
import { IPickerTerm } from '../controls/taxonomyPicker/ITermPicker' ;
13
- import { ITermStore , ITerms , ITerm , IGroup , ITermSet } from './ISPTermStorePickerService' ;
13
+ import { ITermStore , ITerms , ITerm , IGroup , ITermSet , ISuggestTerm } from './ISPTermStorePickerService' ;
14
14
import SPTermStoreMockHttpClient from './SPTermStorePickerMockService' ;
15
15
import { findIndex } from '@microsoft/sp-lodash-subset' ;
16
16
import { ExtensionContext } from '@microsoft/sp-extension-base' ;
17
+ import { EmptyGuid } from '../common/Constants' ;
17
18
18
19
19
20
/**
@@ -23,6 +24,7 @@ export default class SPTermStorePickerService {
23
24
private taxonomySession : string ;
24
25
private formDigest : string ;
25
26
private clientServiceUrl : string ;
27
+ private suggestionServiceUrl : string ;
26
28
27
29
/**
28
30
* Service constructor
@@ -31,6 +33,7 @@ export default class SPTermStorePickerService {
31
33
if ( Environment . type !== EnvironmentType . Local ) {
32
34
{
33
35
this . clientServiceUrl = this . context . pageContext . web . absoluteUrl + '/_vti_bin/client.svc/ProcessQuery' ;
36
+ this . suggestionServiceUrl = this . context . pageContext . web . absoluteUrl + "/_vti_bin/TaxonomyInternalService.json/GetSuggestions" ;
34
37
}
35
38
}
36
39
}
@@ -270,12 +273,12 @@ export default class SPTermStorePickerService {
270
273
* Retrieve all terms that starts with the searchText
271
274
* @param searchText
272
275
*/
273
- public searchTermsByName ( searchText : string , termSet : string ) : Promise < IPickerTerm [ ] > {
276
+ public searchTermsByName ( searchText : string ) : Promise < IPickerTerm [ ] > {
274
277
if ( Environment . type === EnvironmentType . Local ) {
275
278
// If the running environment is local, load the data from the mock
276
279
return SPTermStoreMockHttpClient . searchTermsByName ( searchText ) ;
277
280
} else {
278
- return this . searchTermsByTermSet ( searchText , this . props . termsetNameOrID ) ;
281
+ return this . searchTermsByTermSet ( searchText ) ;
279
282
}
280
283
}
281
284
@@ -375,55 +378,72 @@ export default class SPTermStorePickerService {
375
378
* @param searchText
376
379
* @param termsetId
377
380
*/
378
- private searchTermsByTermSet ( searchText : string , termSet : string ) : Promise < IPickerTerm [ ] > {
381
+ private searchTermsByTermSet ( searchText : string ) : Promise < IPickerTerm [ ] > {
379
382
if ( Environment . type === EnvironmentType . Local ) {
380
383
// If the running environment is local, load the data from the mock
381
384
return SPTermStoreMockHttpClient . searchTermsByName ( searchText ) ;
382
385
} else {
383
386
return new Promise < IPickerTerm [ ] > ( resolve => {
384
387
this . getTermStores ( ) . then ( termStore => {
385
- let TermSetId = termSet ;
386
- if ( ! this . isGuid ( termSet ) ) {
388
+ let TermSetId = this . props . termsetNameOrID ;
389
+ if ( ! this . isGuid ( TermSetId ) ) {
387
390
// Get the ID of the provided term set name
388
- const crntTermSet = this . getTermSetId ( termStore , termSet ) ;
391
+ const crntTermSet = this . getTermSetId ( termStore , TermSetId ) ;
389
392
if ( crntTermSet ) {
390
393
TermSetId = this . cleanGuid ( crntTermSet . Id ) ;
391
394
} else {
392
395
resolve ( null ) ;
393
396
return ;
394
397
}
395
398
}
399
+ if ( termStore === undefined || termStore . length === 0 ) {
400
+ resolve ( null ) ;
401
+ return ;
402
+ }
396
403
397
- let data = `<Request xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009" SchemaVersion="15.0.0.0" LibraryVersion="16.0.0.0" ApplicationName="Javascript Library"><Actions><ObjectPath Id="456" ObjectPathId="455" /><ObjectIdentityQuery Id="457" ObjectPathId="455" /><ObjectPath Id="459" ObjectPathId="458" /><ObjectIdentityQuery Id="460" ObjectPathId="458" /><ObjectPath Id="462" ObjectPathId="461" /><ObjectIdentityQuery Id="463" ObjectPathId="461" /><ObjectPath Id="465" ObjectPathId="464" /><SetProperty Id="466" ObjectPathId="464" Name="TermLabel"><Parameter Type="String">${ searchText } </Parameter></SetProperty><SetProperty Id="467" ObjectPathId="464" Name="DefaultLabelOnly"><Parameter Type="Boolean">true</Parameter></SetProperty><SetProperty Id="468" ObjectPathId="464" Name="StringMatchOption"><Parameter Type="Number">0</Parameter></SetProperty><SetProperty Id="469" ObjectPathId="464" Name="ResultCollectionSize"><Parameter Type="Number">10</Parameter></SetProperty><SetProperty Id="470" ObjectPathId="464" Name="TrimUnavailable"><Parameter Type="Boolean">true</Parameter></SetProperty><ObjectPath Id="472" ObjectPathId="471" /><Query Id="473" ObjectPathId="471"><Query SelectAllProperties="false"><Properties /></Query><ChildItemQuery SelectAllProperties="false"><Properties><Property Name="IsRoot" SelectAll="true" /><Property Name="Id" SelectAll="true" /><Property Name="Name" SelectAll="true" /><Property Name="PathOfTerm" SelectAll="true" /><Property Name="TermSet" SelectAll="true" /></Properties></ChildItemQuery></Query></Actions><ObjectPaths><StaticMethod Id="455" Name="GetTaxonomySession" TypeId="{981cbc68-9edc-4f8d-872f-71146fcbb84f}" /><Method Id="458" ParentId="455" Name="GetDefaultKeywordsTermStore" /><Method Id="461" ParentId="458" Name="GetTermSet"><Parameters><Parameter Type="Guid">${ TermSetId } </Parameter></Parameters></Method><Constructor Id="464" TypeId="{61a1d689-2744-4ea3-a88b-c95bee9803aa}" /><Method Id="471" ParentId="461" Name="GetTerms"><Parameters><Parameter ObjectPathId="464" /></Parameters></Method></ObjectPaths></Request>` ;
404
+ let data = {
405
+ start : searchText ,
406
+ lcid : this . context . pageContext . web . language , // TODO : get the user's navitation LCID. Here it's the default web language LCID
407
+ sspList : this . cleanGuid ( termStore [ 0 ] . Id ) ,
408
+ termSetList : TermSetId ,
409
+ anchorId : this . props . anchorId ? this . props . anchorId : EmptyGuid ,
410
+ isSpanTermStores : false ,
411
+ isSpanTermSets : false ,
412
+ isIncludeUnavailable : this . props . hideTagsNotAvailableForTagging === true ,
413
+ isIncludeDeprecated : this . props . hideDeprecatedTags === true ,
414
+ isAddTerms : false ,
415
+ isIncludePathData : false ,
416
+ excludeKeyword : false ,
417
+ excludedTermset : EmptyGuid
418
+ } ;
398
419
399
420
const reqHeaders = new Headers ( ) ;
400
421
reqHeaders . append ( "accept" , "application/json" ) ;
401
- reqHeaders . append ( "content-type" , "application/xml " ) ;
422
+ reqHeaders . append ( "content-type" , "application/json " ) ;
402
423
403
424
const httpPostOptions : ISPHttpClientOptions = {
404
425
headers : reqHeaders ,
405
- body : data
426
+ body : JSON . stringify ( data )
406
427
} ;
407
428
408
429
409
- return this . context . spHttpClient . post ( this . clientServiceUrl , SPHttpClient . configurations . v1 , httpPostOptions ) . then ( ( serviceResponse : SPHttpClientResponse ) => {
430
+ return this . context . spHttpClient . post ( this . suggestionServiceUrl , SPHttpClient . configurations . v1 , httpPostOptions ) . then ( ( serviceResponse : SPHttpClientResponse ) => {
410
431
return serviceResponse . json ( ) . then ( ( serviceJSONResponse : any ) => {
411
- // Retrieve the term collection results
412
- const termStoreResult : ITerms [ ] = serviceJSONResponse . filter ( ( r : { [ x : string ] : string ; } ) => r [ '_ObjectType_' ] === 'SP.Taxonomy.TermCollection' ) ;
413
- if ( termStoreResult . length > 0 ) {
414
- // Retrieve all terms
415
-
416
- let terms = termStoreResult [ 0 ] . _Child_Items_ ;
417
-
418
- let returnTerms : IPickerTerm [ ] = [ ] ;
419
- terms . forEach ( term => {
420
- if ( term . Name . toLowerCase ( ) . indexOf ( searchText . toLowerCase ( ) ) !== - 1 ) {
421
- returnTerms . push ( this . convertTermToPickerTerm ( term ) ) ;
422
- }
423
- } ) ;
424
- resolve ( returnTerms ) ;
432
+ const groups = serviceJSONResponse . d . Groups ;
433
+ if ( groups && groups . length > 0 ) {
434
+ // Retrieve the term collection results
435
+ const terms : ISuggestTerm [ ] = groups [ 0 ] . Suggestions ;
436
+ if ( terms . length > 0 ) {
437
+ // Retrieve all terms
438
+
439
+ let returnTerms : IPickerTerm [ ] = terms . map ( ( term : ISuggestTerm ) => this . convertSuggestTermToPickerTerm ( term ) ) ;
440
+ resolve ( returnTerms ) ;
441
+ return ;
442
+ }
443
+
425
444
}
426
- return null ;
445
+
446
+ resolve ( [ ] ) ;
427
447
} ) ;
428
448
} ) ;
429
449
} ) ;
@@ -552,4 +572,22 @@ export default class SPTermStorePickerService {
552
572
termSetName : term . TermSet . Name
553
573
} ;
554
574
}
575
+
576
+ private convertSuggestTermToPickerTerm ( term : ISuggestTerm ) : IPickerTerm {
577
+ let path : string = "" ;
578
+ let termSetName : string = "" ;
579
+ if ( term . Paths && term . Paths . length > 0 ) {
580
+ const fullPath = term . Paths [ 0 ] . replace ( / ^ \[ / , "" ) . replace ( / \] $ / , "" ) ;
581
+ const fullPathParts = fullPath . split ( ":" ) ;
582
+ path = fullPathParts . join ( ";" ) + ";" + term . DefaultLabel ;
583
+ termSetName = fullPathParts [ 0 ] ;
584
+ }
585
+ return {
586
+ key : this . cleanGuid ( term . Id ) ,
587
+ name : term . DefaultLabel ,
588
+ path : path ,
589
+ termSet : EmptyGuid , // TermSet Guid is not given with suggestion
590
+ termSetName : termSetName
591
+ } ;
592
+ }
555
593
}
0 commit comments