Skip to content

Commit

Permalink
search terms by term id in type ahead search in taxonomy control
Browse files Browse the repository at this point in the history
Bug fix for pnp#150, Added new method to utilize the anchorId parameter to lookup the terms matching the keyword given under anchorId terms in type ahead search of taxonomy picker control.  
Method name added: searchTermsByTermId
  • Loading branch information
praveenbattula committed Jul 26, 2019
1 parent ac4dfe3 commit bc13ee2
Showing 1 changed file with 72 additions and 20 deletions.
92 changes: 72 additions & 20 deletions src/services/SPTermStorePickerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,30 +258,82 @@ export default class SPTermStorePickerService {
// If the running environment is local, load the data from the mock
return SPTermStoreMockHttpClient.searchTermsByName(searchText);
} else {
return this.searchTermsByTermSet(searchText, this.props.termsetNameOrID).then((response) => {
var _terms = response;
if (anchorId) {
var anchorTerm_1 = _terms.filter((t) => { return t.key.toLowerCase() === anchorId.toLowerCase(); }).shift();
if (anchorTerm_1) {
//var anchorDepth = anchorTerm_1.PathDepth;
var _anchorName = anchorTerm_1.name;
var anchorTerms = _terms.filter((t) => { return t.path.substring(0, anchorTerm_1.path.length) === anchorTerm_1.path && t.key !== anchorTerm_1.key; });
// anchorTerms = anchorTerms.map(function (term) {
// term.PathDepth = term.PathDepth - anchorTerm_1.PathDepth;
// return term;
// });
_terms = anchorTerms;

return _terms;
return this.searchTermsByTermSet(searchText, this.props.termsetNameOrID);
}
}

/**
* Searches terms for the given term Id
* @param searchText
* @param termId
*/
public searchTermsByTermId(searchText: string, termId: string): Promise<IPickerTerm[]> {
if (Environment.type === EnvironmentType.Local) {
// If the running environment is local, load the data from the mock
return SPTermStoreMockHttpClient.searchTermsByName(searchText);
} else {
return new Promise<IPickerTerm[]>(resolve => {
var childTerms = this.getTermsById(termId);
if(childTerms){
resolve(this.searchTermsBySearchText(childTerms ,searchText));
}
else{
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="1" ObjectPathId="0" /><ObjectIdentityQuery Id="2" ObjectPathId="0" /><ObjectPath Id="4" ObjectPathId="3" /><ObjectIdentityQuery Id="5" ObjectPathId="3" /><ObjectPath Id="7" ObjectPathId="6" /><ObjectIdentityQuery Id="8" ObjectPathId="6" /><ObjectPath Id="10" ObjectPathId="9" /><Query Id="11" ObjectPathId="9"><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="0" Name="GetTaxonomySession" TypeId="{981cbc68-9edc-4f8d-872f-71146fcbb84f}" /><Method Id="3" ParentId="0" Name="GetDefaultSiteCollectionTermStore" /><Method Id="6" ParentId="3" Name="GetTerm"><Parameters><Parameter Type="String">'+termId+'</Parameter></Parameters></Method><Property Id="9" ParentId="6" Name="Terms" /></ObjectPaths></Request>';

const reqHeaders = new Headers();
reqHeaders.append("accept", "application/json");
reqHeaders.append("content-type", "application/xml");

const httpPostOptions: ISPHttpClientOptions = {
headers: reqHeaders,
body: data
};

return this.context.spHttpClient.post(this.clientServiceUrl, SPHttpClient.configurations.v1, httpPostOptions).then((serviceResponse: SPHttpClientResponse) => {
return serviceResponse.json().then((serviceJSONResponse: any) => {
// Retrieve the term collection results
const termStoreResult: ITerms[] = serviceJSONResponse.filter((r: { [x: string]: string; }) => r['_ObjectType_'] === 'SP.Taxonomy.TermCollection');
if (termStoreResult.length > 0) {
// Retrieve all terms

let terms = termStoreResult[0]._Child_Items_;

let returnTerms: IPickerTerm[] = [];
terms.forEach(term => {
returnTerms.push({
key: this.cleanGuid(term.Id),
name: term.Name,
path: term.PathOfTerm,
termSet: this.cleanGuid(term.TermSet.Id),
termSetName: term.TermSet.Name
});
});
sessionStorage.setItem(termId, JSON.stringify(returnTerms));
resolve(this.searchTermsBySearchText(returnTerms, searchText));
}
else
return _terms;
}
else
return _terms;
return null;
});
});
}
});
}
}

private searchTermsBySearchText(terms, searchText) {
if(terms){
return terms.filter((t) => { return t.name.toLowerCase().indexOf(searchText.toLowerCase()) > -1 });
}
else
return [];
}

private getTermsById(termId) {
var terms = sessionStorage.getItem(termId);
if(terms)
return JSON.parse(terms);
else
return null;
}

/**
* Searches terms for the given term set
Expand Down

0 comments on commit bc13ee2

Please sign in to comment.